How do I make a menu that does not require the user to press [enter] to make a selection?
<p>I've got a menu in Python. That part was easy. I'm using <code>raw_input()</code> to get the selection from the user. </p>

<p>The problem is that <code>raw_input</code> (and input) require the user to press <kbd>Enter</kbd> 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:</p>

<pre><code>import sys
print """Menu
1) Say Foo
2) Say Bar"""
answer = raw_input("Make a selection&gt; ")

if "1" in answer: print "foo"
elif "2" in answer: print "bar"
</code></pre>

<p>It would be great to have something like</p>

<pre><code>print menu
while lastKey = "":
    lastKey = check_for_recent_keystrokes()
if "1" in lastKey: #do stuff...
</code></pre>

<p><strong>On Linux:</strong></p>

<ul>
<li>set raw mode</li>
<li>select and read the keystroke</li>
<li>restore normal settings</li>
</ul>

<pre>
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"

</pre>
