检测 Python 中的按键输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17815686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect key input in Python
提问by Velocity-plus
(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.
(2013 年)我不知道为什么 Python 这么奇怪,你不能通过在 google 中很容易地搜索找到它,但它很简单。
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.
这应该包含在python核心中,所以请不要链接与核心python无关的模块。
采纳答案by unutbu
You could make a little Tkinter app:
你可以制作一个小 Tkinter 应用程序:
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()
回答by Praznav
Use Tkinter there are a ton of tutorials online for this. basically, you can create events. Here is a linkto 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!
使用 Tkinter 有大量的在线教程。基本上,您可以创建事件。这是一个很棒的网站的链接!这使得捕获点击变得容易。此外,如果您正在尝试制作游戏,Tkinter 也有一个 GUI。虽然,我根本不会推荐 Python 用于游戏,但它可能是一个有趣的实验。祝你好运!
回答by Nae
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:
按键输入是预定义的事件。您可以通过使用一种或多种现有绑定方法(、、、)将event_sequence
(s)附加到event_handle
(s)来捕获事件。为了做到这一点:bind
bind_class
tag_bind
bind_all
- define an
event_handle
method - pick an event(
event_sequence
) that fits your case from an events list
- 定义一个
event_handle
方法 event_sequence
从事件列表中选择适合您情况的事件()
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.
当一个事件发生时,所有这些绑定方法event_handle
在传递一个Event
对象的同时隐式调用该方法,该对象包括关于发生的事件的细节的信息,作为参数。
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.
为了检测按键输入,可以先捕捉所有的'<KeyPress>'
或'<KeyRelease>'
事件,然后利用event.keysym
属性找出使用的特定按键。
Below is an example using bind
to catch both '<KeyPress>'
and '<KeyRelease>'
events on a particular widget(root
):
下面是一个bind
用于在特定小部件()上捕获'<KeyPress>'
和'<KeyRelease>'
事件的示例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()