首页 > 解决方案 > msvcrt.kbhit() 总是返回 false 。无法检测到 Escape 键

问题描述

我试过if (msvcrt.getch() == chr(27).encode())了,没用

我程序中的 msrcvt.getch() 总是在 debug-print 语句中打印到 b'\xff' 。

它根本没有检测到我正在按下的 ESC 键。如何使它工作。请提供 Python 3.8.x 的任何示例代码。

标签: python-3.xwindows-10

解决方案


尝试:如果 msvcrt.getch()==b'\x1b'

前任:

import msvcrt
while True:
    if msvcrt.kbhit():
        key_stroke = msvcrt.getch()
        if key_stroke==b'\x1b':
            print ("Esc key pressed")
        else:
            print (str(key_stroke).split("'")[1],"key pressed")

推荐阅读