Making A Beep Noise When Key Is Pressed Down

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
marypoppins_1
Posts: 93
Joined: 28 Oct 2020, 02:58

Making A Beep Noise When Key Is Pressed Down

14 May 2021, 04:01

Hello,
I want to make it so that when a key is pressed down, a beep (censor sound) plays until the key is released.
I'm not sure whether to use SoundBeep or SoundPlay, but this is what I need
Press the key (hotkey)
Sleep for 150ms
now it will continuously check for key state and as long as it is down I want to play a beep. I tried using soundbeep but there was no way to make it play indefinitely and then somehow stop it. This is the code

Code: Select all

Launch_Media::
    Sleep, 150
    MyState := GetKeyState("Launch_Media", "P")
    While(MyState){
        SoundBeep, 1000, 
    }
    Return
I'm not sure if it is possible to have something like a 50ms clip of the beep and then as long as the key is pressed have that play and loop until it's released.
Any help is appreciated. Thank you in advance
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Making A Beep Noise When Key Is Pressed Down

14 May 2021, 04:33

You're checking the value of an unchanging variable in the loop. The variable doesn't know while its in the the loop that you want it to have a new value of GetKeyState just because you assigned it that value once before. You need to update it in the loop. If you do that, you don't even need a variable:

Code: Select all

Launch_Media::
    Sleep, 150
    While GetKeyState("Launch_Media", "P")
        SoundBeep, 1000
    Return
I don't know of a good way of having it play one continuous beep. You could try SoundPlay, but I think it would also have gaps.
Rohwedder
Posts: 7612
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Making A Beep Noise When Key Is Pressed Down

14 May 2021, 05:14

Hallo,
try:

Code: Select all

Launch_Media::
Sleep, 150
FileDelete, Beep.ahk
FileAppend, #NoTrayIcon`nSoundBeep`,1000`,1.E6+0,Beep.ahk
Run, Beep.ahk,,, PID
KeyWait, Launch_Media
Process, Close ,% PID
Return
marypoppins_1
Posts: 93
Joined: 28 Oct 2020, 02:58

Re: Making A Beep Noise When Key Is Pressed Down

14 May 2021, 07:01

I tried Roh's method and it worked perfectly. However my launch media key isn't really a "key" idk it functioned kind of weirdly and doesn't look like most other keys on my keyboard. so I used a different key and it all worked fine. Thank you and have a nice day!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: haomingchen1998, ReyAHK and 267 guests