wie kann ich gucken wie lange eine taste gedrückt wurde?

  • da brauchst du den befehl _Ispressed :thumbup:
    das ganze dann zb in einer while schleife sieht dann ungefähr so aus:

    [autoit]

    $counter = 0
    While _Ispressed("41") ;41 ist zB A (siehe Liste unten)
    Sleep(1000)
    $counter = $counter + 1
    WEnd
    MsgBox(0,"","Die Taste wurde "&$counter&"Sekunden gedrückt.")

    [/autoit]


    hau rein team2way

    Einmal editiert, zuletzt von team2way (7. April 2009 um 12:31)

  • Mahlzeit,
    Wenn ich auf den Thread verweisen darf: _Ispressed
    meine Lösung ist diese hier (Credits an alle aus dem Thread):
    Du brauchst die Keys.ini datei mit folgendem inhalt:

    Spoiler anzeigen

    [keys]
    01=Left mouse button
    02=Right mouse button
    04=Middle mouse button (three-button mouse)
    05=Windows 2000/XP: X1 mouse button
    06=Windows 2000/XP: X2 mouse button
    08=BACKSPACE key
    09=TAB key
    0C=CLEAR key
    0D=ENTER key
    10=SHIFT key
    11=CTRL key
    12=ALT key
    13=PAUSE key
    14=CAPS LOCK key
    1B=ESC key
    20=SPACEBAR
    21=PAGE UP key
    22=PAGE DOWN key
    23=END key
    24=HOME key
    25=LEFT ARROW key
    26=UP ARROW key
    27=RIGHT ARROW key
    28=DOWN ARROW key
    29=SELECT key
    2A=PRINT key
    2B=EXECUTE key
    2C=PRINT SCREEN key
    2D=INS key
    2E=DEL key
    30=0 key
    31=1 key
    32=2 key
    33=3 key
    34=4 key
    35=5 key
    36=6 key
    37=7 key
    38=8 key
    39=9 key
    41=A key
    42=B key
    43=C key
    44=D key
    45=E key
    46=F key
    47=G key
    48=H key
    49=I key
    4A=J key
    4B=K key
    4C=L key
    4D=M key
    4E=N key
    4F=O key
    50=P key
    51=Q key
    52=R key
    53=S key
    54=T key
    55=U key
    56=V key
    57=W key
    58=X key
    59=Y key
    5A=Z key
    5B=Left Windows key
    5C=Right Windows key
    60=Numeric keypad 0 key
    61=Numeric keypad 1 key
    62=Numeric keypad 2 key
    63=Numeric keypad 3 key
    64=Numeric keypad 4 key
    65=Numeric keypad 5 key
    66=Numeric keypad 6 key
    67=Numeric keypad 7 key
    68=Numeric keypad 8 key
    69=Numeric keypad 9 key
    6A=Multiply key
    6B=Add key
    6C=Separator key
    6D=Subtract key
    6E=Decimal key
    6F=Divide key
    70=F1 key
    71=F2 key
    72=F3 key
    73=F4 key
    74=F5 key
    75=F6 key
    76=F7 key
    77=F8 key
    78=F9 key
    79=F10 key
    7A=F11 key
    7B=F12 key
    7C=F13 key
    7D=F14 key
    7E=F15 key
    7F=F16 key
    80H=F17 key
    81H=F18 key
    82H=F19 key
    83H=F20 key
    84H=F21 key
    85H=F22 key
    86H=F23 key
    87H=F24 key
    90=NUM LOCK key
    91=SCROLL LOCK key
    A0=Left SHIFT key
    A1=Right SHIFT key
    A2=Left CONTROL key
    A3=Right CONTROL key
    A4=Left MENU key
    A5=Right MENU key
    BA=;
    BB==
    BC=,
    BD=-
    BE=.
    BF=/
    C0=`
    DB=[
    DC=\
    DD=]


    und das hier ist mein Code:

    Spoiler anzeigen
    [autoit]

    #include <Array.au3>
    #include <misc.au3>
    #include <Timers.au3>

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

    $dll = DllOpen("user32.dll")
    $keys = IniReadSection("keys.ini", "keys")

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

    Local $starttime
    While 1
    For $x = 1 To $keys[0][0]
    If _IsPressed($keys[$x][0], $dll) Then
    $key = $keys[$x][1]
    $starttime = _Timer_Init()
    While _IsPressed($keys[$x][0], $dll)
    Sleep(1)
    WEnd
    ToolTip($key & " was pressed" & @CRLF & Round(_Timer_Diff($starttime) / 1000, 2) & " seconds")
    Beep(500, 100)
    EndIf
    Next
    Sleep(1)
    WEnd

    [/autoit]

    Wer immer nur das tut, was er bereits kann - wird auch immer nur das bleiben, was er bereits ist!

  • Ich würde es so machen, wobei ich die gezählten sektunden real eher am Ende der Schleife in eine TxT-Datei schreiben würde wenn ich sie weiter nutzen möchte.

    Spoiler anzeigen
    [autoit]


    #include <Misc.au3>
    $dll = DllOpen("user32.dll")
    $counter = 0
    While 1

    Sleep(999)
    If _IsPressed("41", $dll) Then
    $counter = $counter + 1
    Else
    ExitLoop
    EndIf
    WEnd
    DllClose($dll)
    MsgBox(0,"","Die Taste wurde "&$counter&" Sekunden gedrückt.")

    [/autoit]

    Lieben Gruß,
    Alina

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Geheime Information: ;)
    OuBVU5ebLhHu5QvlnAyQB4A7SzBrvWulwL7RLl2BdH5tI6sIYspeMKeXMSXl

  • Hi

    Bei _IsPressed wird für jede Taste ein DllCall ausgeführt...

    Ressourcenschonender ist diese Lösung:

    Spoiler anzeigen
    [autoit]


    Global $aKeyState = _WinAPI_GetKeyboardState()
    Global $aKeyTimer[256][2]
    For $i = 0 To 255
    $aKeyTimer[$i][0] = $aKeyState[$i]
    Next

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

    Global $hGui = GUICreate("TestKeyTimer", 400, 50)
    Global $hLabel = GUICtrlCreateLabel("", 2, 2, 396, 46)
    GUISetState()

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

    While GUIGetMsg() <> -3
    $aKeyState = _WinAPI_GetKeyboardState()
    For $i = 0 To 255
    If $aKeyState[$i] < 0 And $aKeyState[$i] <> $aKeyTimer[$i][0] Then
    $aKeyTimer[$i][0] = $aKeyState[$i]
    $aKeyTimer[$i][1] = TimerInit()
    ElseIf $aKeyState[$i] <> $aKeyTimer[$i][0] Then
    $aKeyTimer[$i][0] = $aKeyState[$i]
    GUICtrlSetData($hLabel, "Taste Nr.: " & $i & " ( CHR: " & Chr($i) & " ) war " & Round(TimerDiff($aKeyTimer[$i][1]), 2) & " ms lang gedrückt")
    EndIf
    Next
    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]

    Allerdings bekommt man damit nur Resultate, wenn man den Fokus im GUI hat!
    (Man kann nicht prüfen, welche Taste z.b. im Notepad gedrückt wurde - und das ist auch gut so ;))

    lgE

    • danke funzt das eigentlich auch wenn man 2 tasten gleichzeitig drückt?


    naja ich hab noch ein prob

    [autoit]


    #include <Misc.au3>
    $dll = DllOpen("user32.dll")
    $counter = 0
    while 1
    If _IsPressed("41") then
    While 1

    Sleep(1)
    If _IsPressed("41", $dll) Then
    $counter = $counter + 1
    Else
    ExitLoop
    EndIf
    WEnd
    DllClose($dll)
    $open = fileopen(@appdatadir&"\levelR.txt",1)
    filewriteline($open,"a =" & $counter)
    fileclose($open)
    DllClose($dll)
    endif
    wend

    [/autoit]

    ich will halt mit meinem kumpel gucken wer schneller tippen kann und
    das das dann so ungefähr aufgelistet werden würde
    zb so a = 30 also in tausendstel
    aber bei mir wiederholt der das die ganze zeit was kann ich tun?

    Einmal editiert, zuletzt von Sithlord95 (7. April 2009 um 13:35)

  • Hi

    Bei mir gehts nur bis zu 4 Tasten gleichzeitig

    Spoiler anzeigen
    [autoit]


    Global $sLabel=""

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

    Global $aKeyState = _WinAPI_GetKeyboardState()
    Global $aKeyTimer[256][2]
    For $i = 0 To 255
    $aKeyTimer[$i][0] = $aKeyState[$i]
    Next

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

    Global $hGui = GUICreate("TestKeyTimer", 400, 400)
    Global $hLabel = GUICtrlCreateLabel("", 2, 2, 396, 396)
    GUISetState()

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

    While GUIGetMsg() <> -3
    $aKeyState = _WinAPI_GetKeyboardState()
    For $i = 0 To 255
    If $aKeyState[$i] < 0 And $aKeyState[$i] <> $aKeyTimer[$i][0] Then
    $aKeyTimer[$i][0] = $aKeyState[$i]
    $aKeyTimer[$i][1] = TimerInit()
    ElseIf $aKeyState[$i] <> $aKeyTimer[$i][0] Then
    $aKeyTimer[$i][0] = $aKeyState[$i]
    $sLabel&= "Taste Nr.: " & $i & " ( CHR: " & Chr($i) & " ) war " & Round(TimerDiff($aKeyTimer[$i][1]), 2) & " ms lang gedrückt" & @LF
    GUICtrlSetData($hLabel,$sLabel)
    EndIf
    Next
    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]
  • bezieht sich das auch auf mein anderes problem oder nur auf das problem mit den gleichzeitigen tasten?

    Ja, also bei mir geht das auch mit drei Tasten. Vier? Dazu habe ich nicht die richtige Tastatur hier oder zu kleine Finger ;)

    Lieben Gruß,
    Alina

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Geheime Information: ;)
    OuBVU5ebLhHu5QvlnAyQB4A7SzBrvWulwL7RLl2BdH5tI6sIYspeMKeXMSXl