Skip to content

Instantly share code, notes, and snippets.

@h3ct0r
Created February 27, 2019 16:42
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 h3ct0r/ca8c33a20a89432e37c4f4c887d717fb to your computer and use it in GitHub Desktop.
Save h3ct0r/ca8c33a20a89432e37c4f4c887d717fb to your computer and use it in GitHub Desktop.
Python keyboard bindings with "key release"
#!/usr/bin/env python
import sys, select, termios, tty
def getKey():
tty.setraw(sys.stdin.fileno())
rfds, wfds, efds = select.select([sys.stdin], [], [], 0.3)
print(rfds, wfds, efds)
if len(rfds) > 0 or len(wfds) > 0 or len(efds) > 0:
key = sys.stdin.read(1)
# keyboard arrows are a 'triplet' (3 codes), so if the keycode
# begin with x1b, we asume the rest are the 2 remaining keycodes
if key == '\x1b':
key += sys.stdin.read(2)
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
return key
else:
return None
if __name__=="__main__":
settings = termios.tcgetattr(sys.stdin)
try:
while(1):
key = getKey()
if (key == '\x03'):
break
print("k", key)
except:
print e
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment