如何在Python中检测ESCape按键?
我正在命令窗口(Windows 7、Python 3.1)中运行一个进程,我希望用户通过按 Esc 键来中止该进程。但是,按 Esc 似乎没有执行任何操作,循环永远不会中断。我还尝试从 IDE (Wing) 中运行脚本,但同样,循环无法中断。
以下是我的概念验证测试的精简版本...
import msvcrt
import time
aborted = False
for time_remaining in range(10,0,-1):
# First of all, check if ESCape was pressed
if msvcrt.kbhit() and msvcrt.getch()==chr(27):
aborted = True
break
print(str(time_remaining)) # so I can see loop is working
time.sleep(1) # delay for 1 second
#endfor timing loop
if aborted:
print("Program was aborted")
else:
print("Program was not aborted")
time.sleep(5) # to see result in command window before it disappears!
如果有人能告诉我哪里可能出错,我将不胜感激。
I am running a process in a command window (Windows 7, Python 3.1) where I would like the user to abort the process by pressing Esc key. However, pressing Esc doesn't appear to do anything, the loop never breaks. I have also tried running the script from within my IDE (Wing), but again, the loop cannot be interrupted.
The following is a stripped-down version of my proof-of-concept test...
import msvcrt
import time
aborted = False
for time_remaining in range(10,0,-1):
# First of all, check if ESCape was pressed
if msvcrt.kbhit() and msvcrt.getch()==chr(27):
aborted = True
break
print(str(time_remaining)) # so I can see loop is working
time.sleep(1) # delay for 1 second
#endfor timing loop
if aborted:
print("Program was aborted")
else:
print("Program was not aborted")
time.sleep(5) # to see result in command window before it disappears!
If anyone could tell me where I might be going wrong I would be most grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Python 3 字符串是 unicode,因此必须编码为字节以进行比较。尝试这个测试:
或者这个测试:
或者这个测试:
Python 3 strings are unicode and, therefore, must be encoded to bytes for comparison. Try this test:
Or this test:
Or this test:
您确实应该精简更多内容,如下所示:
所以问题是:
msvcrt.getch()
返回字节
,chr(27)
返回字符串
。在 Python 3 中,它们是两种不同的类型,因此“==
”部分永远不会起作用,并且if
语句将始终被评估为False
。解决方案对您来说应该是显而易见的。
关于字符串与字节的更多,来自《Dive into Python 3》一书。
交互式控制台非常好用。对于调试很有用,请尝试更多地使用它:)
You should really strip down more, like this one below:
So here is the problem:
msvcrt.getch()
returnsbytes
,chr(27)
returnsstring
. In Python 3 they are two distinct types, so the "==
" part will never work, and theif
statement will always be evaluated asFalse
.The solution should be obvious to you.
More about strings vs bytes, from the book Dive into Python 3.
The interactive console is very useful for debugging, try use it more :)
您不需要编码,解码,chr,ord,...
或者如果您想在代码中的某处看到“27”:
You don't need encode, decode, chr, ord, ....
or if you'd like to see "27" in the code somewhere:
Python 2/3 兼容代码:
代码部分取自
pager< /code>
模块,里面有更多东西。
Python 2/3 compatible code:
Code parts are taken from
pager
module with more stuff inside.您是否尝试过使用不同的密钥来测试它是否不仅仅是该密钥?
您是否也尝试过此处的示例 看看它们是否有效?
Have you tried using a different key to test if it's not just that key?
Did you also try the examples here to see if they worked?
将 cv2 导入为 cv
a = cv.waitkey(0)
if (a == 27 或 ord('esc') == 27):
中止=真
休息
import cv2 as cv
a = cv.waitkey(0)
if (a == 27 or ord('esc') == 27):
aborted = True
break