using message loop for hotkey capturing
Tim Golden
mail at timgolden.me.uk
Wed Mar 24 05:41:00 EDT 2010
On 23/03/2010 17:01, Alex Hall wrote:
> 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.
There's definitely some confusion, as MRAB picked up, with the
globalkeys / globalfuncs thing. I can see no problem with simply
defining an additional pair of dictionaries parallel with each of
the keys/funcs dictionaries (note that the numbers are different
from those of othe other dictionaries as these are the registration
ids of the hotkeys):
global_keys = {
100: (68, win32com.MOD_ALT | win32con.MOD_CONTROL ...),
...
}
global_funcs = {
100 : dict.ShowLookupDialog,
...
}
Then your central action code could be something like:
action_to_take = global_funcs.get (msg.wParam) or HOTKEY_ACTIONS.get (msg.wParam)
if action_to_take:
action_to_take ()
This assumes that the funcs dictionaries will only ever contain a
valid function, so the only possible Falsish value will arise from
the get returning None.
TJG
BTW, you're shadowing a couple of builtins: global & dict which
might give you problems later if you needed them. There are other
issues with the structure of your code but nothing a little
refactoring couldn't sort out if you felt so inclined.
More information about the Python-list
mailing list