Toggle a key with hotkey in autohotkey

19,441

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

Solution 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
Share:
19,441

Related videos on Youtube

András Geiszl
Author by

András Geiszl

I'm a self-taught programmer. My favorite language is C# (because of Visual Studio of course), but I also like to program in JavaScript (with JQuery and Ajax) and Python. Languages that I once programmed in are PHP, Java, Jade, Stylus, Assembly. I especially hate PHP and Java (just because of Eclipse).

Updated on September 16, 2022

Comments

  • András Geiszl
    András Geiszl over 1 year

    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?

  • András Geiszl
    András Geiszl over 10 years
    It doesn't work. The problem is the same: It starts holding the "w", but it doesn't release it.
  • Isti115
    Isti115 over 10 years
    It is not bad, but it has a loop in it which could be avoided! Also it is a bit overcomplicated. :)
  • Programmer Paul
    Programmer Paul over 10 years
    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.
  • Isti115
    Isti115 over 10 years
    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