summaryrefslogtreecommitdiff
path: root/keyboardpython
diff options
context:
space:
mode:
authorWilliam Holland <william.holland@codethink.co.uk>2015-05-29 00:26:55 +0100
committerWilliam Holland <william.holland@codethink.co.uk>2015-05-29 00:32:33 +0100
commit9cde71b2dd45bd8f30991c03927adac47b2cd6e1 (patch)
tree7bc4a5be35fa020f97ef7d3c1fdefc80bcc0bef7 /keyboardpython
parentdc4ffdd6054ca3ce20af0355bac673d66533eb1c (diff)
downloadkeyboard-python-9cde71b2dd45bd8f30991c03927adac47b2cd6e1.tar.bz2
Add set_to_read_key function
This is so that unnessary code does not need to be run each time getkey is called and to encourage the caller to reset the tty
Diffstat (limited to 'keyboardpython')
-rw-r--r--keyboardpython/__init__.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/keyboardpython/__init__.py b/keyboardpython/__init__.py
index 6cff951..3e42f08 100644
--- a/keyboardpython/__init__.py
+++ b/keyboardpython/__init__.py
@@ -1,5 +1,15 @@
import key
+def set_to_read_key(fd='default'):
+ ''' Set tty so that os.read gets one keypress at a time '''
+ import sys,termios
+ if fd == 'default': fd = sys.stdin.fileno()
+ tty = termios.tcgetattr(fd)
+ tty[3] = tty[3] & ~termios.ICANON & ~termios.ECHO
+ tty[6][termios.VMIN] = 1
+ tty[6][termios.VTIME] = 0
+ termios.tcsetattr(fd, termios.TCSANOW, tty)
+
def getkey(fd='default',buffersize=6):
''' Read keypress and return Key object.
@@ -7,14 +17,7 @@ def getkey(fd='default',buffersize=6):
- buffersize is passed to os.read, 6 is
enough for alt+F12 for example.
'''
- import sys,os,termios
+ import os,sys
if fd == 'default': fd = sys.stdin.fileno()
- tty = termios.tcgetattr(fd)
- # the next 4 lines are voodoo from stackoverflow, without them
- # os.read waits for enter key to return
- tty[3] = tty[3] & ~termios.ICANON & ~termios.ECHO
- tty[6][termios.VMIN] = 1
- tty[6][termios.VTIME] = 0
- termios.tcsetattr(fd, termios.TCSANOW, tty)
c = os.read(fd,buffersize)
return key.parse_code(c)