user3709398
user3709398

Reputation: 85

How do you map a keyboard key in Python 2.7?

I was wondering how do you map a key in python 2.7 to do something, like if you pressed 'a' (without having to hit enter), you would get a print statement. Thanks a bunch in advance!

Upvotes: 2

Views: 979

Answers (1)

ely
ely

Reputation: 77514

You could use Tkinter or PyQT or some other library that offers event handling. For example:

import Tkinter as tk

def key_press(event):
    print event.char
    return event.char
    # Or whatever processing you might want.

tk_app = tk.Tk()
tk_app.bind('<KeyPress>', key_press)
tk_app.mainloop()

Upvotes: 3

Related Questions