msvcrt.kbhit() 总是返回 false .无法检测退出键 [英] msvcrt.kbhit() always returns false . Unable to detect Escape key

查看:50
本文介绍了msvcrt.kbhit() 总是返回 false .无法检测退出键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

The msrcvt.getch() in my program always prints to b'\xff' in the debug-print statement.

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

It simply didn't detects the ESC key I am pressing. How to make it work. Please provide any sample code for Python 3.8.x.

推荐答案

Try: if msvcrt.getch()==b'\x1b'

Try: if 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")

这篇关于msvcrt.kbhit() 总是返回 false .无法检测退出键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆