tkinter 3.0 multiple keyboard events together

Pavel Kosina geon at post.cz
Sun Dec 28 03:43:20 EST 2008


janislaw napsal(a):
> Um, I could be only guessing what are you meant to do, unless you
> describe your problem in more detailed way. I.e. describe the desired
> behaviour, show code which you have, and describe the current
> behaviour.
>
>   

well, I am working on a tutorial for youngster (thats why i need to stay 
the code as easy as possible). In this game you are hunted by robots. I 
could use key"7" on numeric keypad for left-up moving but seems to me, 
that "4"+"8" is much more standard for them.
>> This solution has disadvantage that after you release one key, that the
>> function keyPressHandler stopped to be called by the other pressed keys.
>> I would have not to build moving the player in KeyPresshandler, but on
>> another new function that would have to read periodically "keys" and to
>> act afterwards....
>>     
>
> Hmmm. Maybe you'd like to hook into Tkinter event loop, i.e. by custom
> events if you don't like periodical polling.
>   
No, that would be even more difficult. I already have a code that use 
your idea:

from Tkinter import *
root = Tk()

pressedKeys=set()

def onKeyPress(event):
    pressedKeys.add(event.keysym)
 
def onKeyRelease(event):
    pressedKeys.remove(event.keysym)

def move():
    print list(pressedKeys)
    root.after(100,move)
     
root.bind("<KeyPress>", onKeyPress)
root.bind("<KeyRelease>", onKeyRelease)
root.after(100,move)
root.mainloop()


well, I thought that gui´s have such a problem already built-in - so 
that i am not pressed to code it. You know, its not exactly about me - 
if I do it for myself, for my program, that there is no problem, but I 
need to explained it to begginners ..... And I do not want, as might be 
more usual, do my module, that would cover this "insanity" (look at it 
with 13-years old boy eyes ;-) ). Do you like to say me, that this is 
not a standard "function" neither in tkinter, not say gtk or the others, 
too?

I would expect something like this:

def onKeyTouch(event):
    print (event.keysymAll)
   
root.bind("<KeyPress>", onKeyTouch)

and all the pressed keys are printed ....all the functions OnKeyPress, 
OnKeyRelease, move, even set pressedKeys are in onKeyTouch....


P.




More information about the Python-list mailing list