summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Holland <william.holland@codethink.co.uk>2015-05-29 08:53:42 +0100
committerWilliam Holland <william.holland@codethink.co.uk>2015-05-29 08:53:42 +0100
commit6eada2a74f2fa175c658a36c55c8488cdbf04381 (patch)
tree09b9e5ceb14dc1b3c69c7064c90d1ccef205c54e
parent6f84025af8bd84308c2872d6129ab3c1cfb96a2a (diff)
downloadkeyboard-python-6eada2a74f2fa175c658a36c55c8488cdbf04381.tar.bz2
Add queue_keypresses function to handle everything
-rw-r--r--keyboardpython/__init__.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/keyboardpython/__init__.py b/keyboardpython/__init__.py
index 160a6b8..50410b1 100644
--- a/keyboardpython/__init__.py
+++ b/keyboardpython/__init__.py
@@ -29,3 +29,18 @@ def getkey(fd='default',buffersize=6):
if fd == 'default': fd = sys.stdin.fileno()
c = os.read(fd,buffersize)
return key.parse_code(c)
+
+def queue_keypresses(q,signal,fd='default',buffersize=6):
+ ''' Read keypress and put in Queue q while signal
+ is True, deals with setup and teardown.
+
+ - fd='default' will try and get this stdin.
+ - buffersize is passed to os.read, 6 is
+ enough for alt+F12 for example.
+ '''
+ import Queue
+ assert isinstance(q,Queue.Queue), "expected a Queue"
+ if signal: set_to_read_key(fd)
+ while signal:
+ if not q.full(): q.put(getkey(fd,buffersize))
+ reset_tty(fd)