Press any key (GetKeyboardState)

  • Es gibt zwar die IsPressed -UDF mit einer Anykey -Funktion, die allerdings für jede Taste einen eigenen DLLCall ausführt

    Hier 3 Beispiele, wie man die GetKeyboardState-Funktion nutzen könnte:

    Spoiler anzeigen
    [autoit]

    $hGui = GUICreate("Example 1 - Press any key to exit...", 400, 100)
    GUISetState()
    $sKeyboardState = _WinAPI_GetKeyboardState(1)
    While _WinAPI_GetKeyboardState(1) = $sKeyboardState
    Sleep(100)
    WEnd
    GUIDelete($hGui)

    [/autoit] [autoit][/autoit] [autoit]

    Sleep(500)

    [/autoit] [autoit][/autoit] [autoit]

    $hGui = GUICreate('Example 2 - Press "A" to exit...', 400, 200)
    GUISetState()
    $aKeyboardStateOld = _WinAPI_GetKeyboardState()
    While 1
    $aKeyboardState = _WinAPI_GetKeyboardState()
    If $aKeyboardState[65] <> $aKeyboardStateOld[65] Then ExitLoop
    Sleep(100)
    WEnd
    GUIDelete($hGui)

    [/autoit] [autoit][/autoit] [autoit]

    Sleep(500)

    [/autoit] [autoit][/autoit] [autoit]

    $hGui = GUICreate('Example 3 - Type...', 400, 400)
    $hLabel = GUICtrlCreateLabel("", 2, 2, 396, 396)
    GUISetState()
    While GUIGetMsg() <> -3
    $aKeyboardState = _WinAPI_GetKeyboardState()
    $sKeys = "I" & @TAB & "CHR" & @TAB & "State" & @TAB & "Toogle" & @LF
    For $i = 65 To 90
    $sKeys &= $i & @TAB & Chr($i) & @TAB & BitAND($aKeyboardState[$i], 0xF0) & @TAB & BitAND($aKeyboardState[$i], 0x0F) & @LF
    Next
    GUICtrlSetData($hLabel, $sKeys)
    Sleep(100)
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _WinAPI_GetKeyboardState
    ; Description ...: Returns the status of the 256 virtual keys
    ; Syntax.........: _WinAPI_GetKeyboardState($iFlag=0)
    ; Parameters ....: $iFlag - Return Type
    ; 0Returns an array[256]
    ;1Returns a string
    ; Return values .: Success - Array[256] or String containing status of 256 virtual keys
    ; Failure - False
    ; Author ........: Eukalyptus
    ; Modified.......:
    ; Remarks .......: If the high-order bit is 1, the key is down; otherwise, it is up.
    ;If the key is a toggle key, for example CAPS LOCK, then the low-order bit is 1
    ;when the key is toggled and is 0 if the key is untoggled
    ; Related .......: _IsPressed
    ; Link ..........;
    ; Example .......;
    ; ===============================================================================================================================
    Func _WinAPI_GetKeyboardState($iFlag = 0)
    Local $aDllRet, $lpKeyState = DllStructCreate("byte[256]")
    $aDllRet = DllCall("User32.dll", "int", "GetKeyboardState", "ptr", DllStructGetPtr($lpKeyState))
    If @error Then Return SetError(@error, 0, 0)
    If $aDllRet[0] = 0 Then
    Return SetError(1, 0, 0)
    Else
    Switch $iFlag
    Case 0
    Local $aReturn[256]
    For $i = 1 To 256
    $aReturn[$i - 1] = DllStructGetData($lpKeyState, 1, $i)
    Next
    Return $aReturn
    Case Else
    Return DllStructGetData($lpKeyState, 1)
    EndSwitch
    EndIf
    EndFunc ;==>_WinAPI_GetKeyboardState

    [/autoit]

    lgE

  • Tolle Funktion :thumbup:

    Würde aber das dritte Fenster so amchen, damit es nicht immer so flackert:

    Spoiler anzeigen
    [autoit]

    Sleep(500)

    [/autoit] [autoit][/autoit] [autoit]

    $hGui = GUICreate('Example 3 - Type...', 400, 400)
    $hLabel = GUICtrlCreateLabel("", 2, 2, 396, 396)
    $oldKeys=""
    GUISetState()
    While GUIGetMsg() <> -3
    $aKeyboardState = _WinAPI_GetKeyboardState()
    $sKeys = "I" & @TAB & "CHR" & @TAB & "State" & @TAB & "Toogle" & @LF
    For $i = 65 To 90
    $sKeys &= $i & @TAB & Chr($i) & @TAB & BitAND($aKeyboardState[$i], 0xF0) & @TAB & BitAND($aKeyboardState[$i], 0x0F) & @LF
    Next
    if $oldKeys <> $sKeys then
    GUICtrlSetData($hLabel, $sKeys)
    $oldKeys = $sKeys
    endif
    Sleep(100)
    WEnd

    [/autoit]
  • tolle funktion!

    ich hab die auch mal bei msdn gesehen, aber das mit denn dll-calls raff ich einfach nicht.

    vielen dank :thumbup:

    Canyon