odd error

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Mar 10 10:16:27 CET 2010


On Tue, 09 Mar 2010 11:48:10 -0500, Alex Hall wrote:

> Okay, I changed the keycode from 99 (c) to 107 (k), and the errors have
> disappeared. However, now the function that should be called is not. As
> I said in a previous message, I have always had trouble with this sort
> of keystroke dictionary. It seems like, if a keycode is out of order or
> not one more than the number before it, the function to which it is tied
> will not get called.

Dictionaries aren't ordered, that can't be the problem.


> keys.append({
>   1 : (48, win32con.MOD_CONTROL),
>   2 : (49, win32con.MOD_CONTROL), [...]

Dicts don't have an append message. Why are you building a list and 
adding a dictionary to it?

The question is, how many such dicts are in the list, and which one are 
you searching for the function? Is it possible that the problem is that 
you have multiple dicts in the keys list, and then perform your look-ups 
on the wrong one?



Likewise for your list of functions:

> funcs.append({
>   1 : exitProgram,
>   2 : arm.sayLoad1, [...]

Perhaps all you need is a single dict, mapping characters to functions:

funcs = {  # Just a dict
    # keycode: function
    'q': exitProgram,
    'a': arm.sayLoad1
    # etc.
    }


Then whenever you get a keyboard event, convert it to the character:

keycode = 113
c = chr(keycode)
funcs(c)()




-- 
Steven



More information about the Python-list mailing list