Skip to content

Instantly share code, notes, and snippets.

@cvubrugier
Created April 22, 2015 13:15
Show Gist options
  • Save cvubrugier/2868ab3bc66e9ba5cc14 to your computer and use it in GitHub Desktop.
Save cvubrugier/2868ab3bc66e9ba5cc14 to your computer and use it in GitHub Desktop.
Ugly script that intercepts keypress on Linux with Python
#!/usr/bin/python
from __future__ import print_function
import sys
import termios
import tty
def main():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
r = sys.stdin.read(5)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
print('r = %r' % r)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment