Using one key combination for several commands? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1278
Joined: 06 Aug 2017, 08:21
Location: Germany

Using one key combination for several commands?

12 Sep 2017, 08:58

Hi,

Depending on how many times a key combination was pressed,
I would like to execute different commands.

I tried it with a test example, but it didn't work:

Code: Select all

MyNumber := 0
~^+F11::
MyNumber := MyNumber ++
Sleep 3000

if (MyNumber = 1) Run, Calc.exe
if (MyNumber = 2) Run, mspaint.exe
If Ctrl+Shift+F11 gets pressed one time and within 3000 ms not a second time,
Calc.exe should be executed.
If it gets pressed a second time within 3000 ms,
Calc.exe should not be executed but mspaint.exe.

How must be the working code?
Since I got no syntax error when running the code, I guess it's a logical mistake.
Maybe MyNumber is always 0...

Regards
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
GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: Using one key combination for several commands?  Topic is solved

12 Sep 2017, 09:11

Code: Select all

^+F11::	
CtrlShiftF11_count++      	; start counter
If (CtrlShiftF11_count = 1)
	SetTimer CtrlShiftF11_action, -50
return

	CtrlShiftF11_action:
KeyWait, Ctrl, L
KeyWait, Shift, L
	If (CtrlShiftF11_count = 1)
		MsgBox, CtrlShiftF11_action 1
	If (CtrlShiftF11_count = 2)
		MsgBox, CtrlShiftF11_action 2
; ...
CtrlShiftF11_count := 0      	; reset counter
return
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: Using one key combination for several commands?

12 Sep 2017, 09:13

While the hotkey is waiting, no new instances can run due to the default setting of #MaxThreadsPerHotkey. You can change the setting with this command or use SetTimer to work with multiple instances.

Code: Select all

MyNumber := 0

~^+F11::
	;MyNumber := MyNumber ++
	MyNumber ++
	If MyNumber = 1	; Only with the first press: Run the label ‘CtrlShiftF11’ in a new thread.
		SetTimer, CtrlShiftF11, -3000	; Use a negetive value to run the timer only once.
	Return

CtrlShiftF11:
	if (MyNumber = 1)
		Run, Calc.exe
	if (MyNumber = 2)
		Run, mspaint.exe
	if (MyNumber = 3)
		MsgBox hjk
	
	MyNumber := 0
	Return
User avatar
Scr1pter
Posts: 1278
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Using one key combination for several commands?

12 Sep 2017, 09:16

Thanks for the fast answer - it works perfectly :thumbup: :thumbup:

Also thank you to you, DyaTactic. ;)

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: macromint, Spawnova and 342 guests