Global keyboard function in Autohotkey?

醉酒当歌 提交于 2019-12-10 11:54:12

问题


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

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