我知道python getch()
适合检测单个按键。Python getch() - 多字符
是否有任何方法,我可以使用相同的函数,用于检测多个keystrokes.s,
此外,有可能的是,程序可以使等待它打印出在输出之前。
如:When I press 'w', the program must wait for another keystroke, 'a', before it prints the output for 'w'. I know this is workaround, but I think, as of now, this should do.
示例代码:
try:
from msvcrt import getch
print "I am Here"
except ImportError:
print "Hi"
def getch():
print "I am here!"
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def getchs():
while True:
yield getch()
for choice in getchs():
if choice == 'w':
print (80 * '-')
print ("You have chosen Orange...")
print ("Here's the nutritional fact of the Orange:")
print ("'One medium orange contains 1.23 grams of protein, 62 calories and 3.1 grams of dietary fiber.'")
print (80 * '-')
elif choice == 'a':
print (80 * '-')
print ("You have chosen Banana...")
print ("Here's the nutritional fact of the Banana:")
print ("'One medium banana contains 1.29 grams of protein, 105 calories and 3.1 grams of dietary fiber")
print (80 * '-')
现在这个完美的作品来检测 'W' 和'a'
如何将我纳入功能,具有组合:'wa'
,使用getch()
,而不是raw_input
我有搜索了这个,找不到。
另外,curses
模块有助于实现这个吗?
谢谢
难道Unix的工作?谢谢 – vamosrafa
最受欢迎。我已经更新了答案。请检查。 – MA1
谢谢,我会检查。 – vamosrafa