using message loop for hotkey capturing

A

Alex Hall

Hi all, but mainly Tim Golden:
Tim, I am using your wonderful message loop for keyboard input, the
one on your site that you pointed me to a few months ago. It has been
working perfectly as long as I had only one dictionary of keys mapping
to one dictionary of functions, but now I want two of each. My program
has different modes, which may have varying keystrokes, and I also
have some global keystrokes which are the same across all modes, like
exiting or switching modes. I cannot figure out how to make the
message loop look in two dictionaries at onc. I tried using an if,
saying that if action_to_take was not set in the mode-specific
dictionary then look at the global dictionary, but it is like it is
never looking in the global dictionary at all. I get no syntax errors
or problems when running the program, so it has to be something in my
logic. Go to
http://www.gateway2somewhere.com/sw/main.pyw
to see what I mean; the problem code is near the very bottom of the
file. Thanks for any suggestions. Oh, please note that I indent one
space per indentation level.
 
B

bagratte

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 globalhotkey. 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).
 
B

bagratte

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 globalhotkey. 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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top