我知道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