Single or Double Left Click Tray Icon : Subroutine or display menu

Helpful script writing tricks and HowTo's
DigiDon
Posts: 178
Joined: 19 May 2014, 04:55
Contact:

Single or Double Left Click Tray Icon : Subroutine or display menu

Post by DigiDon » 15 Sep 2015, 16:18

Hi !

Normally the tray menu is displayed whenever the user right-click the tray icon.
And you can set a default item for when you double-click the tray.

A lot of people I guess (including myself) would like to launch a specific subroutine or simply display the same tray menu if the user left-clic this icon, or double left-click it.

This is something I wanted to do from a long time, but as I couldn't find anything doing searches I thought it was not feasable until I found very old threads about that.
EDIT : After looking for "AHK_NOTIFYICON" which is the magic solution I found numerous threads but none seem to me very very clear in the title or with partial answer to the large point so I hope maybe this one will be more visible.

topics examples :
[Solved] Single-click tray icon without a tray item - AHKScript (an AutoHotkey Forum) - Mozilla Firefox
http://ahkscript.org/boards/viewtopic.php?t=6110
How do i change the actions of clicking the tray icon - Ask for Help - AutoHotkey Community - Mozilla Firefox
http://www.autohotkey.com/board/topic/6 ... /?p=391707
Show tray menu if Left Click (thanks goes to Lexikos) - Ask for Help - AutoHotkey Community - Mozilla Firefox
http://www.autohotkey.com/board/topic/3 ... o-lexikos/

So thanks Lexikos for the OnMessage(0x404, "AHK_NOTIFYICON") solution.
Here is a sum-up that I think can be helpful to others and should even be included in the documentation.

To recognize what the user makes on tray icon, one should simply add to the top auto-execute part of his script :

Code: Select all

OnMessage(0x404, "AHK_NOTIFYICON")
and then add this as a function:

Code: Select all

AHK_NOTIFYICON(wParam, lParam) 
{
    if (lParam = 0x202) { ; user left-clicked tray icon
			;ADD ANY SUBROUTINE OR FUNCTION HERE
        return
    }
	else if (lParam = 0x203) {; user double left-clicked tray icon
		;ADD ANY SUBROUTINE OR FUNCTION HERE
        return
    }
   if (lParam = 0x204) { ; user right-clicked tray icon
			;ADD ANY SUBROUTINE OR FUNCTION HERE
        return
    }
    if (lParam = 0x208) { ; user middle-clicked tray icon
			;ADD ANY SUBROUTINE OR FUNCTION HERE
        return
    }
}
If you want to display tray icon as it was right clicked, just put this line as action:

Code: Select all

Menu, Tray, Show
The problem is when the user double left-click, both lParam 0x202 and 0x203 are sent.

I tried to distinguish and tried some timer/static/global variable tricks but couldn't really understand what was happening.

Anyway in the meantime this solution with timers seems to be working well:

Code: Select all

AHK_NOTIFYICON(wParam, lParam) 
{
    if (lParam = 0x202) {
			Settimer, Tray_SingleLclick, -400
        return 1
    }
	else if (lParam = 0x203) {
		Settimer, Tray_DoubleLclick, -150
        return 1
    }
}

Tray_DoubleLclick:
loop 2
Settimer, Tray_SingleLclick, Off
msgbox You Double left-clicked tray icon
return

Tray_SingleLclick:
msgbox You left-clicked tray icon
return
So, can it be maybe completed/adjusted and put into documentation? :roll:

Thanks and see ya! :)
EverFastAccess : Take Notes on anything the Fast way: Attach notes, Set reminders & Speed up research in 1 gesture - AHK topic
AHK Dynamic Obfuscator L - Protect your AHK code by Obfuscation - AHK topic
QuickModules for Outlook : Sort Outlook emails very quickly to multiple folders - AHK topic
Coding takes lots of time and efforts. If I have helped you or if you enjoy one of my free projects, please consider a small donation :thumbup:
Sorry I am working hard at the moment at a new job and can't commit on delays of answers & updates

Return to “Tutorials (v1)”