Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

One shortcut, multiple actions, depending on click count


  • Please log in to reply
No replies to this topic
Shield
  • Members
  • 4 posts
  • Last active: Dec 21 2009 10:25 AM
  • Joined: 20 Dec 2009
Hi there ! Posted Image

I recently wanted a media players shortcut in the Quick Launch bar to trigger different actions, depending on whether it was single or double clicked, and without the help of a persistent script that watches the mouse or something like that. I managed to do this using the following code, which I think it's worth posting.

Please note that this will only work as a process, so you have to compile it !

If not A_IsCompiled
	{	MsgBox Please compile this script, otherwise it won't run as a process, which is necessary !
		ExitApp
	}

; Waiting period for a potential further instance of the script. This will work as threshold for a double click, which is why we use that system setting.

Sleep % DllCall("GetDoubleClickTime")

; Now look for all running instances of this script using COM

For process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
	{	If (process.Name = A_ScriptName)
			{	InstanceCounter++								; Count the instances

				ProcessCreationDate := process.CreationDate				; Fetch the birthday of a qualified process
				StringTrimRight ProcessCreationDate,ProcessCreationDate,4		; Simplify the timestring
				If not CreationDateOfFirstInstance
					CreationDateOfFirstInstance := ProcessCreationDate

				If (ProcessCreationDate <= CreationDateOfFirstInstance)		; If the process is older than other instances ...
					{	CreationDateOfFirstInstance := ProcessCreationDate	; ... remember its birthday ...
						ProcessIdOfFirstInstance := process.ProcessId		; ... and ID
					}
			}
	}

; Knowing now which instance was created first, we can check if it's this very instance itself

ThisScriptsProcessId := DllCall("GetCurrentProcessId")

If (ProcessIdOfFirstInstance = ThisScriptsProcessId)						; If so ...
	{	IfEqual InstanceCounter,1							; ... and there was no second one during the initial waiting period detected, perform the single-click-action
			MsgBox ,,,Single click (%InstanceCounter% instances);,1
		Else
			MsgBox ,,,DoubleClick (%InstanceCounter% instances);,1		; In case of multiple instances, perform the double-click-action instead
		ExitApp
	}

; The following code only becomes executed by any instance but the first one. Only action is to ensure it doesn't quit before it's detected by the first one
Process WaitClose,%ProcessIdOfFirstInstance%
ExitApp

Compile it using AutoHotkey_L, create a shortcut to the exe inside the Quick Launch bar or Start menu, and try both single and (fast) double-click on it...

Note: I advise not to integrate the desired actions into the script code. Launch them by replacing the MsgBox's with a suitable Run-command instead.

Hope the code will be useful for somebody.