简体   繁体   English

如何判断Python是否按下了某个键?

[英]How can I tell if a certain key was pressed in Python?

import sys

print (sys.platform)
print (2 ** 100)
input('press Enter to exit')

Suppose I wanted to use the number 1 as the button that must be pressed to exit. 假设我想使用数字1作为必须按下以退出的按钮。 How would I go about doing this? 我将如何去做呢?

Something like this? 像这样吗

http://mail.python.org/pipermail/python-list/1999-October/014262.html http://mail.python.org/pipermail/python-list/1999-October/014262.html

Not so clean, but doable. 不是很干净,但是可行。

If you're building a command line app, why not use one of the libraries that help you build one. 如果要构建命令行应用程序,为什么不使用可帮助您构建一个库的库之一。

For example: 例如:

Something like this will do what you want: 这样的事情会做你想要的:

while(raw_input('Press "1" to exit.') != '1'):
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM