KeyWait

Waits for a key or mouse/controller button to be released or pressed down.

KeyWait KeyName , Options

パラメータ

KeyName

型:文字列

This can be just about any single character from the keyboard or one of the key names from the key list, such as a mouse/controller button. Controller attributes other than buttons are not supported.

また、vkFFのような明示的な仮想鍵コードを指定することもできる。これは、キーに名前がなく、押しても何も表示されないような場合に便利です。その仮想キーコードは、キー一覧ページの下部にある手順で決定することができます。

Options

型:文字列

If blank or omitted, the function will wait indefinitely for the specified key or mouse/controller button to be physically released by the user. ただし、キーボードフックが装着されておらず、KeyNameSend関数などで人為的に解放されたキーボードキーである場合は、物理的に解放されたとみなされます。マウスフック未装着時のマウスボタンも同様です。

Otherwise, specify a string of one or more of the following options (in any order, with optional spaces in between):

D: Wait for the key to be pushed down.

L: Check the logical state of the key, which is the state that the OS and the active window believe the key to be in (not necessarily the same as the physical state). This option is ignored for controller buttons.

T: Timeout (e.g. T3). タイムアウトして0を返すまでの待ち時間を示す秒数です。キーまたはボタンが指定された状態になった場合、この関数はタイムアウトを待たずに終了します。代わりに、すぐに1が返されます。

タイムアウト値には、2.5などの浮動小数点数を指定できますが、0x03などの16進数値を指定してはいけません。

戻り値

型:整数(ブーリアン)

この関数は、タイムアウトした場合は 0(偽)を、そうでない場合は 1(真)を返します。

備考

キーボードやマウスのボタンの物理的な状態は、キーボードやマウスのフックが設置されていない限り、通常は論理的な状態と同じであり、その場合、ユーザーが物理的にキーを押しているかどうかが正確に反映されます。スクリプトがフックを使用しているかどうかは、KeyHistory関数またはメニュー項目で確認できます。スクリプトにInstallKeybdHook関数とInstallMouseHook関数を追加することで、どちらかまたは両方のフックを強制的にインストールさせることができます。

関数が待機状態にある間、ホットキーカスタムメニュー項目、またはタイマーによって新しいスレッドを起動することができます。

2つ以上のキーが離されるのを待つには、KeyWaitを連続して使用します。事例:

KeyWait "Control"  ; ControlとAltの両方が解除されるのを待つ。
KeyWait "Alt"

To wait for any one key among a set of keys to be pressed down, see InputHook example #4.

GetKeyState, Key List, InputHook, KeyHistory, InstallKeybdHook, InstallMouseHook, ClipWait, WinWait

Aキーが解放されるのを待ちます。

KeyWait "a"

マウスの左ボタンが押し下げられるのを待ちます。

KeyWait "LButton", "D"

Waits up to 3 seconds for the first controller button to be pressed down.

KeyWait "Joy1", "D T3"

左Altキーが論理的に解除されるのを待ちます。

KeyWait "LAlt", "L"

このホットキーを押すと、KeyWaitはユーザーが物理的にCapsLockキーを離すのを待ちます。そのため、以降の記述は、プレスではなくリリースで行っています。この動作は、~CapsLock up::と似ています。

~CapsLock::
{
    KeyWait "CapsLock"  ; ユーザーが物理的に解除するのを待ちます。
    MsgBox "You pressed and released the CapsLock key."
}

Remaps a key or mouse button. (内蔵のリマッピング機能を使った方が簡単なので、この例は説明のためだけです)。次のホットキーでは、NumpadAddが押されている間、マウスボタンが押されたままになっており、NumpadAddが効果的にマウスボタンに変換されています。

*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.
}

キーがダブルプレスされたことを検出します(ダブルクリックに似ています)。KeyWaitは、RControlキーを押しながら他のキーを変更する際に、キーボードのオートリピート機能が不要な二度押しを発生させないようにするために使用します。これは、ホットキーのスレッドを実行し続けることで、#MaxThreadsPerHotkeyがデフォルト設定の1であることを頼りに、自動繰り返しをブロックすることで実現します。For a more elaborate script that distinguishes between single, double and triple-presses, see SetTimer example #3.

~RControl::
{
    if (A_PriorHotkey != "~RControl" or A_TimeSincePriorHotkey > 400)
    {
        ; プレスの間隔が空きすぎて、ダブルプレスになっていません。
        KeyWait "RControl"
        return
    }
    MsgBox "You double-pressed the right control key."
}