使用 AutoHotkey 记录多个密钥 [英] Log multiple Keys with AutoHotkey
问题描述
我正在尝试通过 AutoHotkey 玩赛车游戏.我使用这个脚本将我按下的键粘贴到一个 txt 中,让它们自动播放.
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.
我现在的问题是这个脚本只能记录一个键,例如 {w down} 用于前进.但是要在游戏中更好地驾驶,您必须按 w 和 d 向右行驶.
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
推荐答案
我建议使用 OnMessage()
来监视按下 (0x100) 和按下 (0x101) 事件消息.以下是相关帮助文档的一些链接:OnMessage()、
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.
下面是一个工作示例,它将在您的桌面testEE.txt
上创建一个文件,其参数与您正在记录的内容类似.请注意,这不会记录鼠标移动或点击(有可能,我只是没有那样设置).按 F1
开始和停止录制.
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 )
}
这将重播您的按键:
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
注意它也记录了开始和停止记录的热键,所以如果它看到这个键,我就让它Continue
.如果您将热键更改为某个其他键或键组合,则需要将其设置为忽略该键或键组合.
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.
我发现这些帖子特别有用,供参考:
https://autohotkey.com/board/topic/27067-mouse-move-检测/
https://www.autohotkey.com/boards/viewtopic.php?t=11922
如何每次激活功能目标窗口在 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
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
这篇关于使用 AutoHotkey 记录多个密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!