(In 2013) I don't know why Python is that weird, you can't find this by searching in google very easily, but it's quite simple.
How can I detect 'SPACE' or actually any key? How can I do this:
print('You pressed %s' % key)
This should be included in python core, so please do not link modules not related for core python.
Key input is a predefined event. You can catch events by attaching
event_sequence
(s) toevent_handle
(s) by using one or multiple of the existing binding methods(bind
,bind_class
,tag_bind
,bind_all
). In order to do that:event_handle
methodevent_sequence
) that fits your case from an events listWhen an event happens, all of those binding methods implicitly calls the
event_handle
method while passing anEvent
object, which includes information about specifics of the event that happened, as the argument.In order to detect the key input, one could first catch all the
'<KeyPress>'
or'<KeyRelease>'
events and then find out the particular key used by making use ofevent.keysym
attribute.Below is an example using
bind
to catch both'<KeyPress>'
and'<KeyRelease>'
events on a particular widget(root
):Use Tkinter there are a ton of tutorials online for this. basically, you can create events. Here is a link to a great site! This makes it easy to capture clicks. Also, if you are trying to make a game, Tkinter also has a GUI. Although, I wouldn't recommend Python for games at all, it could be a fun experiment. Good Luck!
You could make a little Tkinter app: