Key to play sound while pressed

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Elaphe666
Posts: 131
Joined: 28 Nov 2015, 11:39

Key to play sound while pressed

25 Jun 2016, 07:46

I know how to play a sound with a hotkey, but is it posible to have a beep sound and keep it sounding as long as you hold that key down, just like the way a car horn works? I think I could make a long wav and make the hotkey detect when the key is relased and at that point interrupt the sound play... Any idea?
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Key to play sound while pressed

27 Jun 2016, 11:49

To the best of my knowledge, there is no way for AutoHotkey to create an infinitely long sound - the sound length will be limited to the length of the WAV file you create. However, it is certainly possible to play the sound repeatedly while a key is held down (IE: like an alarm - beep beep beep beep). This should work for any length WAV file (or even a simple system beep)...

Code: Select all

F12:: 	; Replace this with the key of your choice.
SoundToPlay := "*-1"  	; *-1 is the SoundPlay code for the system beep.   You can change this to any other SoundPlay code or to a WAV file path & name.
While GetKeyState(A_ThisHotKey, "P")
{
	If SoundPlaying
		Continue
	Else
		SetTimer, PlayTheSound, -1
}
SoundPlay, DoesNotExist.avi  	; This aborts any currently playing sound file.
Return
 
PlayTheSound:
SoundPlaying := True
SoundPlay, %SoundToPlay%, Wait
If InStr(SoundToPlay, "*")  	; This gives a little time for the system sounds to play, since they ignore the Wait switch.
	Sleep 500
SoundPlaying := False
Return
john_tonson
Posts: 1
Joined: 08 May 2017, 10:41

Re: Key to play sound while pressed

08 May 2017, 11:08

Hello @Shadowpheonix
can I use only to notify on cntl key when pressed?
I tried, but it just work on combination with ! or ^ or +, but they alone it does not work.
e.g; ^d:: is ok but ^:: alone does't work and give the sound!, it considered that as the letter '^' instead of cntl

(because I had a problem in cntl that it does randomly press by its own, i.e; stuck, and I want to check when it does that)
thanks
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Key to play sound while pressed

11 May 2017, 08:50

john_tonson wrote:Hello @Shadowpheonix
can I use only to notify on cntl key when pressed?
I tried, but it just work on combination with ! or ^ or +, but they alone it does not work.
e.g; ^d:: is ok but ^:: alone does't work and give the sound!, it considered that as the letter '^' instead of cntl

(because I had a problem in cntl that it does randomly press by its own, i.e; stuck, and I want to check when it does that)
thanks
^ is a special symbol in AutoHotkey which represents the Ctrl key when used in combination with another key. If you want a hotkey that triggers when Ctrl is pressed by itself, you need to use Ctrl::. :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 102 guests