Help with Double tap and hold script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Strkzs
Posts: 3
Joined: 19 Sep 2017, 06:24

Help with Double tap and hold script

19 Sep 2017, 06:41

Hey guys.

I am a newbie, and couldn't gather enough information to make it work. My desire is to make a double key + hold action, so that combination would also hold another key, until i release it.

This is what i have accomplished so far, that works only just the half way:

~d::
KeyWait, d
KeyWait, d, D T.3
If (!ErrorLevel)
Send {e down}
While GetKeyState(d, "p")
Sleep 10
Send {e up}
Return


The idea is to double tap D, and hold it, so it will trigger E and hold it as well. Release both when released.

Thank you.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Help with Double tap and hold script

19 Sep 2017, 10:04

I think what you're missing is a block around the commands after the If statement.

Code: Select all

If (!ErrorLevel)
{
Send {e down}
While GetKeyState(d, "p")
Sleep 10
Send {e up}
}
Return
(I would use a second KeyWait, d instead of that While loop myself, but either should work.)

Without the block, only the Send {e down} is affected -- just like how the While loop only repeats the Sleep 10 command. And while you didn't provide a description of what doesn't work, my guess is your program is responding to the artificial Send {e up}.

If there is still something wrong, please let us know what isn't working -- whether it's not doing what you expect or doing something unexpected.
Strkzs
Posts: 3
Joined: 19 Sep 2017, 06:24

Re: Help with Double tap and hold script

19 Sep 2017, 11:02

Exaskryz wrote:I think what you're missing is a block around the commands after the If statement.

Code: Select all

If (!ErrorLevel)
{
Send {e down}
While GetKeyState(d, "p")
Sleep 10
Send {e up}
}
Return
(I would use a second KeyWait, d instead of that While loop myself, but either should work.)

Without the block, only the Send {e down} is affected -- just like how the While loop only repeats the Sleep 10 command. And while you didn't provide a description of what doesn't work, my guess is your program is responding to the artificial Send {e up}.

If there is still something wrong, please let us know what isn't working -- whether it's not doing what you expect or doing something unexpected.
Well, it's probably working fine. It's just missing some part, that my lack of knowledge doesn't let me to add. Or maybe it's completely different from what I want.

So once again, I want fast double-tap (with holding the key afterwards) to execute repeating of the two buttons, until I release. I want to press twice the "D" button, hold it, and get "dedede..." typed, as long as i hold the button. What I get now, is: "dedddd...." the "E" button getting typed one time, and ignored afterwards. It really feels to be missing something, or has to be written other way.

Thank you for the help, really appreciate that.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Help with Double tap and hold script

19 Sep 2017, 14:42

Ooooooh, yeah, OK. What you need is to repeat Send de.

I also noticed I made a mistake with the GetKeyState in my code above, the "d" needs quotation marks too.

Code: Select all

If (!ErrorLevel)
While GetKeyState("d","p")
Send de ; or ed because you might start off with two d's in a row with de, from the second press and hold d counting as a sent character
return
(Brackets unnecessary because each conditional execution involves only the line right below it.

However, one last change you need to make is the hotkey probably needs the $ modifier. This prevents the Send d part from activating your hotkey again. So use a $d:: hotkey. And of course, have your KeyWaits like you originally had.

---

The reason the AHK hold down doesn't work is because your Keyboard Driver is responsible for deciding that a hold down really means you want the key repeated which is a process of Key Down -> Key Up -> Key Down -> Key Up. AHK doesn't invoke the keyboard driver with its Send command, thus the driver doesn't create its own artificial up and down strokes while AHK puts the key into a down state.
Strkzs
Posts: 3
Joined: 19 Sep 2017, 06:24

Re: Help with Double tap and hold script

19 Sep 2017, 22:14

Exaskryz wrote:Ooooooh, yeah, OK. What you need is to repeat Send de.

I also noticed I made a mistake with the GetKeyState in my code above, the "d" needs quotation marks too.

Code: Select all

If (!ErrorLevel)
While GetKeyState("d","p")
Send de ; or ed because you might start off with two d's in a row with de, from the second press and hold d counting as a sent character
return
(Brackets unnecessary because each conditional execution involves only the line right below it.

However, one last change you need to make is the hotkey probably needs the $ modifier. This prevents the Send d part from activating your hotkey again. So use a $d:: hotkey. And of course, have your KeyWaits like you originally had.

---

The reason the AHK hold down doesn't work is because your Keyboard Driver is responsible for deciding that a hold down really means you want the key repeated which is a process of Key Down -> Key Up -> Key Down -> Key Up. AHK doesn't invoke the keyboard driver with its Send command, thus the driver doesn't create its own artificial up and down strokes while AHK puts the key into a down state.
Wow, thank you very much! It does work indeed!

However, it send repeats too fast, is there a way to slow that down? Also, is it possible to make timeout lower than 1 second? I was trying 0,5 for example, but it didn't work, seems like everything lower than 1 sec doesn't work.

Though, I can work with that anyway, thank you once again!
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Help with Double tap and hold script

20 Sep 2017, 00:29

Use a Block for the While loop and include a Sleep command.

The Timeout in KeyWait needs to use a period (Americanized numbers). So T0.5 should work. I imagine it's a bit annoying for many people around the world, but it's done because commas already separate parameters in all the commands and functions.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5 and 377 guests