summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Holland <william.holland@codethink.co.uk>2015-05-28 23:24:08 +0100
committerWilliam Holland <william.holland@codethink.co.uk>2015-05-28 23:24:08 +0100
commit741be5feeb39972c6c7cde7f45c3b6646bfe1476 (patch)
tree72e5c098250bb716c810929719e02ba301025332
parent3b811b1bfbfd806473f106dc20f124008e566858 (diff)
downloadkeyboard-python-741be5feeb39972c6c7cde7f45c3b6646bfe1476.tar.bz2
Move into module folder and add __init__
-rw-r--r--keyboard-python.py1
-rw-r--r--keyboardpython/__init__.py20
-rw-r--r--keyboardpython/__init__.pycbin0 -> 942 bytes
-rw-r--r--keyboardpython/key.py (renamed from key.py)21
-rw-r--r--keyboardpython/key.pycbin0 -> 4299 bytes
5 files changed, 30 insertions, 12 deletions
diff --git a/keyboard-python.py b/keyboard-python.py
deleted file mode 100644
index 23f7e44..0000000
--- a/keyboard-python.py
+++ /dev/null
@@ -1 +0,0 @@
-import key
diff --git a/keyboardpython/__init__.py b/keyboardpython/__init__.py
new file mode 100644
index 0000000..6bfc0f5
--- /dev/null
+++ b/keyboardpython/__init__.py
@@ -0,0 +1,20 @@
+import key, Queue
+
+def getkey(fd='default',buffersize=6):
+ ''' Read keypress and return Key object.
+
+ - fd='default' will try and get this stdin.
+ - buffersize is passed to os.read, 6 is
+ enough for alt+F12 for example.
+ '''
+ import sys,os,termios
+ 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)
diff --git a/keyboardpython/__init__.pyc b/keyboardpython/__init__.pyc
new file mode 100644
index 0000000..05c5847
--- /dev/null
+++ b/keyboardpython/__init__.pyc
Binary files differ
diff --git a/key.py b/keyboardpython/key.py
index dc960c1..c1bf980 100644
--- a/key.py
+++ b/keyboardpython/key.py
@@ -3,9 +3,9 @@ ESC = '\x1b'
def assert_type(thing,_type):
''' if thing is not of type _type raise an exception '''
if type(thing) is not _type:
- raise TypeError,
- "%s is of %s, should be of %s" %
- (str(thing),type(thing),_type)
+ raise(TypeError,
+ "%s is of %s, should be of %s" % (str(thing),type(thing),_type)
+ )
class Key:
''' a keyboard keypress object '''
@@ -13,14 +13,13 @@ class Key:
def __init__(self,name,*modifiers):
assert_type(name,str)
for m in modifiers:
- assert isinstance(m,Key),
- "Modifiers expected to be instances of Key"
+ assert isinstance(m,Key), "Modifiers expected to be instances of Key"
self.name = name
self.modifiers = list(modifiers)
self.modifiers.sort()
def __eq__(self,other):
- if not isinstance(Key,other): return False
+ if not isinstance(other,Key): return False
return str(self) == str(other)
def __ne__(self,other):
@@ -82,27 +81,27 @@ table = {
def is_alt(code):
''' is this the code of a combination modified by Alt? '''
- assert_type(name,str)
+ assert_type(code,str)
if len(code) == 2 and code[0] == ESC:
return True
return False
def is_ctrl_alpha(code):
''' is this the code of a combination modified by Ctrl? '''
- assert_type(name,str)
+ assert_type(code,str)
if code < '\x20': return True
return False
def get_ctrl_alpha(code):
''' get alphabetic key modified by Ctrl '''
- assert_type(name,str)
+ assert_type(code,str)
letter = chr(ord(code) + 0x40)
return Key(letter,CTRL)
def parse_code(code):
''' get Key object from code '''
- assert_type(name,str)
+ assert_type(code,str)
if code in table.keys(): return table[code]
- elif is_alt(code): return from_keycode(code[1:]).modify(ALT)
+ elif is_alt(code): return parse_code(code[1:]).modify(ALT)
elif is_ctrl_alpha(code): return get_ctrl_alpha(code)
else: return Key(code)
diff --git a/keyboardpython/key.pyc b/keyboardpython/key.pyc
new file mode 100644
index 0000000..ad75f49
--- /dev/null
+++ b/keyboardpython/key.pyc
Binary files differ