Skip to content

Instantly share code, notes, and snippets.

@jasonrdsouza
Created February 24, 2012 15:54
Show Gist options
  • Select an option

  • Save jasonrdsouza/1901709 to your computer and use it in GitHub Desktop.

Select an option

Save jasonrdsouza/1901709 to your computer and use it in GitHub Desktop.
Python function to get keypresses from the terminal
def getchar():
#Returns a single character from standard input
import tty, termios, sys
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
while 1:
ch = getchar()
print 'You pressed', ch
@sfsdfd

sfsdfd commented Nov 19, 2017

Copy link
Copy Markdown

Good code. The only problem is that it traps everything - including control-Z, control-C, and anything else a user might invoke to force an interrupt signal.

@sternmotor

Copy link
Copy Markdown

insert following lines for easier testing ... now the loop breaks on every non-printable character

while 1:
    ch = getchar()
    if ch.strip() == '':
        print('bye!')
        break
    else:
        print 'You pressed', ch

@kirhgoff

kirhgoff commented Jan 3, 2018

Copy link
Copy Markdown

My problem that it requires user to press something, I am looking for a code to return nothing if nothing has been pressed

@awaiseye

awaiseye commented Feb 3, 2018

Copy link
Copy Markdown

ImportError: No module named termios

@mvaganov

Copy link
Copy Markdown

To handle Ctrl+C, and the windows imports:

def getchar():
	# Returns a single character from standard input
	import os
	ch = ''
	if os.name == 'nt': # how it works on windows
		import msvcrt
		ch = msvcrt.getch()
	else:
		import tty, termios, sys
		fd = sys.stdin.fileno()
		old_settings = termios.tcgetattr(fd)
		try:
			tty.setraw(sys.stdin.fileno())
			ch = sys.stdin.read(1)
		finally:
			termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
	if ord(ch) == 3: quit() # handle ctrl+C
	return ch

while 1:
	ch = getchar()
	print 'You pressed %c (%i)' % (ch, ord(ch))

@oname-15h

Copy link
Copy Markdown

Good code. The only problem is that it traps everything - including control-Z, control-C, and anything else a user might invoke to force an interrupt signal.

if you use
tty.setcbreak() instead of tty.setraw()
linux will catch the control characters(like ctrl+c) but will still pass through keystrokes

@daver1691

daver1691 commented Jun 22, 2022

Copy link
Copy Markdown

@oname-15h, I find that KeyboardInterrupt at tty.setcbreak() crashes the BASH shell for some reason.

If tty.setraw() is used as before, then its capturing of all control characters makes the "try" block redundant, but if we want a way to return to the shell from within the getchar() routine, then we can test for ordinal values of the control codes and call sys.exit() if found.

def getchar():
    import tty, termios, sys
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    tty.setraw(sys.stdin.fileno())
    ch = sys.stdin.read(1)
    termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    # Exit on ctrl-c, ctrl-d, ctrl-z, or ESC
    if ord(ch) in [3, 4, 26, 27]:
        sys.exit()
    return ch

@Vosjedev

Vosjedev commented Mar 2, 2023

Copy link
Copy Markdown

The best solution on the internet so far, good job.
@jasonrdsouza Could i use this in a public script?
Thanks!

@jerwinnnnn1212

Copy link
Copy Markdown

ty

@Lampe2020

Copy link
Copy Markdown

Wouldn't it be better to move the imports outside the function body? That way the modules don't get imported every time the function is run but instead just once and then reused for every call to the function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment