tkinter 3.0 multiple keyboard events together

janislaw wicijowski at gmail.com
Sat Dec 27 12:38:31 CET 2008


On 26 Gru, 17:44, Pavel Kosina <g... at post.cz> wrote:
> janislaw napsal(a):
>
> > On 26 Gru, 05:52, Pavel Kosina <g... at post.cz> wrote:
>
> >> Is it possible to catch in an event more that one key from keyboard? In
> >> my code, I can handle always the only one, the first I press, the others
> >> are omitted. Say, I press both "4" and "8" and only "4" is catched.
>
> >> def movePlayer(event):
> >>     print (event.keysym)
>
> > Each keypress triggers another event. Fortunately there are two types
> > of events: reaction to press and release. The logic to write to
> > recognize those as simultaneous clicks is up to you :)
>
> Might you give me a little bit more? Just a link to a site where this is
> explained and showed would be OK. I really did my best but everything is
> bad.
>
> geon

Use google to find the appropriate site, or browse this site, there
are plenty of examples. You may want to examine the code I wrote to
you to catch the idea:
#----------------------
import Tkinter
import pprint

tk = Tkinter.Tk()
f = Tkinter.Frame(tk, width=100, height=100)
msg = Tkinter.StringVar()
msg.set('Hello')
l = Tkinter.Label(f,  textvariable=msg)
l.pack()
f.pack()

keys = set()

def keyPressHandler(event):
    keys.add(event.char)
    display()

def keyReleaseHandler(event):
    keys.remove(event.char)
    display()

def display():
    msg.set(str(keys))

f.bind_all('<KeyPress>', keyPressHandler)
f.bind_all('<KeyRelease>', keyReleaseHandler)

f.mainloop()
#----------------

Regards,
JW



More information about the Python-list mailing list