msvcrt
有一个方便的函数:^{
我有一个函数_Getch()
,比如:
def _Getch():
if sys.stdin.isatty():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
else:
return sys.stdin.read(1)
只有一个按键。你知道吗
当有人按下时,问题就来了:
ESC发送\x1b
。这是1字节:实际的转义字符。
向上翻页发送\x1b[H
。这是3个字节。
F2发送\x1b[OQ
。这是4个字节。
F5发送\x1b[15~
。这是5个字节。
看这是怎么回事?一旦读取了ESC,就无法预测以下序列将持续多久。你知道吗
随后的_Getch()
调用将获得这些字节,但问题是有多少个_Getch()
调用。你知道吗
我想定义一个如下函数,它将读取stdin
中等待的所有内容,直到什么都没有了:
def _Kbhit():
y = []
while msvcrt.kbhit(): # while something is waiting
y.append(msvcrt.getch()) # get it!
return y
以下是我的目标(from here)的Unix等价物:
def _Kbhit():
fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
return sys.stdin.read(waiting_buffer_len) # ?
我只是不知道如何定义waiting_buffer_len
。你知道吗
我已经搜索了所有相关的文档(tty
,termios
,sys.stdin
,fcntl
,os
),但是我找不到我要找的。你知道吗
多亏了this answer,我做了更多的搜索,而且不直观:
答案是
waiting_buffer_len
可以是任何长度:效果很好。你知道吗
相关问题 更多 >
编程相关推荐