Function Reference


_IsPressed

Check if key has been pressed

#include <Misc.au3>
_IsPressed ( [$vKey = Default [, $vDLL = Default [, $bCheckModifierKey = False]]] )

Parameters

$vKey [optional] Key to check for (string), $VK_* as defined in "WinAPIvkeysConstants.au3" or an array of Key to be pressed
If Default then the internal handle to DLL will be closed. Don't forget to close it by _IsPressed() when no longer needed.
$vDLL [optional] Handle to DLL or default to user32.dll
$bCheckModifierKey [optional] the SHIFT, CTRL, ALT or WIN modifier keys will be verified to avoid "modifier key" + $vKey return for just $vKey (See Remarks) (Default)

Return Value

>0: if the key is pressed or the one-based of the selected key in the array (See Remarks).
0: if the key is not pressed or no _IsPressed(...) on _IsPressed().

Remarks

If calling this function repeatedly, you should open 'user32.dll' and pass the handle.
Make sure to close the handle at end of script using DllClose().
If you use $vDLL = Default and you don't terminate the AutoIt script and yous stop using _IsPressed(...)
then call _IsPressed() to close the internal used handle.

If $vKey is a string or a $VK_* as defined in "WinAPIvkeysConstants.au3" then
    _IsPressed() will return 1 until the key is pressed.
    Even brief key presses can result in multiple returns within a loop.
    If the code called does not include a blocking function (such as MsgBox) and the user does not require multiple returns,
    the script should wait until _IsPressed() returns 0 before continuing (See example 1).

If $vKey is an array then the function returns when the key is pressed,
    the return value is 1 if the $vKey[0] is pressed, 2 if the $vKey[1] is pressed, ... (See example 2).

When $bCheckModifierKey is Not True, then the combinaison of a "modifier key" + key will return 0,
    and the @extended is set when the modifier key pressed to: 1 = SHIFT, 2 = CTRL, 3 = ALT, 4 = WIN, 5 = right WIN (See example 2).

If $bCheckModifierKey = True then the return occur when the "modifier key" is released.
If $bCheckModifierKey = -1 then the return occur when the "modifier key" is pressed.

VirtualKey String Key
$VK_LBUTTON 01 Left mouse button
$VK_RBUTTON 02 Right mouse button
$VK_CANCEL 03 Control-break processing
$VK_MBUTTON 04 Middle mouse button (three-button mouse)
$VK_XBUTTON1 05 X1 mouse button
$VK_XBUTTON2 06 X2 mouse button
$VK_BACK 08 BACKSPACE key
$VK_TAB 09 TAB key
$VK_CLEAR 0C CLEAR key
$VK_RETURN 0D ENTER key
$VK_SHIFT 10 SHIFT key
$VK_CONTROL 11 CTRL key
$VK_MENU 12 ALT key
$VK_PAUSE 13 PAUSE key
$VK_CAPITAL 14 CAPS LOCK key
$VK_ESCAPE 1B ESC key
$VK_SPACE 20 SPACEBAR
$VK_PRIOR 21 PAGE UP key
$VK_NEXT 22 PAGE DOWN key
$VK_END 23 END key
$VK_HOME 24 HOME key
$VK_LEFT 25 LEFT ARROW key
$VK_UP 26 UP ARROW key
$VK_RIGHT 27 RIGHT ARROW key
$VK_DOWN 28 DOWN ARROW key
$VK_SELECT 29 SELECT key
$VK_PRINT 2A PRINT key
$VK_EXECUTE 2B EXECUTE key
$VK_SNAPSHOT 2C PRINT SCREEN key
$VK_INSERT 2D INS key
$VK_DELETE 2E DEL key
$VK_HELP 2F DEL key
$VK_0 - 9 30-39 0-9 keys
$VK_A - Z 41-5A A-Z keys
$VK_LWIN 5B Left Windows key
$VK_RWIN 5C Right Windows key
$VK_APPS 5D PopUp Menu Key - Applications key on a Microsoft Natural Keyboard
$VK_NUMPAD0 - 9 60-69 Numeric keypad 0 key
$VK_MULTIPLY 6A Multiply key
$VK_ADD 6B Add key
$VK_SEPARATOR 6C Separator key
$VK_SUBTRACT 6D Subtract key
$VK_DECIMAL 6E Decimal key
$VK_DIVIDE 6F Divide key
$VK_F1 - 12 70 F1-F12 key
$VK_F13 - 16 7C-7F F13 key - F16 key
$VK_F17 - 24 80H-87H F17 key - F24 key
$VK_NUMLOCK 90 NUM LOCK key
$VK_SCROLL 91 SCROLL LOCK key
$VK_LSHIFT A0 Left SHIFT key
$VK_RSHIFT A1 Right SHIFT key
$VK_LCONTROL A2 Left CONTROL key
$VK_RCONTROL A3 Right CONTROL key
$VK_LMENU A4 Left ALT key
$VK_RMENU A5 Right ALT key
$VK_OEM_1 BA ;
$VK_OEM_PLUS BB =
$VK_OEM_COMMA BC ,
$VK_OEM_MINUS BD -
$VK_OEM_PERIOD BE .
$VK_OEM_2 BF /
$VK_OEM_3 C0 `
$VK_OEM_4 DB [
$VK_OEM_5 DC \
$VK_OEM_6 DD ]

See Also

Search GetAsyncKeyState in MSDN Library.

Example

Example 1 : _Ispressed using $vkey as string

#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIvkeysConstants.au3>

Local $hDLL = DllOpen("user32.dll")

While 1
    If _IsPressed("10", $hDLL) Then
        ConsoleWrite("_IsPressed - Shift Key was pressed. @extended = " & @extended & @CRLF)
        ; Wait until key is released.
        While _IsPressed("10", $hDLL)
            Sleep(100)
        WEnd
        ConsoleWrite("_IsPressed - Shift Key was released. @extended = " & @extended & @CRLF)
    ElseIf _IsPressed("1B", $hDLL) Then
        ConsoleWrite("_IsPressed - Esc Key was pressed. @extended = " & @extended & @CRLF)
        MsgBox($MB_SYSTEMMODAL, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.", 3)
        ExitLoop
    EndIf
    Sleep(100)
WEnd

DllClose($hDLL)

Example 2 : _IsPressed using $vkey as array with modifier key checking

#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIvkeysConstants.au3>

Local $asModifierKeys[6] = [0, "VK_SHIFT", "VK_CONTROL", "VK_MENU", "VK_LWIN", "VK_RWIN"]

Local $aKeys[3] = [$VK_ESCAPE, $VK_LBUTTON, $VK_TAB]
While 1
    Local $iRet = _IsPressed($aKeys, Default, True) ; Check modifier
    If Not $iRet And @extended Then ConsoleWrite("The modifier key " & $asModifierKeys[@extended] & " has been pressed. @extended = " & @extended & @CRLF)

    Local $sKey
    Switch $iRet
        Case 1 ; Keyboard ESC
            $sKey = "{ESCAPE}"
            ExitLoop

        Case 2 ; MouseClick Left
            $sKey = "{LBUTTON}"
            ExitLoop

        Case 3 ; Keyboard Tab
            $sKey = "{TAB}"
            ExitLoop

    EndSwitch

    Sleep(100)
WEnd

_IsPressed() ; to DLLClose the the default Dll created by the previous _IsPressed_(...)

Send("^z") ; to avoid change in the Scite Window

ConsoleWrite("The key " & $sKey & " has been pressed." & @CRLF)
MsgBox($MB_SYSTEMMODAL, "Result", "The key " & $sKey & " has been pressed.", 2)

Example 3 : _Ispressed using $vkey as integer with modifier key checking

#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIvkeysConstants.au3>

Example()
Func Example()
    While 1
        If _IsPressed($VK_F2, Default, True) Then
            ConsoleWrite("_IsPressed - F2 (without any modifier keys pressed) was pressed." & @CRLF)
            ; Wait until key is released.
            While _IsPressed($VK_F2, Default, True)
                Sleep(100)
            WEnd
            ConsoleWrite("_IsPressed - F2 (without any modifier keys pressed) was released." & @CRLF)
        ElseIf _IsPressed($VK_ESCAPE) Then
            MsgBox($MB_SYSTEMMODAL, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.", 3)
            ExitLoop
        EndIf
        Sleep(100)
    WEnd

    _IsPressed() ; to release the internal handle created by previous call to _IsPressed(...)

EndFunc   ;==>Example