Here is an example that allows you to keep backspace (keep in mind ASCII code for backspace is 127):
import curses
import time
window = curses.initscr()
curses.cbreak()
window.nodelay(True)
# Uncomment if you don't want to see the character you enter
# curses.noecho()
while True:
key = window.getch()
try:
if key not in [-1, 127]:
print key
except KeyboardInterrupt:
curses.echo()
curses.nocbreak() # Reset the program, so the prompt isn't messed up afterwards
curses.endwin()
raise SystemExit
finally:
try:
time.sleep(0.01)
except KeyboardInterrupt:
curses.echo()
curses.nocbreak() # Reset the program, so the prompt isn't messed up afterwards
curses.endwin()
raise SystemExit
finally:
pass
The key not in [-1, 127]
ignores printing 127 (ASCII DEL), or -1 (error). You can add other items into this, for other character codes.
The try/except/finally is for handling Ctrl-C. This resets the terminal so you don't get weird prompt output ater running.
Here is a link to the official Python docs, for future reference:
https://docs.python.org/2.7/library/curses.html#module-curses
I hope this helps.