Key sounds

Get help with using AutoHotkey and its commands and hotkeys
Eusorph
Posts: 12
Joined: 07 Oct 2017, 14:48

Key sounds

07 Oct 2017, 15:08

Hello,

I have turned to AutoHotkey because I couldn't find a decent piece of windows software that would add typwriting sounds to my keyboard. My reference is the ClickClack plugin in WriteMonkey, which works perfectly (but only inside WriteMonkey.)
So, I have used this piece of simple code that I found here in the forums, adding the other keys that are missing here:

~a::c()
~b::c()
~c::c()

c(){
Soundplay, C:\mechanical-switch.wav
Return
}

It works, but the problem is that the sounds of keypresses get cut off when I touch type at my normal speed, so instead of hearing, "Clak, Clak, Clak," I hear "C/C/Clak" or even worse "C....Clak."
Is there a way to have the sounds of the subquents keypresses play one over the other? That's how it works in WriteMonkey and it is perfect.
I am sure there is way, because a funny thing happens when I leave WriteMonkey open in the background, which is that the script suddenly starts working fine in the same as the ClickClak plugin in WriteMonkey.
I tried to type in the "wait" command, but I must have written it wrong, because the script returns an error.

Thank you,

Lapo
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Key sounds

07 Oct 2017, 15:23

SoundPlay documentation says that it's expected to only play one file at a time. But it does give the hint that that may be on a per script basis. That is, if you launch a separate script, you could have multiple sounds playing.

What I would consider doing is using the Run command to launch a second script that is simply Soundplay, C:\mechanica-switch.wav, 1.

On my system, having just tested, it works. The 1 is necessary at the end because the script would otherwise continue to the second line, which doesn't exist, and then close the script before the sound is even played. My testing code was:

Code: Select all

Loop, 3
Run Eusorph.ahk
return

Code: Select all

; This is Eusorph.ahk
; https://autohotkey.com/boards/viewtopic.php?f=5&t=38007
SoundPlay, C:\Users\Exaskryz\Music\Alert Sound.wav, 1
teadrinker
Posts: 494
Joined: 29 Mar 2015, 09:41
Contact:

Re: Key sounds

07 Oct 2017, 17:57

Also, you could use WMPlayer object like this:

Code: Select all

#Persistent
Loop, Files, %A_WinDir%\Media\*.wav
{
   PlaySound(A_LoopFileFullPath)
   Sleep, 300
} until A_Index = 5

PlaySound(filePath)  {
   static players := []
   player := ComObjCreate("WMPlayer.OCX.7")
   ComObjConnect(player, WatchStatus)
   ( !WatchStatus.players && WatchStatus.players := players )
   Loop
      i := A_Index
   until !players.HasKey(i) && (players[i] := player) && player.url := filePath
}

class WatchStatus  {
   StatusChange(wmp)  {
      static Stopped := 1
      if (wmp.PlayState = Stopped)  {
         for k, v in this.players  {
            if (v = wmp)
               break
         }
         this.players.Delete(k)
      }
   }
}
User avatar
dmg
Posts: 254
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Re: Key sounds

07 Oct 2017, 21:53

Though I am loath to suggest any but a AHk solution, perhaps this will fulfill your needs?
http://www.portablefreeware.com/index.php?id=1806

I have used this, and it works very well. :)
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
Eusorph
Posts: 12
Joined: 07 Oct 2017, 14:48

Re: Key sounds

08 Oct 2017, 02:26

teadrinker wrote:Also, you could use WMPlayer object like this:

Code: Select all

#Persistent
Loop, Files, %A_WinDir%\Media\*.wav
{
   PlaySound(A_LoopFileFullPath)
   Sleep, 300
} until A_Index = 5

PlaySound(filePath)  {
   static players := []
   player := ComObjCreate("WMPlayer.OCX.7")
   ComObjConnect(player, WatchStatus)
   ( !WatchStatus.players && WatchStatus.players := players )
   Loop
      i := A_Index
   until !players.HasKey(i) && (players[i] := player) && player.url := filePath
}

class WatchStatus  {
   StatusChange(wmp)  {
      static Stopped := 1
      if (wmp.PlayState = Stopped)  {
         for k, v in this.players  {
            if (v = wmp)
               break
         }
         this.players.Delete(k)
      }
   }
}
Sorry to be dumb, but I am new to scripting.
So, I this is how I compiled the script, but I always get an error back.
Also, since the script is quite complicated for me (I hoped there was something simpler I could understand.) I should add right away that the sounds provided by the Writemonkey plugin are: key.wav, key2.wav, space.wa, enter.wav, delete.wav. So, I would like to be able normal key sounds for normal keys and then separte sound for Space, Enter, Delete.

Here is how I compiled your script. Get an error on line 4.
#Persistent
Loop, Files, C:\Users\lapom\OneDrive\writemonkey3\plugins\ClickClack\schemes\IBM keyboard\key.wav
{
PlaySound(C:\Users\lapom\OneDrive\writemonkey3\plugins\ClickClack\schemes\IBM keyboard\key.wav)
Sleep, 300
} until A_Index = 5

PlaySound(C:\Users\lapom\OneDrive\writemonkey3\plugins\ClickClack\schemes\IBM keyboard\key.wav) {
static players := []
player := ComObjCreate("WMPlayer.OCX.7")
ComObjConnect(player, WatchStatus)
( !WatchStatus.players && WatchStatus.players := players )
Loop
i := A_Index
until !players.HasKey(i) && (players := player) && player.url := filePath
}

class WatchStatus {
StatusChange(wmp) {
static Stopped := 1
if (wmp.PlayState = Stopped) {
for k, v in this.players {
if (v = wmp)
break
}
this.players.Delete(k)
}
}
}
Eusorph
Posts: 12
Joined: 07 Oct 2017, 14:48

Re: Key sounds

08 Oct 2017, 02:40

Exaskryz wrote:SoundPlay documentation says that it's expected to only play one file at a time. But it does give the hint that that may be on a per script basis. That is, if you launch a separate script, you could have multiple sounds playing.

What I would consider doing is using the Run command to launch a second script that is simply Soundplay, C:\mechanica-switch.wav, 1.

On my system, having just tested, it works. The 1 is necessary at the end because the script would otherwise continue to the second line, which doesn't exist, and then close the script before the sound is even played. My testing code was:

Code: Select all

Loop, 3
Run Eusorph.ahk
return

Code: Select all

; This is Eusorph.ahk
; https://autohotkey.com/boards/viewtopic.php?f=5&t=38007
SoundPlay, C:\Users\Exaskryz\Music\Alert Sound.wav, 1

OK, so I put the run command into my first script and the sounds can't keep up and end up screeching. I set loop to 1 and it almost work, but now really. When I write at my normal speed, as usual, sounds stop playing and then play all at once in a Trrrrrr when I stop pressing keys.
Eusorph
Posts: 12
Joined: 07 Oct 2017, 14:48

Re: Key sounds

08 Oct 2017, 02:42

dmg wrote:Though I am loath to suggest any but a AHk solution, perhaps this will fulfill your needs?
http://www.portablefreeware.com/index.php?id=1806

I have used this, and it works very well. :)
Qwertick, like any other piece of software I have tried doens't work. It plays sounds the same as the script I have written: which means that they cut off. When I used a Mac, there were several apps that worked fine, but on Windows there is none. Anyone wants to write it?
teadrinker
Posts: 494
Joined: 29 Mar 2015, 09:41
Contact:

Re: Key sounds

08 Oct 2017, 02:45

It was just an example. To assign sounds to hotkeys, you should create the script like this:

Code: Select all

~a:: PlaySound("C:\Users\lapom\OneDrive\writemonkey3\plugins\ClickClack\schemes\IBM keyboard\key.wav")
~b:: PlaySound("C:\Users\lapom\OneDrive\writemonkey3\plugins\ClickClack\schemes\IBM keyboard\key2.wav")
~c:: PlaySound("C:\Users\lapom\OneDrive\writemonkey3\plugins\ClickClack\schemes\IBM keyboard\delete.wav")

PlaySound(filePath)  {
   static players := []
   player := ComObjCreate("WMPlayer.OCX.7")
   ComObjConnect(player, WatchStatus)
   ( !WatchStatus.players && WatchStatus.players := players )
   Loop
      i := A_Index
   until !players.HasKey(i) && (players[i] := player) && player.url := filePath
}

class WatchStatus  {
   StatusChange(wmp)  {
      static Stopped := 1
      if (wmp.PlayState = Stopped)  {
         for k, v in this.players  {
            if (v = wmp)
               break
         }
         this.players.Delete(k)
      }
   }
}
Eusorph
Posts: 12
Joined: 07 Oct 2017, 14:48

Re: Key sounds

08 Oct 2017, 02:56

teadrinker wrote:It was just an example. To assign sounds to hotkeys, you should create the script like this:

Code: Select all

~a:: PlaySound("C:\Users\lapom\OneDrive\writemonkey3\plugins\ClickClack\schemes\IBM keyboard\key.wav")
~b:: PlaySound("C:\Users\lapom\OneDrive\writemonkey3\plugins\ClickClack\schemes\IBM keyboard\key2.wav")
~c:: PlaySound("C:\Users\lapom\OneDrive\writemonkey3\plugins\ClickClack\schemes\IBM keyboard\delete.wav")

PlaySound(filePath)  {
   static players := []
   player := ComObjCreate("WMPlayer.OCX.7")
   ComObjConnect(player, WatchStatus)
   ( !WatchStatus.players && WatchStatus.players := players )
   Loop
      i := A_Index
   until !players.HasKey(i) && (players[i] := player) && player.url := filePath
}

class WatchStatus  {
   StatusChange(wmp)  {
      static Stopped := 1
      if (wmp.PlayState = Stopped)  {
         for k, v in this.players  {
            if (v = wmp)
               break
         }
         this.players.Delete(k)
      }
   }
}

Thank you. Done, but it still doesn't work. Sounds cut each other off and then kind of machine gun back to life as soon as there is a gap in typing.
Has anybody tried giving a look at the code inside WriteMonkey ClickClack plugin? I have the beta and it can be opened as a Json file to be read.
Again, with my normal script, when WriteMonkey runs in the background everything sounds fine.
teadrinker
Posts: 494
Joined: 29 Mar 2015, 09:41
Contact:

Re: Key sounds

08 Oct 2017, 03:24

Unfortunately, I don't know anything about WriteMonkey, and can't say, what is the issue. I tested my code on Windows 7 and 10, and for me it works fine, sounds don't cut each other off. May be, there is a conflict between AHK and WriteMonkey. Did you try turn WriteMonkey off and launch the script separately? What are your OS and AHK versions?
Eusorph
Posts: 12
Joined: 07 Oct 2017, 14:48

Re: Key sounds

08 Oct 2017, 03:47

teadrinker wrote:Unfortunately, I don't know anything about WriteMonkey, and can't say, what is the issue. I tested my code on Windows 7 and 10, and for me it works fine, sounds don't cut each other off. May be, there is a conflict between AHK and WriteMonkey. Did you try turn WriteMonkey off and launch the script separately? What are your OS and AHK versions?
I am on a Surface Pro 4 with Windows 10 1703. AHK is the latest (I downloaded it yesterday.) WriteMonkey is portable so it only runs when you open it and I have of course run the scripts without WriteMonkey-I said that only to point out the strange behaviout that to me means something under the hood is working.
Are you sure your sounds don't cut off? Have you tried touch typing? If you just push the key slowly it's fine, but touch typing breaks it.
Eusorph
Posts: 12
Joined: 07 Oct 2017, 14:48

Re: Key sounds

08 Oct 2017, 03:49

teadrinker wrote:Unfortunately, I don't know anything about WriteMonkey, and can't say, what is the issue. I tested my code on Windows 7 and 10, and for me it works fine, sounds don't cut each other off. May be, there is a conflict between AHK and WriteMonkey. Did you try turn WriteMonkey off and launch the script separately? What are your OS and AHK versions?
Sorry to be dumb again, but could you write down the "full" script I should use. The first one and the second one you posted are different. The second one lacks the #Persistent and the whole first part. Is that why it doesn't work for me? You are talking to someone who doesn't code, so you should assume I am as dumb as a rock on this.

Thanks
teadrinker
Posts: 494
Joined: 29 Mar 2015, 09:41
Contact:

Re: Key sounds

08 Oct 2017, 03:56

:D My second code should work as is, for keys a b c, if you specify right paths.
Eusorph
Posts: 12
Joined: 07 Oct 2017, 14:48

Re: Key sounds

08 Oct 2017, 04:02

teadrinker wrote::D My second code should work as is, for keys a b c, if you specify right paths.
Ok, thanks. That's what I did. I'll try rebooting.
teadrinker
Posts: 494
Joined: 29 Mar 2015, 09:41
Contact:

Re: Key sounds

08 Oct 2017, 04:07

If your OS Win 10, there are next files in it:

Code: Select all

~a:: PlaySound("C:\Windows\Media\Alarm03.wav")
~b:: PlaySound("C:\Windows\Media\Alarm04.wav")
~c:: PlaySound("C:\Windows\Media\Alarm05.wav")
~d:: PlaySound("C:\Windows\Media\Alarm06.wav")

PlaySound(filePath)  {
   static players := []
   player := ComObjCreate("WMPlayer.OCX.7")
   ComObjConnect(player, WatchStatus)
   ( !WatchStatus.players && WatchStatus.players := players )
   Loop
      i := A_Index
   until !players.HasKey(i) && (players[i] := player) && player.url := filePath
}

class WatchStatus  {
   StatusChange(wmp)  {
      static Stopped := 1
      if (wmp.PlayState = Stopped)  {
         for k, v in this.players  {
            if (v = wmp)
               break
         }
         this.players.Delete(k)
      }
   }
}
Try this code.
Eusorph
Posts: 12
Joined: 07 Oct 2017, 14:48

Re: Key sounds

08 Oct 2017, 04:25

teadrinker wrote:If your OS Win 10, there are next files in it:

Code: Select all

~a:: PlaySound("C:\Windows\Media\Alarm03.wav")
~b:: PlaySound("C:\Windows\Media\Alarm04.wav")
~c:: PlaySound("C:\Windows\Media\Alarm05.wav")
~d:: PlaySound("C:\Windows\Media\Alarm06.wav")

PlaySound(filePath)  {
   static players := []
   player := ComObjCreate("WMPlayer.OCX.7")
   ComObjConnect(player, WatchStatus)
   ( !WatchStatus.players && WatchStatus.players := players )
   Loop
      i := A_Index
   until !players.HasKey(i) && (players[i] := player) && player.url := filePath
}

class WatchStatus  {
   StatusChange(wmp)  {
      static Stopped := 1
      if (wmp.PlayState = Stopped)  {
         for k, v in this.players  {
            if (v = wmp)
               break
         }
         this.players.Delete(k)
      }
   }
}
Try this code.
Tried. Your code works-I can hear the echoes, but as soon as I replace your sounds with my typewriting ones, it doesn't anymore. Could the problem be that the typewriting sounds are so short, the next sound doesn't have the time to echo over the preceding one?
Here is the link to the sounds if you want to try them: https://1drv.ms/f/s!Ajj5J2FF0ZjL9kFFmN4MYEOWA03B
teadrinker
Posts: 494
Joined: 29 Mar 2015, 09:41
Contact:

Re: Key sounds

08 Oct 2017, 04:52

Eusorph wrote:Could the problem be that the typewriting sounds are so short, the next sound doesn't have the time to echo over the preceding one?
I think so. I've tried, all sounds work without interruptions.
Eusorph
Posts: 12
Joined: 07 Oct 2017, 14:48

Re: Key sounds

08 Oct 2017, 04:53

teadrinker wrote:
Eusorph wrote:Could the problem be that the typewriting sounds are so short, the next sound doesn't have the time to echo over the preceding one?
I think so. I've tried, alll sounds work without interruptions.
You mean you downloaded the typewriting sounds and they work on your system?
teadrinker
Posts: 494
Joined: 29 Mar 2015, 09:41
Contact:

Re: Key sounds

08 Oct 2017, 05:12

Actually, if I type fast, there are delays, unfortunately.
teadrinker
Posts: 494
Joined: 29 Mar 2015, 09:41
Contact:

Re: Key sounds

08 Oct 2017, 05:29

More precisely, on my laptop there are delays, on the desktop computer there aren't.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Xtra and 94 guests