Triple actions shourtcuts (Single, Double, Hold) with combination of single key and modifier keys

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
trpldss
Posts: 4
Joined: 19 Jun 2023, 01:28

Triple actions shourtcuts (Single, Double, Hold) with combination of single key and modifier keys

25 May 2024, 21:35

Hello!

Firstly, please understand that I have zero knowledge about coding and programming.
I can't understand what every single line does so please bear with me.
I have been looking for triple action (Single tap, Double tap, Hold) shortcut mapping method that I can easily tweak and assign with AHK and I have finally found this amazing solution.

https://superuser.com/questions/1838305/this-single-double-and-hold-ahk-script-is-not-working?newreg=ad80262a205c4bb0b5038afc185a8f2f

It is almost perfect but one issue is that I can't use modifier keys as mainKey.

For Illustrator, There's a lot of limits when it comes to customizing shortcuts.
Some of them are hard coded which I can't change at all and it doens't even allow me to use cntl or alt keys for Tools' shortcuts.

What I am trying to do is, I assign shortcut "Shift+1" to Selection Tool, "Shift+2" to Lasso Tool and mapping the shortcuts with AHK.
Like, when pressing "Cntl+Space", it triggers "Shift+1" so I can use Selection Tool and "Cntl+Double Tap Space" for "Shift+2" which is Lasso Tool.
This way, I can use Cntl key for Tools.

Code: Select all

        TripleBind(mainKey, msec, singleKey, doubleKey, holdKey)
{
  ProcessPress(key)
  {
    static count := 0
    held := true
    ++count
    AfterTime()
    {
      if (held) {
        Send holdKey
      } else if (count == 1) {
        Send singleKey
      } else {
        Send doubleKey
      }
      count := 0
    }

    if (count == 1) {
      SetTimer AfterTime, -1 * msec
      KeyWait mainKey
      held := false
    }
  }

  Hotkey "$" . mainKey, ProcessPress
}

TripleBind("space", 300, "Numpad1", "^+!v", "+q") ---This works fine
TripleBind("^space", 300, "Numpad2", "^+!b", "+w") ---This doesn't work

With the script I can assign any keys with modifier keys (i.g ^+!v, +q, etc.) for the singleKey, doubleKey and holdKey, but I can't assign them for mainKey. mainKey seems to work with only a single key without modifer keys.

I need the mainKey works with any modifier keys (cntl, alt, shift or all of three at once)
What parts should be added or modified to achieve it?

I really appreciate your time.
niCode
Posts: 320
Joined: 17 Oct 2022, 22:09

Re: Triple actions shourtcuts (Single, Double, Hold) with combination of single key and modifier keys

26 May 2024, 00:04

One idea is to change the KeyWait to:

Code: Select all

KeyWait RegExReplace(mainKey, '[~*$!^+#<>]')
It doesn't check if modifiers are being held during second press/hold but as long as they were held when the function started it continues to carry out that original single/double/hold execution so it should be good enough to feign working as if they were held.
trpldss
Posts: 4
Joined: 19 Jun 2023, 01:28

Re: Triple actions shourtcuts (Single, Double, Hold) with combination of single key and modifier keys

26 May 2024, 03:08

niCode wrote:
26 May 2024, 00:04
One idea is to change the KeyWait to:

Code: Select all

KeyWait RegExReplace(mainKey, '[~*$!^+#<>]')
It doesn't check if modifiers are being held during second press/hold but as long as they were held when the function started it continues to carry out that original single/double/hold execution so it should be good enough to feign working as if they were held.
It works! Thank you very much. One more thing, how could I activate this script only for illustrator?
The #IfWinActive method didn't work so I researched and added this line on the top, but it doens't work either.

Code: Select all

#HotIf WinActive("ahk_exe Illustrator.exe")
Rohwedder
Posts: 7776
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Triple actions shourtcuts (Single, Double, Hold) with combination of single key and modifier keys

26 May 2024, 03:11

Hallo,
in addition, multi-letter key names must be bracketed for singleKey, doubleKey and holdKey:

Code: Select all

#Requires AutoHotkey v2.0
TripleBind("space", 300, "{Numpad1}", "^+!v", "+q")
TripleBind("^space", 300, "{Numpad2}", "^+!b", "+w")

TripleBind(mainKey, msec, singleKey, doubleKey, holdKey) {
	ProcessPress(key) {
		static count := 0
		held := true, ++count
		AfterTime() {
			Send held?holdKey:(count==1)?singleKey:doubleKey
			count := 0
		}
		if (count == 1) {
			SetTimer AfterTime, -msec
			KeyWait RegExReplace(mainKey, '[~*$!^+#<>]')
			held := false
		}
	}
	Hotkey "$" . mainKey, ProcessPress
}
niCode
Posts: 320
Joined: 17 Oct 2022, 22:09

Re: Triple actions shourtcuts (Single, Double, Hold) with combination of single key and modifier keys

26 May 2024, 03:45

Rohwedder wrote:
26 May 2024, 03:11
in addition, multi-letter key names must be bracketed
Good catch. I even tested that part but I guess I was only focused on any result instead of the correct result.

trpldss wrote:
26 May 2024, 03:08
how could I activate this script only for illustrator?
#HotIf works for normal hotkey assignments like:

Code: Select all

#HotIf WinActive("ahk_exe Illustrator.exe")
F1::MsgBox('hello')
#HotIf
When using the Hotkey function, you use the HotIf function like so:

Code: Select all

HotIf (*) => WinActive('ahk_exe Illustrator.exe')
Hotkey "$" . mainKey, ProcessPress
HotIf
trpldss
Posts: 4
Joined: 19 Jun 2023, 01:28

Re: Triple actions shourtcuts (Single, Double, Hold) with combination of single key and modifier keys

26 May 2024, 10:53

Thank you both! Is there any way that I can disable the shortcuts when the cursor is in the text type field? (Like, in Text box, Text Field in popup windows ,etc.)
I have found this codes and have no idea how and where to appply it in the script.
Sorry again, if it's too basic question.

Code: Select all

#SingleInstance force
	#If (A_Cursor != "IBeam" && WinActive("ahk_exe Illustrator.exe"))
Rohwedder
Posts: 7776
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Triple actions shourtcuts (Single, Double, Hold) with combination of single key and modifier keys

26 May 2024, 11:21

TripleBind() uses the Hotkey function, i.e. use the HotIf function as follows:

Code: Select all

HotIf (*) => (A_Cursor != "IBeam" && WinActive("ahk_exe Illustrator.exe"))
Hotkey "$" . mainKey, ProcessPress
HotIf
trpldss
Posts: 4
Joined: 19 Jun 2023, 01:28

Re: Triple actions shourtcuts (Single, Double, Hold) with combination of single key and modifier keys

26 May 2024, 12:18

Rohwedder wrote:
26 May 2024, 11:21
TripleBind() uses the Hotkey function, i.e. use the HotIf function as follows:

Code: Select all

HotIf (*) => (A_Cursor != "IBeam" && WinActive("ahk_exe Illustrator.exe"))
Hotkey "$" . mainKey, ProcessPress
HotIf
Thank you! I tried this, but it still calls the mapped keys and type them instead when I press space, a, b keys in the text input field.

Code: Select all

#Requires AutoHotkey v2.0

TripleBind("space", 300, "1", "2", "3")
TripleBind("a", 300, "4", "5", "6")
TripleBind("b", 300, "7", "8", "9")

HotIf (*) => (A_Cursor != "IBeam" && WinActive("ahk_exe Illustrator.exe"))

TripleBind(mainKey, msec, singleKey, doubleKey, holdKey) {
	ProcessPress(key) {
		static count := 0
		held := true, ++count
		AfterTime() {
			Send held?holdKey:(count==1)?singleKey:doubleKey
			count := 0
		}
		if (count == 1) {
			SetTimer AfterTime, -msec
			KeyWait RegExReplace(mainKey, '[~*$!^+#<>]')
			held := false
		}
	}
	HotIfWinActive "ahk_exe Illustrator.exe"
	Hotkey "$" . mainKey, ProcessPress
}

HotIf
Rohwedder
Posts: 7776
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Triple actions shourtcuts (Single, Double, Hold) with combination of single key and modifier keys

27 May 2024, 02:49

Try:

Code: Select all

#Requires AutoHotkey v2.0

TripleBind("space", 300, "1", "2", "3")
TripleBind("a", 300, "4", "5", "6")
TripleBind("b", 300, "7", "8", "9")

TripleBind(mainKey, msec, singleKey, doubleKey, holdKey) {
	ProcessPress(key) {
		static count := 0
		held := true, ++count
		AfterTime() {
			Send held?holdKey:(count==1)?singleKey:doubleKey
			count := 0
		}
		if (count == 1) {
			SetTimer AfterTime, -msec
			KeyWait RegExReplace(mainKey, '[~*$!^+#<>]')
			held := false
		}
	}
	HotIf (*) => (A_Cursor != "IBeam" && WinActive("ahk_exe Illustrator.exe"))
	Hotkey "$" . mainKey, ProcessPress
	HotIf
}
or:

Code: Select all

#Requires AutoHotkey v2.0

TripleBind("F2", 300, "1", "2", "3")

TripleBind("space", 300, "4", "5", "6", "ahk_class Notepad")

TripleBind("space", 300, "1", "2", "3", "ahk_exe Illustrator.exe", "IBeam")
TripleBind("a", 300, "4", "5", "6", "ahk_exe Illustrator.exe", "IBeam")
TripleBind("b", 300, "7", "8", "9", "ahk_exe Illustrator.exe", "IBeam")

TripleBind(mainKey, msec, singleKey, doubleKey, holdKey, ActiveWin:="", NotCursor:="") {
	ProcessPress(key) {
		static count := 0
		held := true, ++count
		AfterTime() {
			Send held?holdKey:(count==1)?singleKey:doubleKey
			count := 0
		}
		if (count == 1) {
			SetTimer AfterTime, -msec
			KeyWait RegExReplace(mainKey, '[~*$!^+#<>]')
			held := false
		}
	}
	HotIf (*) => (A_Cursor != NotCursor && ActiveWin?WinActive(ActiveWin):True)
	Hotkey "$" . mainKey, ProcessPress
	HotIf
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Feder, Google [Bot] and 47 guests