18

(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.

Velocity-plus
  • 663
  • 1
  • 5
  • 19

5 Answers5

23

You could make a little Tkinter app:

import Tkinter as tk

def onKeyPress(event):
    text.insert('end', 'You pressed %s\n' % (event.char, ))

root = tk.Tk()
root.geometry('300x200')
text = tk.Text(root, background='black', foreground='white', font=('Comic Sans MS', 12))
text.pack()
root.bind('<KeyPress>', onKeyPress)
root.mainloop()
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 1
    does using Tk require a graphical window? I'm working on an embedded system. the sys.strin.read() below is tempting, but you need to press enter. – user391339 Nov 24 '14 at 05:40
  • 5
    @user391339: I think Tk would require a window system, or a virtual display. For an embedded system, a `getch` recipe such as [this one](http://stackoverflow.com/a/21659588/190597) might be better. – unutbu Nov 24 '14 at 09:59
3

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!

Praznav
  • 47
  • 1
2

Key input is a predefined event. You can catch events by attaching event_sequence(s) to event_handle(s) by using one or multiple of the existing binding methods(bind, bind_class, tag_bind, bind_all). In order to do that:

  1. define an event_handle method
  2. pick an event(event_sequence) that fits your case from an events list

When an event happens, all of those binding methods implicitly calls the event_handle method while passing an Event 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 of event.keysym attribute.

Below is an example using bind to catch both '<KeyPress>' and '<KeyRelease>' events on a particular widget(root):

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except ImportError:
    import Tkinter as tk


def event_handle(event):
    # Replace the window's title with event.type: input key
    root.title("{}: {}".format(str(event.type), event.keysym))


if __name__ == '__main__':
    root = tk.Tk()
    event_sequence = '<KeyPress>'
    root.bind(event_sequence, event_handle)
    root.bind('<KeyRelease>', event_handle)
    root.mainloop()
Nae
  • 14,209
  • 7
  • 52
  • 79
1

use the builtin: (no need for tkinter)

s = input('->>')
print(s) # what you just typed); now use if's 
if s == ' ':
 ...
droid192
  • 2,011
  • 26
  • 43
0

You can use pynput tutorial here for taking hint

Its very difficult to do without a module. But pynput is a core package.

Hecker
  • 21
  • 2