Function Reference


_WinAPI_GetAsyncKeyState

Determines whether a key is up or down at the time the function is called

#include <WinAPISys.au3>
_WinAPI_GetAsyncKeyState ( $iKey )

Parameters

$iKey Key to test for

Return Value

Success: If the most significant bit is set the key is down, and if the least significant bit is set, the key was pressed after the previous call to _WinAPI_GetAsyncKeyState(). However, you should not rely on this last behavior; for more information, see the Remarks.
Failure: The return value is zero if the key isn't down, or for the following cases:
The current desktop is not the active desktop
The foreground thread belongs to another process and the desktop does not allow the hook or the journal record.

Remarks

The constants to use for $iKey can be found in WinAPIvkeysConstants.au3

The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call _WinAPI_GetAsyncKeyState($VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling _WinAPI_GetSystemMetrics($SM_SWAPBUTTON), which returns TRUE if the mouse buttons have been swapped.

Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.

You can use the virtual-key code constants $VK_SHIFT, $VK_CONTROL, and $VK_MENU as values for the vKey parameter. This gives the state of the SHIFT, CTRL, or ALT keys without distinguishing between left and right.

See Also

Search GetAsyncKeyState in MSDN Library.

Example

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WinAPISys.au3>
#include <WinAPIvkeysConstants.au3>

Example()

Func Example()
    Local $hGUI = GUICreate("_WinAPI_GetAsyncKeyState Demo", 500, 300)
    GUICtrlCreateLabel("Press the number to select the task you wish to use from list below", 10, 30)
    GUICtrlCreateLabel("Press 1 key for task 1" & @CRLF & @CRLF & _
            "Press 2 key for task 2" & @CRLF & @CRLF & _
            "Press 3 key for task 3" & @CRLF & @CRLF & _
            "Press the ESCAPE key, or click the close button, to exit", 10, 60)
    GUISetState(@SW_SHOW)

    Local Const $iBitMask = 0x8000 ; a bit mask to strip the high word bits from the return of the function.
    While GUIGetMsg() <> $GUI_EVENT_CLOSE
        If BitAND(_WinAPI_GetAsyncKeyState($VK_1), $iBitMask) <> 0 Or BitAND(_WinAPI_GetAsyncKeyState($VK_NUMPAD1), $iBitMask) <> 0 Then
            MsgBox($MB_SYSTEMMODAL, "_WinAPI_GetAsyncKeyState", "Task 1")
        ElseIf BitAND(_WinAPI_GetAsyncKeyState($VK_2), $iBitMask) <> 0 Or BitAND(_WinAPI_GetAsyncKeyState($VK_NUMPAD2), $iBitMask) <> 0 Then
            MsgBox($MB_SYSTEMMODAL, "_WinAPI_GetAsyncKeyState", "Task 2")
        ElseIf BitAND(_WinAPI_GetAsyncKeyState($VK_3), $iBitMask) <> 0 Or BitAND(_WinAPI_GetAsyncKeyState($VK_NUMPAD3), $iBitMask) <> 0 Then
            MsgBox($MB_SYSTEMMODAL, "_WinAPI_GetAsyncKeyState", "Task 3")
        ElseIf BitAND(_WinAPI_GetAsyncKeyState($VK_ESCAPE), $iBitMask) <> 0 Then
            MsgBox($MB_SYSTEMMODAL, "_WinAPI_GetAsyncKeyState", "The Esc Key was pressed, exiting.")
            ExitLoop
        EndIf
    WEnd
    GUIDelete($hGUI)
EndFunc   ;==>Example