Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting the focus on the tray icons in Windows XP using Python

I'm a Windows-XP-using keyboard devotee, and I want to bind Start-T to put the keyboard focus on the tray icon. (Because it'll be so much nicer than Start-B and then many Shift-Tabs.)

So I'd like to make a Python program that puts the keyboard focus on the tray icons. How could I do this from Python? I know very little about controlling Windows' behavior.

If there's some ready-made program that does this, I'll be happy to hear about that too.

like image 578
Ram Rachum Avatar asked Jun 21 '11 19:06

Ram Rachum


1 Answers

I am 99% sure that there is no supported way to access the tray notification area. The actually focusable element is the overflow button, not the tray icon itself - and the Windows Shell team doesn't want to guarantee that this button will always be focusable or even that this button will exist in future Windows versions. So there is no API to access it. This is an extremely hacky way to do it, might stop working at any time - for personal use only (tested on Windows 7):

import win32gui
import win32con
taskbar = win32gui.FindWindow("Shell_TrayWnd", None)
trayArea = win32gui.FindWindowEx(taskbar, None, "TrayNotifyWnd", None)
win32gui.SetForegroundWindow(taskbar)
win32gui.SendMessage(trayArea, win32con.WM_SETFOCUS, 0, 0)

Once again, do not use it in an application that other people will use. win32gui module is part of the Win32 Extensions.

For the hotkey you could use RegisterHotKey but it should be simpler to let Explorer handle that - add a link to your script to the Start menu and define a hotkey for it.

like image 140
Wladimir Palant Avatar answered Oct 08 '22 12:10

Wladimir Palant