Getting a key to hold down when pressed twice?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tgoVIPER
Posts: 14
Joined: 22 Apr 2018, 10:28

Getting a key to hold down when pressed twice?

22 Apr 2018, 10:39

So I read the forums/help section and tried to figure it out on my own before I came here.

What I'm trying to do is hold a key down when it's pressed twice.
In my specific example, what I'd like to do is:
When pressing the 'w' key twice, I'd like it to hold down 'w'. Pressing 'w' again would release the key.
Pressing 'w' once (and releasing), or pressing and holding 'w' once would act normally.

I'm too embarrassed to post my feeble attempt at coding this.
Please help.

Thanks in advance,
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Getting a key to hold down when pressed twice?

22 Apr 2018, 11:04

The built-in variables you want to use are A_PriorKey and A_TimeSincePriorHotkey. You also want the $ modifier to prevent recursion. Create an If statement that compares PriorKey to being the same as your target key, and also that it was pressed twice in quick enough succession by using an inequality for some threshold of milliseconds. This would be used to check if you should use Send {w down}.

I would personally also set a flag (a custom variable of any name that is set to true or false) for checking if it is time to release the key when you next press the hotkey. Something like flag:=true, this line should be paired with the Send {w down} inside of a block.

You can then follow this up with an else statement, in this case I would use else if (flag=true) (or shorthanded as just else if flag which asks if flag has any "true" value). This is where you'd use another block to do Send {w up} and flag:=false.

(true and false are built in variables that basically represent 1 and 0 for boolean logic).

End it all with a return.

You may find Send {w down} doesn't do what you want. You may need to get it to repeat Send w instead. This can be achieved in a couple of ways, the easiest would be turning on a timer with SetTimer. Examples can be found in (Note: The first link is from the old forums, the second link is on these forums) http://www.autohotkey.com/board/topic/6 ... re-thread/ or https://autohotkey.com/boards/viewtopic.php?f=7&t=11952
tgoVIPER
Posts: 14
Joined: 22 Apr 2018, 10:28

Re: Getting a key to hold down when pressed twice?

22 Apr 2018, 12:01

Thanks, Exaskryz! I appreciate you taking the time to get into the nuts and bolts of it.

I'm admittingly being lazy and looking for someone to make a script for me. I do enjoy coding but with work/family I don't have too much time to spend on the weekend learning new stuff. We're all busy with our own stuff, I know.

Again, thank you.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Google [Bot], jomaweb, Rohwedder and 266 guests