András Geiszl
András Geiszl

Reputation: 1074

Toggle a key with hotkey in autohotkey

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?

Upvotes: 5

Views: 25462

Answers (6)

trapper_hag
trapper_hag

Reputation: 790

Suspend 1 ; Start suspended so that only ScrollLock is listened for

#SuspendExempt ; Start exempting from suspension

ScrollLock::
{
  if(A_IsSuspended) ; If the script is suspended, start cruising
  {
    Suspend 0 ; Start listening for W and S
    Send "{W Down}" ; Hold down W
  }
  else ; If we're already cruising, stop
  {
    stopCruising()
  }
}

#SuspendExempt False ; Stop exempting from suspension

stopCruising()
{
  Send "{W}" ; Press W once to stop holding down W
  Suspend 1 ; Stop listening for W and S
}

; Press W or S to stop cruising
w::stopCruising()
s::stopCruising()

This is what I ended up with in AutoHotkey V2. Press ScrollLock to start cruise control and then W, S or ScrollLock to stop it.

Upvotes: 0

user8499005
user8499005

Reputation: 1

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.

Upvotes: 0

user3553576
user3553576

Reputation: 1

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

Upvotes: 0

Isti115
Isti115

Reputation: 2756

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

Upvotes: 2

Programmer Paul
Programmer Paul

Reputation: 508

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
        }
    }
}

Upvotes: 0

Grey
Grey

Reputation: 339

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

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

Upvotes: 0

Related Questions