Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a menu that does not require the user to press [enter] to make a selection?

Tags:

python

I've got a menu in Python. That part was easy. I'm using raw_input() to get the selection from the user.

The problem is that raw_input (and input) require the user to press Enter after they make a selection. Is there any way to make the program act immediately upon a keystroke? Here's what I've got so far:

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 be great to have something like

print menu while lastKey = "":     lastKey = check_for_recent_keystrokes() if "1" in lastKey: #do stuff... 
like image 418
Grant Avatar asked Aug 05 '08 02:08

Grant


1 Answers

On Windows:

import msvcrt answer=msvcrt.getch() 
like image 51
Mark Harrison Avatar answered Sep 26 '22 20:09

Mark Harrison