summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Holland <william.holland@codethink.co.uk>2015-05-30 17:51:06 +0100
committerWilliam Holland <william.holland@codethink.co.uk>2015-05-30 17:51:06 +0100
commite88131d1cbb3893691a63add84070aa123d30f3e (patch)
tree3bc58273c09c377b71092280631549a2a50ea744
parent85c981e5122bcb53e5e4c9e822b3496313da16af (diff)
downloadkeyboard-python-e88131d1cbb3893691a63add84070aa123d30f3e.tar.bz2
Add KeyCombination object
This will allow for more complex keypresses, such as multiple alphabetic keys at once
-rw-r--r--keyboardpython/key.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/keyboardpython/key.py b/keyboardpython/key.py
index 89372af..3174e55 100644
--- a/keyboardpython/key.py
+++ b/keyboardpython/key.py
@@ -14,6 +14,64 @@ def _assert_type(thing,_type):
"%s is of %s, should be of %s" % (str(thing),type(thing),_type)
)
+class KeyCombination:
+ ''' multiple keyboard keys pressed in unison '''
+
+ def __init__(self,*keys):
+ for k in keys:
+ assert isinstance(k,Key)
+ self.keys = list(set(keys))
+ self.keys.sort(key=lambda x: x.name)
+
+ def __str__(self):
+ keys = self.keys
+ if not self.keys: return ''
+ to_print = keys.pop(0).name
+ while keys:
+ to_print += ' %s' % keys.pop(0).name
+ return to_print
+
+ def __eq__(self,other):
+ if not isinstance(other,KeyCombination): return False
+ return str(self) == str(other)
+
+ def __ne__(self,other):
+ return not self.__eq__(self,other)
+
+ def __hash__(self):
+ return hash(str(self))
+
+ def contains_key(self,key):
+ if key in self.keys: return True
+ return False
+
+ def add_key(self,_key):
+ assert isinstance(_key,Key)
+ if self.contains_key(_key): return
+ self.keys.append(_key)
+ self.keys.sort(key=lambda x: x.name)
+
+ def remove_key(self,key):
+ assert isinstance(_key,Key)
+ if key in self.keys:
+ key_index = self.keys.index(key)
+ self.keys.pop(key_index)
+
+ def add_keyCombination(self,key_comb):
+ assert isinstance(key_comb,KeyCombination)
+ for _key in key_comb.keys:
+ self.keys.append(_key)
+ self.keys.sort(key=lambda x: x.name)
+
+ def diff(self,key_comb):
+ assert isinstance(key_comb,KeyCombination)
+ to_return = KeyCombination()
+ for _key in key_comb.keys:
+ if not self.contains_key(_key):
+ to_return.keys.append(_key)
+ to_return.keys.sort(key=lambda x: x.name)
+ return to_return
+
class Key:
''' a keyboard keypress object '''