summaryrefslogtreecommitdiff
path: root/key.py
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 /key.py
parent3b811b1bfbfd806473f106dc20f124008e566858 (diff)
downloadkeyboard-python-741be5feeb39972c6c7cde7f45c3b6646bfe1476.tar.bz2
Move into module folder and add __init__
Diffstat (limited to 'key.py')
-rw-r--r--key.py108
1 files changed, 0 insertions, 108 deletions
diff --git a/key.py b/key.py
deleted file mode 100644
index dc960c1..0000000
--- a/key.py
+++ /dev/null
@@ -1,108 +0,0 @@
-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)
-
-class Key:
- ''' a keyboard keypress object '''
-
- def __init__(self,name,*modifiers):
- assert_type(name,str)
- for m in modifiers:
- 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
- return str(self) == str(other)
-
- def __ne__(self,other):
- if isinstance(Key,other): return True
- return str(self) != str(other)
-
- def __str__(self):
- to_return = str()
- for m in self.modifiers:
- to_return += '%s ' % str(m)
- to_return += self.name
- return to_return
-
- def has_modifier(self,modifier):
- ''' does this object have the modifier? '''
- if modifier in self.modifiers:
- return True
- return False
-
- def modify(self,modifier):
- ''' add a modifier '''
- if modifier not in self.modifiers:
- self.modifiers.append(modifier)
- self.modifiers.sort()
- return self
-
-ALT = Key('Alt')
-CTRL = Key('Control')
-
-table = {
- ESC : Key('Escape'),
- '\t': Key('Tab'),
- ' ': Key('Space'),
- '\n': Key('Enter'),
- '\x7f' : Key('Backspace'),
- '%sOF' % ESC: Key('End'),
- '%sOH' % ESC: Key('Home'),
- '%s[3~' % ESC: Key('Del'),
- '%s[2~' % ESC: Key('Insert'),
- '%s[5~' % ESC: Key('PageUp'),
- '%s[6~' % ESC: Key('PageDown'),
- '%sOP' % ESC: Key('F1'),
- '%sOQ' % ESC: Key('F2'),
- '%sOR' % ESC: Key('F3'),
- '%sOS' % ESC: Key('F4'),
- '%s[15~' % ESC: Key('F5'),
- '%s[17~' % ESC: Key('F6'),
- '%s[18~' % ESC: Key('F7'),
- '%s[19~' % ESC: Key('F8'),
- '%s[20~' % ESC: Key('F9'),
- '%s[21~' % ESC: Key('F10'),
- '%s[23~' % ESC: Key('F11'),
- '%s[24~' % ESC: Key('F12'),
- '%s[A' % ESC: Key('Up'),
- '%s[B' % ESC: Key('Down'),
- '%s[D' % ESC: Key('Left'),
- '%s[C' % ESC: Key('Right'),
- }
-
-def is_alt(code):
- ''' is this the code of a combination modified by Alt? '''
- assert_type(name,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)
- if code < '\x20': return True
- return False
-
-def get_ctrl_alpha(code):
- ''' get alphabetic key modified by Ctrl '''
- assert_type(name,str)
- letter = chr(ord(code) + 0x40)
- return Key(letter,CTRL)
-
-def parse_code(code):
- ''' get Key object from code '''
- assert_type(name,str)
- if code in table.keys(): return table[code]
- elif is_alt(code): return from_keycode(code[1:]).modify(ALT)
- elif is_ctrl_alpha(code): return get_ctrl_alpha(code)
- else: return Key(code)