How can I make this MultiPressListener better?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
GollyJer
Posts: 69
Joined: 19 Sep 2015, 19:33
Contact:

How can I make this MultiPressListener better?

15 Nov 2017, 22:13

My goal was to create a reusable way to handle multiple key presses. This below code works OK but smells and is buggy. Pressing F1, F1, F2 within the listenLength doesn't work correctly.
I feel like there's a better way. Maybe Custom Objects or some other less messy approach?

The goal would be to track keyPresses separately for each handler.
Any insight is appreciated. Thanks!

Code: Select all

#SingleInstance force
#NoEnv 

F1:: ListenForMultiplePresses(func("F1Handler"))
F2:: ListenForMultiplePresses(func("F2Handler"))

ListenForMultiplePresses(pressHandler, listenLength := 400) {
  Global keyPresses
  If ((A_PriorHotkey = A_ThisHotkey) && (keyPresses > 0)) {
    keyPresses += 1
    Return
  }
  keyPresses := 1
  SetTimer, %pressHandler%, %listenLength%
}

F1Handler() {
  Global keyPresses
  If (keyPresses = 1) {
    MsgBox, F1 pressed once.

  } Else If (keyPresses = 2) {
    MsgBox, F1 pressed twice.

  } Else If (keyPresses > 2) {
    MsgBox, F1 pressed twice+.

  }
  SetTimer, F1Handler, Off
  keyPresses := 0
}


F2Handler() {
  Global keyPresses
  If (keyPresses = 1) {
    MsgBox, F2 pressed once.

  } Else If (keyPresses = 2) {
    MsgBox, F2 pressed twice.

  } Else If (keyPresses > 2) {
    MsgBox, F2 pressed twice+.

  }
  SetTimer, F2Handler, Off
  keyPresses := 0
}
Last edited by GollyJer on 16 Nov 2017, 08:51, edited 1 time in total.
DorisMilbourn
Posts: 1
Joined: 16 Nov 2017, 04:29

Re: How can I make this MultiPressListener better?

16 Nov 2017, 04:36

My advice is ,Use react-key down as a higher-order element or decorator to skip key down occasions to the wrapped aspect, or name techniques directly through particular keys. Appropriate for implementing keyboard navigation or other shortcuts.
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: How can I make this MultiPressListener better?

16 Nov 2017, 07:57

Using Arrays, you can allow "smaller" and easier to manipulate messages

Code: Select all

#SingleInstance force
#NoEnv 

F1:: ListenForMultiplePresses(func("F1Handler"))
F2:: ListenForMultiplePresses(func("F2Handler"))

ListenForMultiplePresses(pressHandler, listenLength := 400) {
	Global keyPresses
	If ((A_PriorHotkey = A_ThisHotkey) && (keyPresses > 0)) {
		keyPresses += 1
		Return
	}
	keyPresses := 1
	SetTimer, %pressHandler%, %listenLength%
}

F1Handler() {
	Global keyPresses
	Static Items := ["once", "twice", "twice+"]
	MsgBox, % "F1 pressed " Items[keyPresses]
	SetTimer, F1Handler, Off
	keyPresses := 0
}

F2Handler() {
	Global keyPresses
	Static Items := ["once", "twice", "twice+"]
	MsgBox, % "F2 pressed " Items[keyPresses]
	SetTimer, F2Handler, Off
	keyPresses := 0
}

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
GollyJer
Posts: 69
Joined: 19 Sep 2015, 19:33
Contact:

Re: How can I make this MultiPressListener better?

16 Nov 2017, 09:11

Hi Delta. Thanks for the response.
Ultimately I will be doing different things with each keyPress so shortening the code isn't the primary issue.

What I really need help with is tracking keyPress count independently for each handler.
User avatar
Barney
Posts: 55
Joined: 28 May 2014, 20:03
Location: Germany

Re: How can I make this MultiPressListener better?

16 Nov 2017, 09:32

Then use separate variables for each key.
User avatar
GollyJer
Posts: 69
Joined: 19 Sep 2015, 19:33
Contact:

Re: How can I make this MultiPressListener better?

16 Nov 2017, 10:29

Barney wrote:Then use separate variables for each key.
That would entail creating a separate ListenForMultiplePresses for each hotkey wouldn't it? I'm hoping for a more OOP way of figuring this out.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: OrangeCat and 294 guests