raw_input without pressing enter raw_input without pressing enter python python

raw_input without pressing enter


Under Windows, you need the msvcrt module, specifically, it seems from the way you describe your problem, the function msvcrt.getch:

Read a keypress and return the resulting character. Nothing is echoed to the console. This call will block if a keypress is not already available, but will not wait for Enter to be pressed.

(etc -- see the docs I just pointed to). For Unix, see e.g. this recipe for a simple way to build a similar getch function (see also several alternatives &c in the comment thread of that recipe).


Python does not provide a multiplatform solution out of the box.
If you are on Windows you could try msvcrt with:

import msvcrtprint 'Press s or n to continue:\n'input_char = msvcrt.getch()if input_char.upper() == 'S':    print 'YES'


Actually in the meantime (almost 10 years from the start of this thread) a cross-platform module named pynput appeared.Below a first cut - i.e. that works with lowercase 's' only.I have tested it on Windows but I am almost 100% positive that it should work on Linux.

from pynput import keyboardprint('Press s or n to continue:')with keyboard.Events() as events:    # Block for as much as possible    event = events.get(1e6)    if event.key == keyboard.KeyCode.from_char('s'):        print("YES")