Keywait and simultaneous keypresses

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Sure
Posts: 1
Joined: 29 Jul 2021, 17:47

Keywait and simultaneous keypresses

Post by Sure » 29 Jul 2021, 20:16

Hi,
I'm still fairly new to AHK so I'm not quite sure how trivial this is. I have the following script set up.

Code: Select all

$a::
    Send {a down}
    keyWait, a
    Send {A}
    Send {a up}
return

$d::
    Send {d down}
    keyWait, d
    Send {D}
    Send {d up}
return
These work fine on their own but in the following sequence with simultaneously held keys I'm getting an undesired result.
Here's the outputs I expected (left) | received (right)
hold a: a | a
hold d: ad | ad
release a: adA | ad
release d: adAD | adDA

I can sort of understand the intuition behind how this works (feel free to elaborate if you're so inclined) but I'm a bit lost as to how to get to my desired output.

Thanks in advance for any help and / or pointers you can provide!
User avatar
mikeyww
Posts: 27042
Joined: 09 Sep 2014, 18:38

Re: Keywait and simultaneous keypresses

Post by mikeyww » 29 Jul 2021, 21:59

The standard AHK version 1.x runs one thread at a time. When you press a second hotkey, it interrupts the running routine until the new routine is finished. Try:

Code: Select all

a Up::
d Up::Send % "+" SubStr(A_ThisHotkey, 1, 1)
$a::
$d::
Send % hk := SubStr(A_ThisHotkey, 2)
KeyWait, %hk%
Return
Comment: after you Send {a down}A, A is up, so there is no need to send it up immediately again. Inspect the KeyHistory to understand this.

Code: Select all

#InstallKeybdHook
F3::Send {a down}A
image210729-2307-001.png
image210729-2307-001.png (7.52 KiB) Viewed 119 times
Post Reply

Return to “Ask for Help (v1)”