How KeyWait works?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

How KeyWait works?

23 Feb 2018, 06:45

I don't understand how KeyWait works.

Here is an example from docs:

Code: Select all

~CapsLock::
    KeyWait, CapsLock  ; Wait for user to physically release it.
    MsgBox You pressed and released the CapsLock key.
return
But, from my point of view, it's simply misleads. If you change the key in 2nd line to something different ("z"), the message box still will be shown:

Code: Select all

; Press CapsLock and you will see message box, despite of "KeyWait, z"
~CapsLock::
    KeyWait, z
    MsgBox You pressed and released the CapsLock key.
return
From my point of view, this example probably will be better:

Code: Select all

keyWait, rButton, d
keyWait, rButton
soundBeep, 300, 300
return
And here is the quote about the possibe problem with original example:
camerb (moderator) wrote:Yeah, I've heard of this mistake being made before... perhaps the docs need to have a note in there about how it waits for the key to be in the up state, rather than an up action.

Source: https://autohotkey.com/board/topic/6648 ... /?p=420580
So, is my example is really correct and example in docs isn't correct?

And also, how I could change my example in this way? :

Code: Select all

; Doesn't correctly work: at first mouse click there is only short beep
rButton::
    soundBeep, 300, 300
    keyWait, rButton, d
    keyWait, rButton
    soundBeep, 300, 1000
return
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: How KeyWait works?

23 Feb 2018, 07:00

It's not misleading because KeyWait clearly says that it waits for a key to be released if no options are mentioned. In your case I somehow agree but this has been
answered before like you have said.

How it works?
The msgbox is sent in the second example because z was up the entire time. Change 'KeyWait, Capslock' to 'KeyWait, Capslock, D' that should make more sense.
When D is mentioned keywait waits for the key to be pressed down instead of released. The example is valid because the hotkey itself is Capslock. And hotkeys without
the UP modifier trigger when keys are held down. Notice how it says "You pressed and released" instead of just pressed? So when the key was held down, the keywait
activated and when the key was released the msgbox appeared.

So the example kinda makes sense. But I agree a little more explanation wouldn't hurt.
I am your average ahk newbie. Just.. a tat more cute. ;)
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: How KeyWait works?

23 Feb 2018, 07:22

Nwb wrote:...
Thank you very much. But I can't understand how the code below works.

For some reason:

- If I press "x" quickly, there will be only short beep on 1st press, but on 2nd press it will be long beep
- If I press "x", then delay the finger on the button and then release it, it will be both short and long beeps.

Isn't strange?

Code: Select all

#singleInstance, force

x::
	soundBeep, 300, 300
	keyWait, x, d
	keyWait, x
	soundBeep, 300, 1000
return
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: How KeyWait works?

23 Feb 2018, 08:15

No, it's not strange at all. If you press and release it quickly, you've released it before the first beep is finished, then it's waiting on the key press and release before the second beep. If you hold it down longer, you've held it down longer than the duration of the first beep, so when it gets to the first KeyWait, it is true that it is down so it continues and just waits for you to release it. It doesn't wait for a second press.

This time you seem to think that the d option is waiting for the action of pressing the key down. It is just waiting for it to be down, just like the other one is waiting for it to be up, not the action of it being released.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: How KeyWait works?

23 Feb 2018, 08:30

boiler wrote:...
Now, it seems I understand it completely :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750, septrinus, songdg and 261 guests