我有以下代码(在它自己的文件/ script/ program中):
from msvcrt import getch
while True:
key = ord(getch())
print('You pressed', key)
我在PyCharm的控制台中运行了它,并按下了各种键,字母键、数字键和箭头键,但根本没有输出。
有什么问题吗,还是需要我提供更多信息?
谢谢!
发布于 2014-08-12 22:02:12
听起来这可能是PyCharm控制台的一个问题。
对于操作系统,命令行控制台(cmd.exe)调用getch()
并不总是那么简单。下面是我在自己的代码中使用的一些东西。还要注意评论中的链接。
# see http://msdn.microsoft.com/en-us/library/078sfkak
import msvcrt
def readch(echo=True):
"Get a single character on Windows."
while msvcrt.kbhit(): # clear out keyboard buffer
ch = msvcrt.getch()
if ch in '\x00\xe0': # arrow or function key prefix?
ch = msvcrt.getch() # second call returns the actual key code
ch = msvcrt.getch()
if ch in '\x00\xe0': # arrow or function key prefix?
ch = msvcrt.getch() # second call returns the actual key code
if echo:
msvcrt.putch(ch)
return ch
注意:您可能不需要初始的while
循环,这取决于您正在做什么。
https://stackoverflow.com/questions/25263853
复制相似问题