How to get different outputs on multiple key-presses Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Tom1
Posts: 24
Joined: 17 Sep 2018, 12:05
Contact:

How to get different outputs on multiple key-presses

20 Mar 2019, 15:57

hello there~

Code: Select all

ALT & NUMPAD1::
Send b
KeyWait NUMPAD1
Return
How can I make my code send a different output when I press NUMPAD1 two times or three times (while holding down ALT)?

I'd like the same behavior as when you'd type a symbol using ALT codes and Numpad
User avatar
Scr1pter
Posts: 1271
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: How to get different outputs on multiple key-presses

20 Mar 2019, 16:59

Hi man,

Easiest way would be to increment a helper variable
and make the output depending on its value.
Example:

Code: Select all

!Numpad1::
nMode++
if nMode = 1
{
  Send b
}
if nMode = 2
{
  Send c
}
if nMode = 3
{
  Send d
  nMode = 0 ; Reset value
}
return
If you want to make it depending on the time (like double or triple click), you will have to work with timers.

Edit
I'm not 100% sure if you need KeyWait here.
Keep it in mind, though.

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
Tom1
Posts: 24
Joined: 17 Sep 2018, 12:05
Contact:

21 Mar 2019, 03:09

Spoiler
Edit: Change of mind, please read my 2nd reply
Last edited by Tom1 on 21 Mar 2019, 12:24, edited 2 times in total.
User avatar
Scr1pter
Posts: 1271
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: How to get different outputs on multiple key-presses

21 Mar 2019, 06:01

So, and you actual answer is :?:
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
Tom1
Posts: 24
Joined: 17 Sep 2018, 12:05
Contact:

21 Mar 2019, 10:28

Please ignore my 1st reply.

I changed my mind on having it dependent on pressing down ALT, I’d like the code to be time-dependent.

I’ve made a new code with SetTimer. Is that the best way to do this? (If SetTimer is CPU-intensive, and there’s a better/more lightweight solution, please LMK)

Code: Select all

NUMPAD3::
if keycount > 0
{
    keycount += 1
    return
}

keycount = 1
SetTimer, action, -300
return

action:
if keycount = 1
{
    Send b
}
else if keycount = 2
{
    Send c
}
keycount = 0
return
In this code, I only have outputs for single- & double-press. When I double-press, the code waits for the remainings of the 300ms to finish and then sends c. How can I make the double-press action immediate? (as there's no triple-press action afterwards)

Thank you,
Tom
User avatar
Scr1pter
Posts: 1271
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: How to get different outputs on multiple key-presses  Topic is solved

21 Mar 2019, 16:24

Give it a try, I cannot test it:

Code: Select all

Numpad3::
keyCount++
if keyCount = 2
{
  Send c
  keyCount = 0
}
SetTimer, action, -300
return

action:
if keyCount = 1
{
    Send b
    keyCount = 0
}
return
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to get different outputs on multiple key-presses

21 Mar 2019, 17:07

How do I detect the double press of a key or button?

For a long-press / double-tap solution:

Code: Select all

F2::
if ( A_PriorHotkey = A_ThisHotkey && A_TimeSincePriorHotkey < 175)
{
    SetTimer clic1, Off
    ToolTip F2 Doble
}
else
    SetTimer clic1, -175
KeyWait F2
return


clic1:
if ( GetKeyState("F2") )
    ToolTip F2 Mantenido
else
    ToolTip F2 Único
return
source: Copiar Pegar y Cortar
my scripts
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: How to get different outputs on multiple key-presses

22 Mar 2019, 05:19

Here is how to have it abort at the 2nd press:

Code: Select all

NUMPAD3::
keyCount++
if (keyCount == 2){
    SetTimer, action, off
    GoSub, action
} else {
    SetTimer, action, -300
}
return

action:
if (keycount == 1)
{
    Send b
}
else if (keycount == 2)
{
    Send c
}
keycount = 0
return
Here is the same thing done with my TapHoldManager library:

Code: Select all

#include Lib\TapHoldManager.ahk

thm := new TapHoldManager(, , 2)  ; 2 is the "Max Taps" setting, limiting it to only 2 taps allowed
thm.Add("Numpad3", Func("Numpad3Func")) ; You could set "Max Taps" on a per-key basis here instead
return

Numpad3Func(isHold, taps){
    if (isHold){
        ; Add code here if you want to respond to long press
    } else {
        ; Respond to multi-tap
        if (taps == 1){
            Send b
        } else if (taps == 2){
            Send c
        }
    }
}
For just one key, the code is not that much shorter, but if you want to do it with multiple keys it really starts to pay dividends.
Here is an example with two keys - Numpad 3 and 4:

Code: Select all

#include Lib\TapHoldManager.ahk

thm := new TapHoldManager(, , 2)  ; 2 is the "Max Taps" setting, limiting it to only 2 taps allowed
thm.Add("Numpad3", Func("Numpad3Func")) ; You could set "Max Taps" on a per-key basis here instead
thm.Add("Numpad4", Func("Numpad4Func"))
return

Numpad3Func(isHold, taps){
    if (isHold){
        ; Add code here if you want to respond to long press
    } else {
        ; Respond to multi-tap
        if (taps == 1){
            Send b
        } else if (taps == 2){
            Send c
        }
    }
}

Numpad4Func(isHold, taps){
    if (isHold){
        ; Add code here if you want to respond to long press
    } else {
        ; Respond to multi-tap
        if (taps == 1){
            Send d
        } else if (taps == 2){
            Send e
        }
    }
}
And here is how to do two keys both handled by the same function:

Code: Select all

#include Lib\TapHoldManager.ahk

thm := new TapHoldManager(, , 2)  ; 2 is the "Max Taps" setting, limiting it to only 2 taps allowed
thm.Add("Numpad3", Func("KeyEvent").Bind("Numpad3")) ; You could set "Max Taps" on a per-key basis here instead
thm.Add("Numpad4", Func("KeyEvent").Bind("Numpad4"))
return

KeyEvent(keyName, isHold, taps){
    static keyMappings := {Numpad3: ["b", "c"], Numpad4: ["d", "e"]}
    if (isHold){
        ; Add code here if you want to respond to long press
    } else {
        ; Respond to multi-tap
        Send % keyMappings[keyName, taps]
    }
}
User avatar
Tom1
Posts: 24
Joined: 17 Sep 2018, 12:05
Contact:

29 Mar 2019, 08:30

Thanks for the replies guys.

The code from Scripter worked out for me

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, skeerrt and 165 guests