问题
This script "auto-presses" E while holding it down.
$e::
While GetKeyState("e","P")
{
Random, r, 50, 250
sleep r
Send e
}
return
Is there a way to globally call this function for any key?
For example: Holding A, will auto-press A and Z will auto-press Z, etc.
Without manually assigning every possible key on the code.
回答1:
I am not aware of a way to catch all keys. But you can simplify your code by combining hotkeys this way:
$a::
$b::
$c::
; and so on...
$z::
RandomSendCurrentKey() ; all the above hotkeys will call RandomSendCurrentKey()
return ; 'return' is needed to prevent further execution
RandomSendCurrentKey() {
local key
StringReplace, key, A_ThisHotkey, $,, All ; removes '$' from key
While GetKeyState(key, "P")
{
Random, r, 50, 250
sleep r
Send %key%
}
}
But why do you need a while
loop? It should work without a while
too since AutoHotKey will keep calling the function as long as you press the key:
$a::
$b::
$c::
$z::
RandomSendCurrentKey()
return
RandomSendCurrentKey() {
local key
StringReplace, key, A_ThisHotkey, $,, All ; removes '$' from key
Random, r, 50, 250
sleep r
Send %key%
}
来源:https://stackoverflow.com/questions/28654733/global-keyboard-function-in-autohotkey