GetKeyState

Returns 1 (true) or 0 (false) depending on whether the specified keyboard key or mouse/controller button is down or up. Also retrieves controller status.

IsDown := GetKeyState(KeyName , Mode)

パラメータ

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. 例:B, 5, LWin, RControl, Alt, Enter, Escape, LButton, MButton, Joy1.

また、vkFFのような明示的な仮想鍵コードを指定することも可能です。これは、キーに名前がないような稀なケースに有効です。このようなキーのコードは、キー一覧ページ下部の手順で判別することができます。なお、このコードは16進数である必要があります。

既知の制限事項:この機能は、LeftとNumpadLeftのように、同じ仮想キーコードを共有する2つのキーを区別することはできません。

Mode

型:文字列

This parameter is ignored when retrieving controller status.

If omitted, it defaults to that which retrieves the logical state of the key. OSやアクティブウィンドウがキーがあると信じている状態であり、必ずしも物理的な状態と同じではありません。

そうでなければ、次の文字のうち、ひとつを指定してください。

P: Retrieve the physical state (i.e. whether the user is physically holding it down). キーボードやマウスのボタンの物理的な状態は、キーボードやマウスのフックがインストールされていない限り、論理的な状態と同じになるのが普通で、その場合、ユーザーが物理的にキーやボタンを押し続けているかどうかを正確に反映します(スクリプトの実行中に押し続けている限りは)。スクリプトがフックを使用しているかどうかは、KeyHistory関数またはメニュー項目で確認できます。InstallKeybdHookおよび/またはInstallMouseHookを呼び出すことで、フックを強制的にインストールすることができます。

T: Retrieve the toggle state. CapsLockNumLockScrollLock以外のキーについては、スクリプト起動時のトグル状態は一般に0であり、プロセス間で同期されることはない。

戻り値

型:Integer (boolean), Float, Integer or String (empty)

This function returns 1 (true) if the key is down (or toggled on) or 0 (false) if it is up (or toggled off).

When KeyName is a stick axis such as JoyX, this function returns a floating-point number between 0 and 100 to indicate the stick's position as a percentage of that axis's range of motion. This test script can be used to analyze your controller(s).

When KeyName is JoyPOV, this function returns an integer between 0 and 35900. The following approximate POV values are used by many controllers:

When KeyName is JoyName, JoyButtons, JoyAxes or JoyInfo, the retrieved value will be the name, number of buttons, number of axes or capabilities of the controller. For details, see Game Controller.

When KeyName is a button or control of a controller that could not be detected, this function returns an empty string.

エラー処理

A ValueError is thrown if invalid parameters are detected, e.g. when KeyName does not exist on the current keyboard layout.

備考

To wait for a key or mouse/controller button to achieve a new state, it is usually easier to use KeyWait instead of a GetKeyState loop.

キーボードドライバが特殊なシステムでは、キーの状態、特にCapsLockのようなキーのトグル状態の更新に時間がかかることがあります。このような鍵の状態を変更直後に確認するスクリプトは、鍵の状態を更新する時間をシステムに与えるために、事前にSleepを使用することができます。

For examples of using GetKeyState with a controller, see the controller remapping page and the Controller-To-Mouse script.

GetKeyVK, GetKeySC, GetKeyName, KeyWait, Key List, Controller remapping, KeyHistory, InstallKeybdHook, InstallMouseHook

マウスの右ボタンの現在の状態を取得します。

state := GetKeyState("RButton")

Retrieves the current state of the first controller's second button.

state := GetKeyState("Joy2")

少なくとも1つのShiftがダウンしているかどうかをチェックします。

if GetKeyState("Shift")
    MsgBox "At least one Shift key is down."
else
    MsgBox "Neither Shift key is down."

CapsLockの現在のトグル状態を取得します。

state := GetKeyState("CapsLock", "T")

リマッピングです。(内蔵のリマッピング機能を使った方が簡単なので、この例は説明のためだけです)。次のホットキーでは、NumpadAddが押されている間、マウスボタンが押されたままになっており、NumpadAddが効果的にマウスボタンに変換されています。この方法は、ユーザーがキーやボタンを押しながら動作を繰り返す場合にも使用できます。

*NumpadAdd::
{
    MouseClick "left",,, 1, 0, "D"  ; Hold down the left mouse button.
    Loop
    {
        Sleep 10
        if !GetKeyState("NumpadAdd", "P")  ; The key has been released, so break out of the loop.
            break
        ; ... insert here any other actions you want repeated.
    }
    MouseClick "left",,, 1, 0, "U"  ; Release the mouse button.
}

Makes controller button behavior depend on stick axis position.

joy2::
{
    JoyX := GetKeyState("JoyX")
    if JoyX > 75
        MsgBox "Action #1 (button pressed while stick was pushed to the right)."
    else if JoyX < 25
        MsgBox "Action #2 (button pressed while stick was pushed to the left)."
    else
        MsgBox "Action #3 (button pressed while stick was centered horizontally)."
}

See the controller remapping page and the Controller-To-Mouse script for other examples.