How can I make this MultiPressListener better?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
GollyJer
Posts: 59
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

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: 575
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Somewhere in the US

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].......: 1.1.33.02 Unicode 64-bit
- [OS].........: Windows 10.0.19041
- [GITHUB]...: github.com/DeltaPyth
- [PAYPAL]....: paypal.me/DelPyth
- [DISCORD]..: Tophat Cat // Delta#8888

Remember to use [code]CODE[/code] for your multi-line scripts.
Stay safe, stay inside, and remember to wash your hands for 20 seconds!
User avatar
GollyJer
Posts: 59
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

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: 54
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: 59
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

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”

Who is online

Users browsing this forum: AHKStudent, boiler, mikeyww, roysubs and 60 guests