def convertSequence(sequence, offset=0, dictionary=sequence2KeyPress): initSequence2KeyPress() if offset == len(sequence): current = dictionary.get(None, None) return (current, offset) current = dictionary.get(ord(sequence[offset]), None) if not current: if dictionary == sequence2KeyPress: # default characters k = KeyPress(char=sequence[offset]) return (k, offset + 1) # we are somewhere inside tree - stop here prev = dictionary.get(None, None) return (prev, offset) if isinstance(current, dict): res = convertSequence(sequence, offset=offset + 1, dictionary=current) if res[0] == None: if dictionary == sequence2KeyPress: # failed to resolve full sequence so provide input char by char k = KeyPress(char=sequence[offset]) return (k, offset + 1) else: # failed to convert with current prev = dictionary.get(None, None) return (prev, offset) else: return res else: return (current, offset + 1)
def initSequence2KeyPress(): global _sequence2KeyPressInitialized if _sequence2KeyPressInitialized: return for i in range(ord('z')+1-ord('a')): if not sequence2KeyPress.get(i+1, None): sequence2KeyPress[i+1] = KeyPress(char=chr(ord('a')+i), control=True) escapeDict = sequence2KeyPress[033] # fill rest of escape,printablecharacter as alt-chr for i in range(ord(' '), ord('~')+1): if not escapeDict.get(i, None): escapeDict[i] = KeyPress(char=chr(i), meta=True) _sequence2KeyPressInitialized = True
def char_to_keyinfo(char, control=False, meta=False, shift=False): k=KeyPress() vk = VkKeyScan(ord(char)) if vk & 0xffff == 0xffff: print u'VkKeyScan("%s") = %x' % (char, vk) raise ValueError, u'bad key' if vk & 0x100: k.shift = True if vk & 0x200: k.control = True if vk & 0x400: k.meta = True k.char=chr(vk & 0xff) return k
def make_KeyPress(char, state, keycode): shift = bool(int(state) & int(Shift)) control = bool(int(state) & int(Control)) meta = bool(int(state) & int(Alt)) keyname = code2sym_map.get(keycode, u"").lower() if control and meta: #equivalent to altgr so clear flags control = False meta = False elif control: char = str(keycode) return KeyPress(char, shift, control, meta, keyname)
def make_KeyPress(char, state, keycode): control = (state & (4 + 8)) != 0 meta = (state & (1 + 2)) != 0 shift = (state & 0x10) != 0 if control and char != "\x00": char = chr(VkKeyScan(ord(char)) & 0xFF) elif control: char = chr(keycode) try: keyname = code2sym_map[keycode] except KeyError: keyname = "" return KeyPress(char, shift, control, meta, keyname)
def make_KeyPress(char, state, keycode): control = (state & (4 + 8)) != 0 meta = (state & (1 + 2)) != 0 shift = (state & 0x10) != 0 if control and not meta: #Matches ctrl- chords should pass keycode as char char = chr(keycode) elif control and meta: #Matches alt gr and should just pass on char control = False meta = False try: keyname = code2sym_map[keycode] except KeyError: keyname = u"" out = KeyPress(char, shift, control, meta, keyname) return out
def char_to_keyinfo(char, control=False, meta=False, shift=False): k = KeyPress() vk = VkKeyScan(ord(char)) if vk & 0xFFFF == 0xFFFF: print 'VkKeyScan("%s") = %x' % (char, vk) raise ValueError, "bad key" if vk & 0x100: k.shift = True if vk & 0x200: k.control = True if vk & 0x400: k.meta = True k.char = chr(vk & 0xFF) return k
from common import validkey, KeyPress, make_KeyPress_from_keydescr import sys import select import re import os _sequence2KeyPressInitialized = False # sequences can be discovered with command: # showkey -k -a sequence2KeyPress = { 000: KeyPress(char=' ', control=True, keyname='space'), 011: KeyPress(char='\011', keyname="tab"), 015: KeyPress(char='\015', keyname="return"), 033: { None: KeyPress(char='\033', keyname="escape"), 040: KeyPress(char=' ', meta=True, keyname='space'), 0117: { 061: { 073: { 062: { 0120: KeyPress(shift=True, keyname='f1'), # gnome-terminal 0121: KeyPress(shift=True, keyname='f2'), # gnome-terminal 0122: KeyPress(shift=True, keyname='f3'), # gnome-terminal 0123: KeyPress(shift=True, keyname='f4'), # gnome-terminal }, },