I found quick and simultaneous use of multiple hotkeys in the game, It can easily cause stuck and failure.
So I've come up with a good solution to share with you, hope you like it.
Code: Select all
/*
-------------------------------------------
Multiple hotkeys switch freely, no Stuck and Failure
Author: FeiYue
Usage: (Please refer to the following example)
1. Hotkey start only: GoWait( the label to run, the key for release )
The code begins with another label, not start directly with the hotkey
2. Insert per row: Sleep(MS)
If delay is required, use Sleep(MS), if not, also using Sleep(0)
-------------------------------------------
*/
;-- Hotkey start function, which can send instructions
;-- to the delay function and the background loop
GoWait(label="", key="") {
global RunLabel, DoExit
static MainLoopStart, WaitKey
Critical
if (RunLabel = label)
return
if (MainLoopStart="")
{
MainLoopStart:=1
SetTimer, MainLoop, -1, -1
}
RunLabel:=label, DoExit:=1, WaitKey:=key
SetTimer, WaitKeyUp, % key="" ? "Off" : 10
return
WaitKeyUp:
ListLines, Off
Critical
if !GetKeyState(WaitKey, "P")
{
RunLabel:="", DoExit:=1
SetTimer,, Off
}
return
;-- the background loop, accepting instructions, can start the new thread immediately
MainLoop:
ListLines, Off
Loop {
if IsLabel(RunLabel)
{
DoExit:=0
SetTimer, %RunLabel%, -1
}
Sleep, 10
}
return
}
;-- delay function, accepts the instruction and terminates the thread immediately
Sleep( MS=0 ) {
global DoExit
ListLines, Off
IfEqual, DoExit, 1, Exit
if MS>0
{
endt := A_TickCount + MS
While (A_TickCount < endt)
{
Sleep, 10
IfEqual, DoExit, 1, Exit
}
}
ListLines, On
}
;==== The above function add to your own script, the following is not required ====
;-- press Esc to stop other hotkeys from opening the loop
$Esc::GoWait()
;-- hold down the hotkey 1 loop, send ABC, and release stop
$1::GoWait("aaa1","1")
aaa1:
Loop {
Sleep( 0 )
Send a
Sleep( 100 )
Send b
Sleep( 100 )
Send c
Sleep( 100 )
}
return
;-- hold down the hotkey 2 loop, send XYZ, and release stop
$2::GoWait("aaa2","2")
aaa2:
Loop {
Sleep( 0 )
Send x
Sleep( 100 )
Send y
Sleep( 100 )
Send z
Sleep( 100 )
}
return
;-- Press the hotkey 3 cycle to send 789, and then press again to stop
$3:: (RunLabel!="aaa3") ? GoWait("aaa3") : GoWait()
aaa3:
Loop {
Sleep( 0 )
Send 7
Sleep( 100 )
Send 8
Sleep( 100 )
Send 9
Sleep( 100 )
}
return
;