Hello,
KeyWait with a timeout will wait for the timeout or the wait condition to be met, whichever comes first. With default thread priorities, a triggered hotkey interrupts other threads unless the other thread uses an uninterruptible statement. An interrupted thread resumes when the interrupting thread ends. Suggestions are below.
- Post the script that does not work.
- Describe what the script does when you run it.
- Describe what the script should do, step by step. If possible, include a detailed example.
If you are waiting for a key to be released or something else to happen, you might want to use a different approach. One option is adding a key-up hotkey that then does what you want when the key is released. That hotkey can check to see if something else has happened.
A demonstration is below.
Code: Select all
#Requires AutoHotkey v2.0
timer := 0
F2 Up:: { ; F2 = Start to wait for RButton to be released, or F3 to be pressed
Global timer
SetTimer wait, timer := -500
SoundBeep 1500
}
#HotIf timer ; Hotkeys are active only when timer is running
~RButton Up::
F3:: {
Global timer
SetTimer wait, timer := 0 ; Stop the timer
MsgBox ThisHotkey, 'First action', 'Iconi' ; Show which hotkey was triggered
}
#HotIf
wait() { ; Timer expired before a hotkey was triggered
Global timer := 0
MsgBox 'NONE', 'First action', 'Icon!'
}
You can also do it more simply as follows.
Code: Select all
#Requires AutoHotkey v2.0
RButton:: {
timeout := !KeyWait(ThisHotkey, 'T.5')
If A_ThisHotkey = ThisHotkey
MsgBox timeout, 'Timeout', 'Iconi'
Else MsgBox 'A_ThisHotkey = ' A_ThisHotkey '`n`nThisHotkey = ' ThisHotkey, 'Hotkeys', 'Icon!'
}
#HotIf GetKeyState('RButton', 'P')
F3::MsgBox ThisHotkey, 'Pressed', 'Icon!'
#HotIf
The key with this latter script is that, although you are not canceling the
KeyWait, the desired action occurs in the other function (
F3), and the statement after the KeyWait is conditional.