5

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?

vasili111
  • 6,032
  • 10
  • 50
  • 80
András Geiszl
  • 966
  • 2
  • 16
  • 36
  • Yes the code is wrong, you might want to learn from here: http://www.autohotkey.com/docs/Tutorial.htm. Also when used in a programming context the plural of code is `code`, not `codes`. – this Oct 08 '13 at 10:31

6 Answers6

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
Isti115
  • 2,418
  • 3
  • 29
  • 35
0
+^vk41:: ; shift+ctrl+a
   SetTimer, % "SomeLable", % (bToggle:=!bToggle) ? 25:"Off"
   KeyWait, % "vk41"
   Return

SomeLable:
   SendInput, % "{vk57}" ; w
   Return
Grey
  • 339
  • 1
  • 4
0

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
        }
    }
}
Programmer Paul
  • 488
  • 1
  • 3
  • 13
  • It is not bad, but it has a loop in it which could be avoided! Also it is a bit overcomplicated. :) – Isti115 Oct 24 '13 at 14:23
  • Hi Isti115, I agree that it seems overly complicated. I found that nothing short of this got me the exact nuanced behavior that I was after (mainly how it turns off.) Fortunately, I've not had a problem with performance and it works with every game I try so I just have to map a hotkey to the function. That said, I've always struggled with GetKeystate a bit, so I'm sure there is room for improvement while retaining the behavior. – Programmer Paul Oct 25 '13 at 17:39
  • I used GetKeyState earlier too, but somehow by accident I discovered that if a key held down by autohotkey gets pressed again and released, autohotkey will no longer attempt to hold it down. It was a very lucky discovery, because I no longer had to write the release part! :D – Isti115 Nov 01 '13 at 21:57
0

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
0
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.

0
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.

trapper_hag
  • 780
  • 9
  • 14