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

[{'answer_id': 1840, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 3, 'selected': False, 'text': '<p><strong>On Linux:</strong></p>\n\n<ul>\n<li>set raw mode</li>\n<li>select and read the keystroke</li>\n<li>restore normal settings</li>\n</ul>\n\n<pre>\nimport sys\nimport select\nimport termios\nimport tty\n\ndef getkey():\n    old_settings = termios.tcgetattr(sys.stdin)\n    tty.setraw(sys.stdin.fileno())\n    select.select([sys.stdin], [], [], 0)\n    answer = sys.stdin.read(1)\n    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)\n    return answer\n\nprint """Menu\n1) Say Foo\n2) Say Bar"""\n\nanswer=getkey()\n\nif "1" in answer: print "foo"\nelif "2" in answer: print "bar"\n\n</pre>\n'}, {'answer_id': 1852, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 4, 'selected': True, 'text': '<p><strong>On Windows:</strong></p>\n\n<pre><code>import msvcrt\nanswer=msvcrt.getch()\n</code></pre>\n'}, {'answer_id': 1870, 'author': 'Grant', 'author_id': 30, 'author_profile': 'https://Stackoverflow.com/users/30', 'pm_score': 2, 'selected': False, 'text': '<p>Wow, that took forever. Ok, here\'s what I\'ve ended up with </p>\n\n<pre><code>#!C:\\python25\\python.exe\nimport msvcrt\nprint """Menu\n1) Say Foo \n2) Say Bar"""\nwhile 1:\n    char = msvcrt.getch()\n    if char == chr(27): #escape\n        break\n    if char == "1":\n        print "foo"\n        break\n    if char == "2":\n        print "Bar"\n        break\n</code></pre>\n\n<p>It fails hard using IDLE, the python...thing...that comes with python. But once I tried it in DOS (er, CMD.exe), as a real program, then it ran fine.</p>\n\n<p>No one try it in IDLE, unless you have Task Manager handy.</p>\n\n<p>I\'ve already forgotten how I lived with menus that arn\'t super-instant responsive.</p>\n'}, {'answer_id': 1885, 'author': 'helloandre', 'author_id': 50, 'author_profile': 'https://Stackoverflow.com/users/50', 'pm_score': 0, 'selected': False, 'text': "<p>The  reason msvcrt fails in IDLE is because IDLE is not accessing the library that runs msvcrt. Whereas when you run the program natively in cmd.exe it works nicely. For the same reason that your program blows up on Mac and Linux terminals.</p>\n\n<p>But I guess if you're going to be using this specifically for windows, more power to ya.</p>\n"}]
['https://Stackoverflow.com/questions/1829', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/30/']