summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Holland <william.holland@codethink.co.uk>2015-05-30 17:56:48 +0100
committerWilliam Holland <william.holland@codethink.co.uk>2015-05-30 17:58:39 +0100
commite858e965416118f3917d1e65d3ef400f1bad7a3e (patch)
tree094955ba0bd12a77a257b6b0b5ce0b237e228b40
parent671bea46c4af3bf0a0e6440ccd22993f96c0b944 (diff)
downloadkeyboard-python-e858e965416118f3917d1e65d3ef400f1bad7a3e.tar.bz2
Make parsing use new class and tables
-rw-r--r--keyboardpython/key.py76
1 files changed, 69 insertions, 7 deletions
diff --git a/keyboardpython/key.py b/keyboardpython/key.py
index b0faeb9..2254602 100644
--- a/keyboardpython/key.py
+++ b/keyboardpython/key.py
@@ -190,20 +190,82 @@ def _is_alt(code):
def _is_ctrl_alpha(code):
''' is this the code of a combination modified by Ctrl? '''
+ import sys, termios
_assert_type(code,str)
- if code < '\x20': return True
- return False
+ fd = sys.stdin.fileno()
+ cc = termios.tcgetattr(fd)[-1]
+ if code in cc: return False
+ elif code in _special_chars.keys(): return False
+ elif code < '\x20': return True
+ else: return False
def _get_ctrl_alpha(code):
''' get alphabetic key modified by Ctrl '''
_assert_type(code,str)
letter = chr(ord(code) + 0x40)
- return Key(letter,CTRL)
+ alpha_key = Key(letter)
+ return KeyCombination(alpha_key,CTRL)
def parse_code(code):
''' get Key object from code '''
_assert_type(code,str)
- if code in _table.keys(): return _table[code]
- 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)
+ to_return = KeyCombination()
+ _code = list(code)
+ while _code:
+ if _code[0] == ESC:
+ if len(_code) == 1:
+ # TODO call getkey and add esc
+ key = _special_chars[ESC]
+ to_return.add_key(key)
+ _code.pop(0)
+ elif _code[1] == '[':
+ import re
+ __code = ''.join(_code[2:])
+ for c in _escape_brace_codes.keys():
+ match = re.search('^%s' % c,__code)
+ if match:
+ key = _escape_brace_codes[match.string]
+ to_return.add_key(key)
+ len_match = len(match.string)
+ _code = _code[len_match+2:]
+ break
+ else:
+ ___code = re.sub(';3~','~',__code)
+ match = re.search('^%s' % c,___code)
+ if match:
+ key = _escape_brace_codes[match.string]
+ to_return.add_key(key)
+ to_return.add_key(ALT)
+ len_match = len(match.string)
+ _code = _code[len_match+4:]
+ break
+
+ elif _code[1] == 'O':
+ if _code[2] == '1':
+ # alt F1 - alt F4
+ key = _escape_O_codes[_code[5]]
+ to_return.add_key(key)
+ to_return.add_key(ALT)
+ _code = _code[6:]
+ else:
+ key = _escape_O_codes[_code[2]]
+ to_return.add_key(key)
+ _code = _code[3:]
+ else:
+ to_return.add_key(ALT)
+ _code.pop(0)
+ elif _code[0].isalnum():
+ to_return.add_key(Key(_code[0]))
+ _code.pop(0)
+ elif _is_ctrl_alpha(_code[0]):
+ key_comb = _get_ctrl_alpha(_code[0])
+ to_return.add_keyCombination(key_comb)
+ _code.pop(0)
+ elif _code[0] in _special_chars.keys():
+ key = _special_chars[_code[0]]
+ to_return.add_key(key)
+ _code.pop(0)
+ else:
+ to_return.add_key(Key(_code[0]))
+ _code.pop(0)
+ return to_return