Prev: Are there in Python some static web site generating tools like?webgen, nanoc or webby in Ruby ?
Next: a newbie's question
From: Alex Hall on 9 Mar 2010 11:48 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. I am using the message looping mode from Tim Golden's website, and it works beautifully until I try to put an out-of-sequence keycode into the keystrokes dictionary. The dictionary contains numbers 0-9 (48-57) and all is well, but when I put in this 107 code then the function tied to 107 is not called, yet the ones tied to 48-57 still work normally. Why would the sequence matter, or does it not and I am doing something else wrong? Here is a sample of my dictionary: keys.append({ 1 : (48, win32con.MOD_CONTROL), 2 : (49, win32con.MOD_CONTROL), 3 : (50, win32con.MOD_CONTROL), 4 : (51, win32con.MOD_CONTROL), 5 : (52, win32con.MOD_CONTROL), 6 : (53, win32con.MOD_CONTROL), 7 : (54, win32con.MOD_CONTROL), 8 : (55, win32con.MOD_CONTROL), 9 : (56, win32con.MOD_CONTROL), 10 : (57, win32con.MOD_CONTROL), 11 : (107, win32con.MOD_CONTROL | win32con.MOD_SHIFT) #never calls its #function, and note that it is not in the sequence of the other ten }) and here is a list of functions tied to it: funcs.append({ 1 : exitProgram, 2 : arm.sayLoad1, 3 : arm.sayLoad2, 4 : arm.sayLoad3, 5 : arm.sayLoad4, 6 : arm.sayProcAvg, 7 : arm.sayUsedRam, 8 : arm.sayDisk1Info, 9 : arm.sayDisk2Info, 10 : nextMode, 11: clipboard.toClipboard }) If I were to tie clipboard.toClipboard to any of keys 1-10 (0-9, or 48-57) then it would work fine; it is when the 107 shows up that the function is not called, and this is a huge limitation for the rest of the program since I am stuck with just the ten numbers available on the keyboard. Any suggestions would be great! On 3/9/10, Tim Golden <mail(a)timgolden.me.uk> wrote: > On 09/03/2010 13:55, Alex Hall wrote: >> Hi all, >> In the same program I wrote about yesterday, I have a dictionary of >> keystrokes which are captured. I just tried adding a new one, bringing >> the total to 11. Here are entries 10 and 11; 10 has been working fine >> for months. >> >> 10 : (57, win32con.MOD_CONTROL), >> 11 : (99, win32con.MOD_CONTROL | win32con.MOD_SHIFT) >> >> Now, though, when I press ctrl-shift-c (keystroke 11) > > Ctrl-C (with or without any other modifier) has a special meaning > which overrides any hotkeys. You may be able to do something by > adding a break handler through SetConsoleCtrlHandler (exposed in > win32api). But it would obviously be a special case outside your > normal control flow. > > TJG > -- > http://mail.python.org/mailman/listinfo/python-list > -- Have a great day, Alex (msg sent from GMail website) mehgcap(a)gmail.com; http://www.facebook.com/mehgcap
From: alex23 on 9 Mar 2010 23:01 Alex Hall <mehg...(a)gmail.com> wrote: > Why would the sequence > matter, or does it not and I am doing something else wrong? Here is a > sample of my dictionary: Showing us the code that handles the dictionary lookup + function calling would probably help us a lot more here.
From: Steven D'Aprano on 10 Mar 2010 04:16 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
From: Tim Golden on 10 Mar 2010 04:25 On 10/03/2010 09:16, Steven D'Aprano wrote: > 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)() FWIW (altho' it's not clear from the OP's code) he's basically doing this: http://timgolden.me.uk/python/win32_how_do_i/catch_system_wide_hotkeys.html which uses the dictionary keys as an id in the call to RegisterHotKey. Obviously, that doesn't explain why he's building lists of dictionaries. TJG
From: Alex Hall on 10 Mar 2010 07:09
I am honestly a bit lost as to why keys.append() is not a good choice here, but I have it working. I apparently have to use the ascii for capital letters if I am capturing the shift modifier, not the lowercase ascii. Using 67 instead of 99 works as expected. I use append because the program has three different modes. Eventually, each mode may have its own keystrokes. When the user switches modes, the previous mode's keystrokes are unregistered and the new keystrokes, keys[currentModeNumber], are registered. The same with the functions; when a function is called from the dictionary, it is called using funcs[currentModeNumber]. Again, this lets me put all my functions into one big list, where each member of the list is a dictionary. I probably have the terminology wrong, but hopefully that makes sense. Sorry for not explaining that earlier, but I was just looking for problems in the key codes. Thanks for your help! On 3/10/10, Tim Golden <mail(a)timgolden.me.uk> wrote: > On 10/03/2010 09:16, Steven D'Aprano wrote: >> 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)() > > FWIW (altho' it's not clear from the OP's code) he's basically > doing this: > > http://timgolden.me.uk/python/win32_how_do_i/catch_system_wide_hotkeys.html > > which uses the dictionary keys as an id in the call to RegisterHotKey. > > Obviously, that doesn't explain why he's building lists of dictionaries. > > > TJG > -- > http://mail.python.org/mailman/listinfo/python-list > -- Have a great day, Alex (msg sent from GMail website) mehgcap(a)gmail.com; http://www.facebook.com/mehgcap |