Can you identify which tray icon got single left click? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Can you identify which tray icon got single left click?

06 Jan 2019, 20:37

Hi, guys, I've been searching and testing ideas, including using Sean's TrayIcon library, interrogating my script's HICON that I use for the Menu, Tray, Icon, HICON:*%hIcon%, I've tried using MouseGetPos and all the data I can get from TrayIcon_GetInfo() and I can't get anything to match. I'd like the script to do something when I single left-click the tray icon, but how do I know which icon was clicked?

ETA: The icon isn't static, but keeps changing visually, so I can't search for it that way.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Can you identify which tray icon got single left click?  Topic is solved

28 Jan 2019, 05:57

You can use the Acc library to get the name of the tray icon under the cursor.

Code: Select all

;[Acc functions]
;Acc library (MSAA) and AccViewer download links - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=26201

;tested with Windows 7
q:: ;get name of tray icon under cursor
vChildID := 0
oAcc := Acc_ObjectFromPoint(vChildID)
vName := ""
try vName := oAcc.accName(vChildID)
MsgBox, % vName
return
Since the systray uses a toolbar control, you can get the index of the highlighted item. The scripts that get info from the systray, also get the toolbar button index, which you can match up with the index retrieved via TB_GETHOTITEM.

Code: Select all

;tested on Windows 7
q:: ;toolbar (e.g. systray) - get hot item (highlighted item)
DetectHiddenWindows, On
ControlGet, hTB, Hwnd,, ToolbarWindow321, ahk_class NotifyIconOverflowWindow
SendMessage, 0x447,,,, % "ahk_id " hTB ;TB_GETHOTITEM := 0x447
MsgBox, % ErrorLevel
return
Last edited by jeeswg on 28 Jan 2019, 16:32, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Can you identify which tray icon got single left click?

28 Jan 2019, 15:32

Hi jeeswg, that looks promising, thanks. But so far I'm getting "FAIL" for the ErrorLevel in the second script. The first one does give the text of the tooltip.

Anyway, I've had an idea. It occurs to me that I could identify the tray icon clicked on using the tooltip if it was unique and unchanging. Unfortunately, it's being updated with the time when the mouse hovers. But I can identify it by that dynamically - I hadn't thought of it until now. I can therefore use the first of your scripts (or one of the functions in Sean's TrayIcon script) when a left click is detected, and if that matches the relevant formatted timestamp (maybe I'll stick something unique in there too), then it's a click on the icon of my script that I can process.

Still might be interesting to know why I get the "FAIL" result though. I'm trying not to take it personally. ;)

Thanks for your help.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Can you identify which tray icon got single left click?

28 Jan 2019, 16:37

- I've fixed the script above, adding DetectHiddenWindows, On.
- Although the control/window details may vary depending on the OS. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Can you identify which tray icon got single left click?

28 Jan 2019, 17:22

I now get -1 wherever I click. It's Windows 7 Home Premium 64bit. Thanks.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Can you identify which tray icon got single left click?

29 Jan 2019, 12:52

- Hmm, I always assume the overflow window (box). For me, icons never persist on the taskbar proper.
- You can try adjusting the control/window criteria, or use MouseGetPos.

Code: Select all

q:: ;toolbar (e.g. systray) - get hot item (highlighted item)
DetectHiddenWindows, On
;ControlGet, hTB, Hwnd,, ToolbarWindow321, ahk_class NotifyIconOverflowWindow
;ControlGet, hTB, Hwnd,, ToolbarWindow321, ahk_class Shell_TrayWnd
MouseGetPos,,,, hTB, 2
SendMessage, 0x447,,,, % "ahk_id " hTB ;TB_GETHOTITEM := 0x447
MsgBox, % ErrorLevel
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Can you identify which tray icon got single left click?

06 Feb 2019, 08:27

jeeswg wrote:
29 Jan 2019, 12:52
For me, icons never persist on the taskbar proper.
That's odd. (You've answered my question, I'm just :offtopic: now :lol: ). Do you mean the "systray"/Notification Area (which I suppose is also part of the "Taskbar")? They have settings as to when they're shown and when they're hidden and put in the auxilliary section, but I'm sure you know that. But that's all I can think "icons on the taskbar" means, unless you mean your taskbar buttons (iconized) aren't persisting. If that's the case, surely there's something wrong with Windows!?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Can you identify which tray icon got single left click?

06 Feb 2019, 08:37

- Systray icons could have two meanings: the icons visible on the taskbar itself in the bottom-right corner, and the icons in the overflow box (window). (I wasn't sure if you wanted to handle one or both.)
- Sometimes the icons appear briefly on the taskbar, but then disappear, and are then available in the overflow window.
- (I usually have zero icons in the bottom-right corner, with everything in the overflow window.)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Can you identify which tray icon got single left click?

08 Feb 2019, 07:07

Of course, thanks, that's a good reminder to me - I get used to things looking the way they are on my system and forget people set things up quite differently. Useful reminder, because my current project is a tray clock, and if people don't know about the Taskbar settings, it might just be in the overflow, which would be pretty pointless! For a long time, I've had the box checked to show all icons on my system, so no overflow ones at all. This is probably quite unusual, and isn't the default.

I'm trying it the more usual way, and I'll have to make a note in my clock program to set its icon to always visible...or maybe there's a system call for that. It's intended to replace the system clock (if people wish to), so I'll see if I can program that into the options too.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen and 364 guests