Skip to content

Instantly share code, notes, and snippets.

@dln
Created September 5, 2010 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dln/566113 to your computer and use it in GitHub Desktop.
Save dln/566113 to your computer and use it in GitHub Desktop.
Measure time between pressing 'space' key twice.
import sys
import termios
import time
import tty
def wait_space():
if sys.stdin.read(1) == ' ':
return time.time()
return wait_space()
def measure():
fd = sys.stdin.fileno()
attrs = termios.tcgetattr(fd)
print "Press space twice."
try:
tty.setraw(sys.stdin.fileno())
start = wait_space()
delta = wait_space() - start
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, attrs)
print "Time between presses: %0.04fs" % delta
if __name__ == '__main__':
measure()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment