summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Holland <william.holland@codethink.co.uk>2015-06-02 19:57:15 +0100
committerWilliam Holland <william.holland@codethink.co.uk>2015-06-02 19:57:15 +0100
commitc1b20b404e014bfdb7f20b6f0d9715727a0f86ed (patch)
tree9c051dc7c4a3234b0da2d18f6afef756f69c772d
parent84c29c4f5bf596a34ac3f2d0844fdcdb894aed3a (diff)
downloadkeyboard-python-c1b20b404e014bfdb7f20b6f0d9715727a0f86ed.tar.bz2
Optimise non-escape code sequences
Don't wait for more input if not as escape code
-rw-r--r--keyboardpython/__init__.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/keyboardpython/__init__.py b/keyboardpython/__init__.py
index 2638595..74f112b 100644
--- a/keyboardpython/__init__.py
+++ b/keyboardpython/__init__.py
@@ -1,5 +1,7 @@
from key import *
+ESC = '\033'
+
def get_repeat():
''' try and get the repeat delay and rate from X '''
import subprocess
@@ -39,7 +41,7 @@ def set_to_read_key(fd='default'):
''' Set tty so that os.read gets one keypress at a time '''
import sys,termios
# VMIN min bytes read before returning from os.read()
- VMIN = 0 # 0 for pure timed read
+ VMIN = 1 # 0 for pure timed read
# VTIME tenths of a second elapses between bytes read
VTIME = 1
if fd == 'default': fd = sys.stdin.fileno()
@@ -59,7 +61,7 @@ def reset_tty(fd='default'):
if fd == 'default': fd = sys.stdin.fileno()
termios.tcsetattr(fd, termios.TCSANOW, before_t)
-def getkey(fd='default',timeout=32,buffersize=6):
+def getkey(fd='default',timeout=49,buffersize=6):
''' Read keypress and return Key object.
- fd='default' will try and get this stdin.
@@ -67,11 +69,17 @@ def getkey(fd='default',timeout=32,buffersize=6):
- buffersize is passed to os.read, 6 is
enough for alt+F12 for example.
'''
- import os,sys,time
+ import os,sys,time,termios
if fd == 'default': fd = sys.stdin.fileno()
to_return = str()
timeout = float(timeout) / 1000
c = os.read(fd,buffersize)
+ if c != ESC: return key.parse_code(c)
+ t = termios.tcgetattr(fd)
+ cc = t[-1]
+ cc[termios.VMIN] = 0
+ cc[termios.VTIME] = 1
+ termios.tcsetattr(fd, termios.TCSANOW, t)
endtime = time.time() + timeout
while c and time.time() < endtime:
to_return += c