Over a million developers have joined DZone.

Cross Platform Getch() In Python

DZone's Guide to

Cross Platform Getch() In Python

Free Resource
For Unix, it uses sys, tty, termios modules.

import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
ch = sys.stdin.read(1)

For Windows, it uses msvcrt module.

import msvcrt
ch = msvcrt.getch()

See more details and OSX code in this recipe by Danny Yoo.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}