问题
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