Toggle a key with hotkey in autohotkey

2019-04-12 02:05发布

问题:

So I tried to automate running in a game, where the map is huge, and I have to run miles. I wanted to toggle on the hotkey (Ctrl+Shift+A or something else) press the running (in the game, I can run with w). I tried code, like:

Pause On
Loop
Send w
+^a::Pause

(it can press the w, but it can't release) and like this:

+^a::
toggle := !toggle

while toggle
    Send {w down}

(same problem). It's just my problem, or these codes are wrong?

回答1:

This is my stock function. I usualy map it to ^W or Q. Pressing w or s will cancel it. Easy peasy.

HoldW(){
    SendInput {w up}{w down}
    Loop
    {
        Sleep 100
        GetKeyState state, w
        if state = u
            return
        If GetKeyState("s")
        {
            SendInput {w up}
            return
        }
    }
}


回答2:

I have a (at least i think) much simpler solution :)

#NoTrayIcon

ScrollLock::

    Input, Key, ,{Enter}

    Send, {%Key% Down}

return

You press ScrollLock (which I doubt you use for anything else, otherwise set it to a free key), and then enter the name of button to be held down.

  • If you want to hold down a single character, you just write it in.
  • For other keys you can find the names here: http://www.autohotkey.com/docs/KeyList.htm
  • Mouse: LButton for left, RButton for right and MButton for middle

You end the input with the Enter key, and after that the program will hold down the entered key.

If you want to "lift up" the key, just simply press it once, and it will be held down no more. :)

ps.:I have #NoTrayIcon, because I'm running it permanently in the background, but if you wanted to be able to exit then simply add something like this:

F12::
    ExitApp
return


回答3:

+^vk41:: ; shift+ctrl+a
   SetTimer, % "SomeLable", % (bToggle:=!bToggle) ? 25:"Off"
   KeyWait, % "vk41"
   Return

SomeLable:
   SendInput, % "{vk57}" ; w
   Return


回答4:

A silly noob example where F10 is the toggle hotkey, and the up/down state is a variable. The variable needs to be pre-declared to give the initial value.

To be honest I expected an error message, but it seemed to run fine.

keystate=down

F10::
Send {w %keystate%}
if keystate = down
SetEnv, keystate, up
else if keystate = up
SetEnv, keystate, down
return


回答5:

Toggle      := 1
Q::Send, % Toggle = 1 ? ( "0", Toggle := 0 ) : ( "9", Toggle := 1 )

Change Q to your preferred hotkey, and change "0" and "9" to the keys you want to toggle through. Make sure to set your abilities or weapons to the keys you replace in "0" and "9".

So, lets say I have a primary and secondary weapon. I bind them in game to 9 and 0.

I press Q to cycle between them for fast weapon switching. Or w/e else you want.