using message loop for hotkey capturing

MRAB python at
Tue Mar 23 20:37:51 CET 2010

Alex Hall wrote:
> On 3/23/10, MRAB <python at> wrote:
>> Incidentally, you might want to change:
>>      if(not action_to_take):
>> to:
>>      if action_to_take is None:
>> in case any of the values happen to be 0 (if not now, then possibly at
>> some time in the future).
> Sorry, could you explain why you suggested this? I do not follow.
> Because of the if statement "if action_to_take:", I figured it was
> saying "if action_to_take was successfully set" or something else
> having a boolean value. Guess not?
The code:

     globalFuncs.get (msg.wParam)

returns None if the key isn't in the dict.

'if' and 'while' statements treat other objects than just True as True,
in fact anything for which bool() returns True. For example:

     bool(100) returns True
     bool([1, 2, 3]) returns True
     bool('some text') returns True


     bool(0) returns False
     bool([]) returns False
     bool('') returns False
     bool(None) returns False

I also just noticed that you don't give action_to_take a default value
before checking whether it's a hotkey. Suppose that "msg.message ==
win32con.WM_HOTKEY" was False:

   if msg.message == win32con.WM_HOTKEY:
    action_to_take=globalFuncs.get (msg.wParam)
   if(not action_to_take):

It would get to "if(not action_to_take):" and either find that
action_to_take wasn't defined, or use the value from the previous pass
through the loop.

More information about the Python-list mailing list