Detect Click Type on Tray Icon - Multiple Tray Menus

Post your working scripts, libraries and tools
ahknewguy
Posts: 8
Joined: 02 Apr 2020, 12:13

Detect Click Type on Tray Icon - Multiple Tray Menus

24 Jun 2020, 17:17

Hi, everyone!

This is basically a way to detect when you click on the script's tray icon and with which button. I use it to launch 3 custom menus (Left, Middle and Right Click) and a "default" option with Left DoubleClick.

This is a working example:

Code: Select all

#NoEnv
SendMode Input
#SingleInstance, Force


AHK_ICONCLICKCOUNT := 0		; Start of auto-execute section

Menu, LeftClickMenu, add, LeftClick, Test
Menu, LeftClickMenu, add
Menu, LeftClickMenu, add, % "Exit", ExitProgram

Menu, MiddleClickMenu, add, MiddleClick, Test
Menu, MiddleClickMenu, add
Menu, MiddleClickMenu, add, % "Exit", ExitProgram

Menu, RightClickMenu, add, RightClick, Test
Menu, RightClickMenu, add
Menu, RightClickMenu, add, % "Exit", ExitProgram

OnMessage(0x404, "AHK_ICONCLICKNOTIFY")

Return						; End of auto-execute section


AHK_ICONCLICKNOTIFY(wParam,lParam)
{
	global AHK_ICONCLICKCOUNT
	if (lParam = 0x201)
	{
		AHK_ICONCLICKCOUNT := 1
		SetTimer, AHK_ICONCLICKCHECK, -200
	}
	else if (lParam = 0x203)
	{
		AHK_ICONCLICKCOUNT := 2
	}
	else if (lParam = 0x205)			; RIGHT CLK
	{
		; Menu, Tray, Show			; launch standard menu
		Menu, RightClickMenu, Show	; or a custom one
	}
	else if (lParam = 0x208)			; MIDDLE CLK
	{
		Menu, MiddleClickMenu, Show
	}
	return 0
}

AHK_ICONCLICKCHECK()
{
	global AHK_ICONCLICKCOUNT
	if  (AHK_ICONCLICKCOUNT = 1)		; LEFT CLK
	{
		Menu, LeftClickMenu, Show
	}
	else if (AHK_ICONCLICKCOUNT = 2)	; LEFT DBCLK
	{
		Msgbox % "LEFT DBCLK!"
	}
	return 0
}

Test:
MsgBox, % A_ThisMenu . "@" . A_ThisMenuItem . "@" . A_ThisMenuItemPos
return

ExitProgram:
ExitApp

I put this together from various posts in the old forum. Credits to Lexikos, Animeaime and Serenity.

https://autohotkey.com/board/topic/34578-show-tray-menu-if-left-click-thanks-goes-to-lexikos/
https://autohotkey.com/board/topic/33843-single-click-vs-double-click-on-tray-icon/
https://autohotkey.com/board/topic/11250-mouseover-trayicon-triggering-an-event/#entry153388
User avatar
elModo7
Posts: 167
Joined: 01 Sep 2017, 02:38
GitHub: elModo7
Location: Spain
Contact:

Re: Detect Click Type on Tray Icon - Multiple Tray Menus

25 Jun 2020, 14:46

This was a functionality I wanted but didn't take the time to investigate much as it was a "good to have", thank you for sharing! :)
Have a good day!
:beer:

Return to “Scripts and Functions”

Who is online

Users browsing this forum: baesi, TheArkive and 25 guests