herausfinden, ob beliebige Taste gedrückt wurde (bzw. welche zuletzt gedrückt wurde)

  • Hallo zusammen,

    ich möchte herausfinden, ob jemand die Tastatur oder Maus bewegt hat. Bei der Maus werde ich dies wohl dadurch erreichen können, dass ich ständig die Anfangsposition mit mousgetpos vergleiche.
    Bei der Tastatur könnte man zwar dauernd jede Taste mit ispressed abfragen, aber ich glaube, das lastet den PC aus und ist auch einiges Arbeit.
    Irgendwo habe ich aber gelesen, dass man auch einfach die zuletzt gedrückte Taste sich ausgeben lassen kann. Die würde ich dann laufend mit der "Anfangs"-Taste vergleichen. Leider weiß ich nicht mehr den Befehl. Ich hoffe, da kann mir jemand weiterhelfen :)

    LG FKFK

    • Offizieller Beitrag

    Wenn es Dir egal ist, welche Taste gedrückt wurde:

    Spoiler anzeigen
    [autoit]


    #include <GUIConstantsEx.au3>
    #include <Timers.au3>

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

    $hGui = GUICreate('Test', 400, 300)
    GUICtrlCreateLabel('Maus bewegen oder Taste drücken', 10, 10, 350, 25)
    $hLabel = GUICtrlCreateLabel('', 40, 80, 350, 25)
    GUICtrlSetFont(-1, 12)
    GUISetState()
    While True
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    If _Timer_GetIdleTime() < 250 Then
    If GUICtrlRead($hLabel) = '' Then GUICtrlSetData($hLabel, 'Maus bewegt oder Taste gedrückt')
    Else
    If GUICtrlRead($hLabel) <> '' Then GUICtrlSetData($hLabel, '')
    EndIf
    WEnd

    [/autoit]
  • Da gibt es von Eukalyptus was feines bzgl. der Tastaturabfrage:

    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, @DesktopHeight - 100)
    $hLabel = GUICtrlCreateLabel("", 2, 2, 396, @DesktopHeight - 104)
    GUISetState()
    While GUIGetMsg() <> -3
    $aKeyboardState = _WinAPI_GetKeyboardState()
    $sKeys = "I" & @TAB & "CHR" & @TAB & "State" & @TAB & "Toogle" & @LF
    For $i = 10 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
    ; 0 Returns an array[256]
    ; 1 Returns 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]

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

  • UEZ: Wenn ich das richtig verstanden habe, überprüft die Funktion, welche Taste zuletzt gedrückt wurde und gibt diese zurück?


    Jain.

    Die Funktion gibt den aktuellen Tastenstatus aller 256 (virtuellen) Tasten zurück. Das müsste also die Liste sein, die in der _IsPressed()-Hilfe stehen.
    Du erhälst für jede Taste ein Arrayelement, eine Zahl, das aus 2 Bits besteht. Das sog. High-Bit gibt an, ob eine Taste gedrückt ist (0 / 1).
    Das Low-Bit gibt bei Tasten wie CAPSLOCK oder NUMPAD an, ob die Taste aktiviert ist (ob das LED leuchtet).

    High Bit und Low Bit fragst du so ab:

    [autoit]

    BitAND($aKeyboardState[$i], 0xF0) ; High
    BitAND($aKeyboardState[$i], 0x0F) ; Low

    [/autoit]