summaryrefslogtreecommitdiff
path: root/keyboardpython
diff options
context:
space:
mode:
authorWilliam Holland <william.holland@codethink.co.uk>2015-05-30 18:02:19 +0100
committerWilliam Holland <william.holland@codethink.co.uk>2015-05-30 18:02:19 +0100
commitbb2a7f1bb7b689d68bd55a8c359b4a1f9f3de2ff (patch)
treea8a881dec8e7660857b2e137283aabcc8ad28b36 /keyboardpython
parent8b05054950e269926f8394bd34a0a75617443635 (diff)
downloadkeyboard-python-bb2a7f1bb7b689d68bd55a8c359b4a1f9f3de2ff.tar.bz2
Fix multiple keypresses sometimes not working
Diffstat (limited to 'keyboardpython')
-rw-r--r--keyboardpython/__init__.py37
1 files changed, 26 insertions, 11 deletions
diff --git a/keyboardpython/__init__.py b/keyboardpython/__init__.py
index 50410b1..7034b14 100644
--- a/keyboardpython/__init__.py
+++ b/keyboardpython/__init__.py
@@ -3,32 +3,47 @@ from key import *
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
+ # VTIME tenths of a second elapses between bytes read
+ VTIME = 1
if fd == 'default': fd = sys.stdin.fileno()
- global before_tty
- before_tty = termios.tcgetattr(fd)
- 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)
+ global before_t
+ before_t = termios.tcgetattr(fd)
+ t = termios.tcgetattr(fd)
+ # turn echo and needing to press enter off
+ t[3] = t[3] & ~termios.ICANON & ~termios.ECHO
+ cc = t[-1]
+ cc[termios.VMIN] = VMIN
+ cc[termios.VTIME] = VTIME
+ termios.tcsetattr(fd, termios.TCSANOW, t)
def reset_tty(fd='default'):
''' set tty to how it was before set_to_read_key '''
import sys,termios
if fd == 'default': fd = sys.stdin.fileno()
- termios.tcsetattr(fd, termios.TCSANOW, before_tty)
+ termios.tcsetattr(fd, termios.TCSANOW, before_t)
-def getkey(fd='default',buffersize=6):
+def getkey(fd='default',timeout=40,buffersize=6):
''' Read keypress and return Key object.
- fd='default' will try and get this stdin.
+ - timeout in miliseconds
- buffersize is passed to os.read, 6 is
enough for alt+F12 for example.
'''
- import os,sys
+ import os,sys,time
if fd == 'default': fd = sys.stdin.fileno()
+ to_return = str()
+ timeout = float(timeout) / 1000
c = os.read(fd,buffersize)
- return key.parse_code(c)
+ endtime = time.time() + timeout
+ while c and time.time() < endtime:
+ to_return += c
+ c = os.read(fd,buffersize)
+ if to_return:
+ return key.parse_code(to_return)
+ else: return None
def queue_keypresses(q,signal,fd='default',buffersize=6):
''' Read keypress and put in Queue q while signal