Skip to content

Instantly share code, notes, and snippets.

@acirtautas
Created August 7, 2015 14:03
Show Gist options
  • Save acirtautas/0c462ac785dc3fbf4a63 to your computer and use it in GitHub Desktop.
Save acirtautas/0c462ac785dc3fbf4a63 to your computer and use it in GitHub Desktop.
Python script to read 1-4 key presses and log it to the file, q to quit
import termios, tty, sys, time
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
tty.setcbreak(sys.stdin)
try:
print('Waiting for key pressed [1,2,3,4] or [q] to quit')
while True:
key = sys.stdin.read(1)
if key == '1' or key == '2' or key == '3' or key == '4':
now = time.strftime("%Y-%m-%d %H:%M:%S")
print(' ['+key + '] - key was pressed '+now)
f = open('keys.csv', 'a')
f.write(now+','+key + '\n')
f.closed
if key.lower() == 'q':
sys.exit(0)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
print('Bye.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment