hotkeys does not work when send is in loop

半腔热情 提交于 2019-12-08 07:24:45

问题


Assume this code:

Loop
{
    if enabled
        Send, /
}

m::
    enabled := !enabled
Return

I want to toggle sending / to a Notepad for example. But if I run this code by pressing M on keyboard, then pressing the M key again does not disable sending.
Looks like the send command in the Loop cause this issue since Ive tried using msgbox which does not disable the m key.
How can I make this code to work? (SendInput and Play does not work too)


回答1:


It's because your loop is blocking any other execution. Unless that loop is the only thing in your script, you generally want to avoid using loops and use timers instead.

Timers don't block further execution but act more like their own thread. Here's an example using a timer:

slashTimerActive := 0

m::
    if (!slashTimerActive)
        SetTimer, SendSlash, 100 ; Call the sub every 100ms
    else
        SetTimer, SendSlash, Off

    slashTimerActive := !slashTimerActive ; Flip the variable
return

; Subroutine
SendSlash:
    SendInput, /
return


来源:https://stackoverflow.com/questions/30101897/hotkeys-does-not-work-when-send-is-in-loop

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