KeyWait

キーボードやマウス、ジョイスティックのボタンが押される/離されるまで待機

KeyWait, KeyName [, Options]

Parameters

引数名説明
KeyName 文字キーの文字やキー一覧にあるキー名。
ジョイスティックのJoy1...Joy32以外の要素は使用出来ない。
Options
以下のものを半角スペース区切りで指定。
D
押し下げられるのを待つ(通常は離されるのを待つ)
L
強制的に論理的判定を使用
Tn
nに待機する最大秒数を指定(例:T3)。小数も指定可能。

ErrorLevel

Tオプションで指定した秒数が経過した場合「1」、それ以外は「0」

Remarks

Optionsが何も指定されなかった場合、指定したキーが離されるまでずっと待機し続ける。

WindowsNT系でキーボード/マウスフックが使用されている場合、物理的なキー/ボタンの状態(ユーザーが実際にキー/ボタンを押しているか)によって判定される。
#InstallKeybdHook#InstallMouseHook指令で、強制的にフックを有効にすることが可能。

上記の条件に当てはまらない場合、論理的な状態で判定される。
この場合、AutoHotkeyのSendコマンドのようなプログラムによって生成された操作にも反応してしまう。

待機中はホットキーやタイマーなどで起動されたスレッドが割り込める。

複数のキーが指定の状態になるのを待たせたい場合は、複数のKeyWaitを連続して実行すればよい

KeyWait,Control
KeyWait,Alt

複数のキーのうちのどれかが押されるのを待ちたい場合は、Inputコマンドを使う

Related

Key List, GetKeyState, Input, KeyHistory, #InstallKeybdHook, #InstallMouseHook, ClipWait, WinWait

Example(s)

KeyWait, a  ; Wait for the A key to be released.
KeyWait, LButton, D  ; Wait for the left mouse button to be pressed down.
KeyWait, Joy1, D T3  ; Wait up to 3 seconds for the first joystick button to be pressed down.
KeyWait, LAlt, L  ; Wait for the left-alt key to be logically released.

; Hotkey example:
~Capslock::
KeyWait, Capslock  ; Wait for user to physically release it.
MsgBox You pressed and released the Capslock key.
return

; Remapping example:
; The left mouse button is kept held down while NumpadAdd is down,
; which effectively transforms NumpadAdd into the left mouse button.
*NumpadAdd::
MouseClick, left,,, 1, 0, D  ; Hold down the left mouse button.
KeyWait, NumpadAdd   ; Wait for the key to be released.
MouseClick, left,,, 1, 0, U  ; Release the mouse button.
return


; This example shows how to detect when a key has been double-pressed (similar to double-click):
; It relies on #MaxThreadsPerHotkey being at its default setting of 1.
~RControl::
if A_PriorHotkey <> ~RControl
{
	KeyWait, RControl
	return
}
if A_TimeSincePriorHotkey > 400 ; Too much time between presses, so this isn't a double-press.
{
	KeyWait, RControl
	return
}
MsgBox You double-pressed the right control key.
return