Just how do I make a food selection that does not call for the customer to push [enter] to select?
I've obtained a food selection in Python. That component was very easy. I'm making use of
raw_input() to get the option from the customer.
The trouble is that
raw_input (and also input) call for the customer to push Enter after they select. Exists any kind of means to make the program act when a keystroke? Below's what I've obtained until now:
import sys print """Menu 1) Say Foo 2) Say Bar""" answer = raw_input("Make a selection> ") if "1" in answer: print "foo" elif "2" in answer: print "bar"
It would certainly be wonderful to have something like
print menu while lastKey = "": lastKey = check_for_recent_keystrokes() if "1" in lastKey: #do stuff...
The factor msvcrt falls short in IDLE is due to the fact that IDLE is not accessing the collection that runs msvcrt. Whereas when you run the program natively in cmd.exe it functions perfectly. For the very same factor that your program impacts up on Mac and also Linux terminals.
Yet I presume if you're mosting likely to be utilizing this especially for windows, even more power to ya.
Wow, that took for life. Ok, below's what I've wound up with
#!C:\python25\python.exe import msvcrt print """Menu 1) Say Foo 2) Say Bar""" while 1: char = msvcrt.getch() if char == chr(27): #escape break if char == "1": print "foo" break if char == "2": print "Bar" break
It falls short tough making use of IDLE, the python ... point ... that features python. Once I attempted it in DOS (emergency room, CMD.exe ), as an actual program, after that it ran penalty.
No person attempt it in IDLE, unless you have Task Manager convenient.
I've currently neglected just how I coped with food selections that arn't super-instant receptive.
On Linux :
- set raw setting
- select and also read the keystroke
- recover regular setups
import sys import select import termios import tty def getkey(): old_settings = termios.tcgetattr(sys.stdin) tty.setraw(sys.stdin.fileno()) select.select([sys.stdin], , , 0) answer = sys.stdin.read(1) termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings) return answer print """Menu 1) Say Foo 2) Say Bar""" answer=getkey() if "1" in answer: print "foo" elif "2" in answer: print "bar"