Windows机器上的python的无缓冲字符输入 [英] Unbuffered character input for python on a windows machine
问题描述
我要做的事
我正在设计一个搭配时计的秒表。当您按L时,一圈已完成,当您按下S时,所有圈都完成,并且定时按顺序显示。
在C ++中,我可以使用conio.h中的_getch()函数来做到这一点。我想在python中编写这个程序,因为它会更容易一些,而且C ++中的时间处理也证明是很难的。
还是我在这个链接上写了一个程序(这是为多维数据集计时):
Cube timer
问题
没有像_getch()这样的函数。这是一个问题,因为在一圈的结束,你不能按下一个键和回车键,因为这将是时间和刺激的用户。
我阅读的内容
我读过curses库,无窗口。
我试过一个应该根据网站工作的程序。这是来自链接
但它无效。
我试过的:
-
msvcrt.getch code>
>>> import msvcrt
>> > msvcrt.getch()
'\xff'
我相信FF是十六进制的255.
我不明白为什么会发生这种情况。
-
readch()
在@martineau中建议
import msvcrt
def readch(echo = True):
在Windows上获取单个字符。
while msvcrt.kbhit():#清除键盘缓冲区
msvcrt.getch()
ch = msvcrt.getch()
while'in'\x00\xe0 ':#arrow或功能键前缀?
msvcrt.getch()
ch = msvcrt.getch()#第二次调用返回实际的关键代码
if echo:
msvcrt.putch(ch)
return ch.decode()
a = []
b a.append(readch())
我得到的错误:
>>>
回溯(最近一次调用):
文件C:/Python25/1.py,第30行,在< module>
a.append(readch())
文件C:/Python25/1.py,第25行,在readch中
return ch.decode()
UnicodeDecodeError: ascii'编解码器无法解码位置0中的字节0xff:序数不在范围内(128)
strong>需要帮助的
一个类似于_getch()或类似的在Windows机器上工作的函数。
$ b
机器规格
$ b $ p
Python IDLE 2.5.4或2.6或2.7
Windows XP SP3
在Windows控制台中工作。它有点类似于ActiveState配方,除了它只适用于Windows。它基于此 msdn 文档 _getwch()
。
## ## windows only ####
import msvcrt
def readch(echo = True):
在Windows上获取单个字符。
while msvcrt.kbhit():#清除键盘缓冲区
msvcrt.getwch()
ch = msvcrt.getwch()
如果ch在u'\x00 \ xe0':#arrow或功能键前缀?
ch = msvcrt.getwch()#第二次调用返回实际的关键代码
if echo:
msvcrt.putwch(ch)
return ch
def pause(prompt ='按任意键继续...'):
如果提示:
打印提示符,
readch(echo = False)
(已更新以处理Unicode)。
What I am trying to do
I am trying to design a stopwatch with lap timing. When you press "L" then a lap is completed and when you press "S" all laps are completed and the timings are displayed in order.
While in C++ I can do this with the function _getch() from conio.h and it would be quite easy. I want to write this program in python as it would be a lot more easier and also the time handling in C++ proved to be hard.
Still I did write a program (which was for cube timing) on this link: Cube timer
Problem
There is no function like _getch(). And this is a problem because at the end of a lap you can't press a key and an enter key because it would be time taking and irritating for the user.
things I read
I read about the curses library but alas it has no windows port.
I tried a program that was supposed to work according to the website. This was from the link
But it did not work.
What I tried:
msvcrt.getch()
>>> import msvcrt >>> msvcrt.getch() '\xff'
I believe that FF is the hexadecimal equivalent of 255.
I did not understand why this is happenning.
readch()
as sugggested in @martineauimport msvcrt
def readch(echo=True): "Get a single character on Windows." while msvcrt.kbhit(): # clear out keyboard buffer msvcrt.getch() ch = msvcrt.getch() while ch in '\x00\xe0': # arrow or function key prefix? msvcrt.getch() ch = msvcrt.getch() # second call returns the actual key code if echo: msvcrt.putch(ch) return ch.decode()
a = []
for i in range(10): a.append(readch())
The error that i got:
>>>
Traceback (most recent call last):
File "C:/Python25/1.py", line 30, in <module>
a.append(readch())
File "C:/Python25/1.py", line 25, in readch
return ch.decode()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
What I need help with
A function that works like _getch() or something equivalent that works on a windows machine.
Machine specs
Python IDLE 2.5.4 or 2.6 or 2.7
Windows XP SP3
This is what I've used that seems to work in a Windows console. It's somewhat similar to that ActiveState recipe except that it only works on Windows. It's based on this msdn documentation for _getwch()
.
#### windows only ####
import msvcrt
def readch(echo=True):
"Get a single character on Windows."
while msvcrt.kbhit(): # clear out keyboard buffer
msvcrt.getwch()
ch = msvcrt.getwch()
if ch in u'\x00\xe0': # arrow or function key prefix?
ch = msvcrt.getwch() # second call returns the actual key code
if echo:
msvcrt.putwch(ch)
return ch
def pause(prompt='Press any key to continue . . .'):
if prompt:
print prompt,
readch(echo=False)
(Updated to handle Unicode).
这篇关于Windows机器上的python的无缓冲字符输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!