Log multiple Keys with AutoHotkey

喜夏-厌秋 提交于 2019-12-22 18:19:41

问题


I´m trying to let a race game play via AutoHotkey. I use this script to paste the keys i pressed into a txt to let them automaticly play back.

My problem is now that this script only can log one key, for example {w down} for drive forward. But to drive in the game better you must press w and d to drive to the right.

How can I change the code to make it work the way I want?

$F1::
start := A_TickCount
FileDelete, log.txt

Loop
{
Input, key, L1 V, %endkeyslist%
IfInString, ErrorLevel, EndKey:
{
StringTrimleft, key, ErrorLevel, 7
}
time := (A_TickCount - start)
start := A_TickCount
text1:="Sleep, " time "`n"
text2:="Send {" key " Down} `n"
FileAppend, %text1%, log.txt
FileAppend, %text2%, log.txt

KeyWait, %key% ;waits the key to be released
time := (A_TickCount - start)
start := A_TickCount
text1:="Sleep, " time "`n"
text2:="Send {" key " Up} `n"
FileAppend, %text1%, log.txt
FileAppend, %text2%, log.txt
}
return

$F2:: ExitApp

回答1:


I would recommend using OnMessage() to monitor both key-down (0x100) and key-up (0x101) event messages. Here are some links to the pertinent help documentation: OnMessage(), List of Windows Messages.

Below is a working example that will make a file on your desktop testEE.txt with similar parameters to what you're logging. Note that this will not log mouse movements or clicks (it is possible, I just didn't set it up that way). Press F1 to start and stop recording.

f1::
bT := !bT
If bT
{
    MsgBox ,,, Recording started , 1
    KeyBdHook := DllCall(   "SetWindowsHookEx" , "int" , 13  , "uint" , RegisterCallback( "KeyBdProc" ) , "uint" , 0 , "uint" , 0 )
    nStart := A_TickCount
} Else {
    DllCall( "UnhookWindowsHookEx" , "ptr" , KeyBdHook )
    MsgBox ,,, Recording terminated , 1
}
Return

KeyBdProc( nCode , wParam , lParam )
{
    global
    Critical
    If ( wParam = 0x100 || wParam = 0x101 )
    {
        nKey := Format( "{:x}" , NumGet( lParam + 0 , 0 , "int" ) )
        nTime := A_TickCount - nStart , nStart := A_TickCount
        If ( wParam = 0x100 && nKey != nKeyPrev )
        {
            nKeyPrev := nKey
            FileAppend , % nTime . "|" . nKey . " down`n" , %A_Desktop%/testEE.txt
        }
        If ( wParam = 0x101 )
        {
            If ( nKey = nKeyPrev )
                nKeyPrev := ""
            FileAppend , % nTime . "|" . nKey . " up`n" , %A_Desktop%/testEE.txt
    }   }
    Return DllCall( "CallNextHookEx" , "uint" , KeyBdHook , "int" , nCode , "uint" , wParam , "uint" , lParam )
}

This will replay your keypresses:

f2::
aData := []
FileRead , sText , %A_Desktop%/testEE.txt
Loop , Parse , sText , `n , `r
{
    nCt := A_Index
    Loop , Parse , A_LoopField , |
        aData[ nCt , A_Index ] := A_LoopField
}
Loop , % aData.Length()
{
    If InStr( aData[ A_Index , 2 ] , "70 " ) ; 0x70 is F1
        Continue
    Sleep , % aData[ A_Index , 1 ]
    Send , % "{vk" . aData[ A_Index , 2 ] . "}"
}
Return

Note that it also records the hotkey that starts and stops recording, so I made it Continue if it sees this key. If you change the hotkey to some other key or key-combination, you'll need to set it to ignore that key or key-combination instead.

I found these posts especially helpful, for reference:
https://autohotkey.com/board/topic/27067-mouse-move-detection/
https://www.autohotkey.com/boards/viewtopic.php?t=11922
How to activate a function everytime the target window becomes active in AutoHotkey
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwindowshookexa
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-unhookwindowshookex



来源:https://stackoverflow.com/questions/55578584/log-multiple-keys-with-autohotkey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!