我一直在寻找一段时间,现在遍布互联网,但我似乎找不到一个独特的方式为Linux和Windows,允许捕捉箭头上下,等事件。(Python3)Linux和Windows箭头键按下
随着msvcrt包在Windows上,我发现这个解决方案:
from msvcrt import getch
while True:
print ('Distance from zero: ' + str(pos))
key = ord(getch())
if key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
对于Linux我使用了termios包:
import sys,tty,termios
class _Getch:
def __call__(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(3)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def get():
inkey = _Getch()
while(1):
k=inkey()<