using message loop for hotkey capturing

bagratte bagratte at live.com
Tue Oct 16 09:06:40 EDT 2012


hi all,

i am a complete newbie in windows programming and have come across this problem. i wanted a windows systray application which would seat in the notification are, would have a tiny popup menu and would also respond to a global hotkey. i've assembled something out of the demos in pywin32 which actually works except one thing: i don't like it. i am registering the hotkey within the window class constructor. something like this:

imports...
...

class MainWindow():
    def __init__(self):
        ...
        user32.RegisterHotKey(None, 1, win32con.MOD_CONTROL, ord('A'))

        message_map = {win32con.WM_DESTROY: self.OnDestroy,
                       win32con.WM_COMMAND: self.OnCommand,
                       win32con.WM_USER+20: self.OnTaskbarNotify,
                       win32con.WM_HOTKEY: self.OnHotKey}
        ...

    def OnCommand(self, hwnd, msg, wparam, lparam):
        print "OnCommand"

    def OnHotKey(self):
        print "OnHotkey"

    def OnDestroy(self, hwnd, msg, wparam, lparam):
        user32.UnregisterHotKey(None, 1)
        ...

    ...

def myLoop():
    msg = wintypes.MSG()
    while user32.GetMessageA(byref(msg), None, 0, 0) != 0:
        if msg.message == win32con.WM_HOTKEY:
            w.OnHotKey()
        user32.TranslateMessage(byref(msg))
        user32.DispatchMessageA(byref(msg))

w = MainWindow()
#PumpMessages()
myLoop()



now, if i use myLoop(), which is ripped from tim golden's "home-grown-loop", everything works. however, if i use PumpMessages(), the program won't respond to WM_HOTKEY, and it won't respond to WM_COMMAND (it will respond to WM_USER+20).
i was wondering if what i've done is not a ridiculously wrong way to do it. in other words, is there any way to make the window respond to WM_HOTKEY thus working with PumpMessages()?

i thank you all for your patience (this is my first ever windows programming experience).



More information about the Python-list mailing list