I've been trying to program a "press any key to continue" function, simply because raw_input feels noobish, but so far I haven't been having much luck.

I did a search and the opinions are nearly unanimous that getch() is the way to go, but...things haven't gone that well. Here's an example of what happens:

from msvcrt import getch
while 1:
   keyPressed = getch()
   print keyPressed
   if keyPressed == "x":

The output of running this seemingly harmless program? An infinite loop, even after pressing "x". What's more, getch() seems to pick up the "enter" keystroke to execute this, long after I've lifted the key.

What this looks like to me --and I could be wrong-- is that keystrokes are stored in a stack or list of some sort, and the "Enter" keystroke is on the top of that. It seems that getch() reads the top entry, but *does not remove it*, causing that keystroke to be read for eternity.

I checked both the Python docs and the module docs for info on msvcrt to try to find a way to clear the "enter" keystroke and move on to the next one, but I found utterly nothing. At all.

Anyone know how it's done?

8 Years
Discussion Span
Last Post by FreezeBlink

Are you running it in something like IDLE because if so getch wont work. Try using the python command window.

yeah i just checked the code in the command window and it works a-okay! :)


Using raw_input() makes more sense and it is portable across the different OS and editor output windows.


getch in Python is not easy. One implementation found on the web uses termios

import termios, sys, os                                                         
TERMIOS = termios                                                               
def getkey():                                                                   
        fd = sys.stdin.fileno()                                                 
        old = termios.tcgetattr(fd)                                             
        new = termios.tcgetattr(fd)                                             
        new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO                       
        new[6][TERMIOS.VMIN] = 1                                                
        new[6][TERMIOS.VTIME] = 0                                               
        termios.tcsetattr(fd, TERMIOS.TCSANOW, new)                             
        c = None                                                                
                c = os.read(fd, 1)                                              
                termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)                   
        return c                                                                
if __name__ == '__main__':                                                      
        print 'type something'                                                  
        s = ''                                                                  
        while 1:                                                                
                c = getkey()                                                    
                if c == 'q':                                                    
                print 'got ("q" = end)', c                                      
                s = s + c                                                       
        print s

Are there any ways to do something of that sort which are not Unix-only?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.