Keypress Input

Laura Creighton lac at openend.se
Thu Jun 4 06:50:37 EDT 2015


In a message of Wed, 03 Jun 2015 20:59:04 +0200, Laura Creighton writes:
>Tkinter runs on raspberry pi.
>
>Get it installed, and then run this program.
>
>from Tkinter import *
>root = Tk()
>prompt = 'Press any key. Remember to keep your mouse in the cyan box. '
>lab = Label(root, text=prompt, width=len(prompt), bg='cyan')
>lab.pack()
>
>def key(event):
>    msg = 'event.char is %r and event.keysym is %r' % (event.char, event.keysym)
>        lab.config(text=msg)
>
>root.bind_all('<Key>', key)
>root.mainloop() 
>
>Now you will have to bind the various keys to do what it is you want.
>You need to read online dociumentation for tkinter to learn how to do
>this, as well as how to use tkinter in general.
>
>Laura


I was in a hurry last night.  What I mean is -- Python out of the box
is not in the business of detecting key input.  You normally use a
Gui kit for that.  There are lots of them, and they all do things
sort of the same but slightly differently.  So what you need is
a gui kit that runs on Raspberry Pi.  If you are already using a
gui, then it probably will do it for you; check its documentation.
However, if you aren't using one ...

I don't have a Raspberry Pi, but I have read that tkinter runs there.
We can check.  First make sure that tkinter is installed and run that
short program I posted (also included above). If it works -- every time
you press a key char, it is supposed to tell you what you typed -- then
you are in business.  I just bound pressing every key with telling you that
it got pressed.  You will need to bind particular keys to do particular
things, which you can read about in tkinter documentation about keybinding.

Tkinter is python wrapped around tk, so this page
http://www.tcl.tk/man/tcl8.6/TkCmd/keysyms.htm

lists all the keys you can bind.  Not every OS provides the ability to
bind every key.  You are advised to try the keys you want to bind with
Raspberry Pi -- not having one I cannot check this for you.

Laura



More information about the Python-list mailing list