def process_key_event(id_no, keyval, modmask, backslash, pressed = None): def update_keymap(): display._update_keymap(display.display.info.min_keycode, (display.display.info.max_keycode - display.display.info.min_keycode + 1)) def reply(id_no, handled): # editing = len() # imcontexts[id_no].editing how do i know current input status print_command('ibus_process_key_event_cb', id_no, handled) return False ic = imcontexts[id_no] if use_surrounding_text and pressed != False \ and ic.needs_surrounding_text() and not ic.surrouding_text_received: print_command('ibus_query_surrounding_text_cb', id_no, keyval, modmask, backslash, pressed) return if backslash: keycode = display.keysym_to_keycode(backslash) - 8 if keycode < 0: update_keymap() keycode = display.keysym_to_keycode(backslash) - 8 else: keycodes = display.keysym_to_keycodes(keyval) if not keycodes: update_keymap() keycodes = display.keysym_to_keycodes(keyval) or [(0, 0)] if modmask & modifier.SHIFT_MASK: for keycode_tuple in keycodes: if keycode_tuple[1] & 1: break else: keycode_tuple = keycodes[0] elif keyval < 0x100: for keycode_tuple in keycodes: if keycode_tuple[1]: if keycode_tuple[1] > 1: keycode_tuple = keycodes[0] break else: keycode_tuple = keycodes[0] if keycode_tuple[1] & 1: modmask |= modifier.SHIFT_MASK else: keycode_tuple = keycodes[0] keycode = keycode_tuple[0] - 8 if pressed != None: if not pressed: modmask |= modifier.RELEASE_MASK handled = ic.process_key_event(keyval, keycode, modmask) else: handled_p = ic.process_key_event(keyval, keycode, modmask) handled_r = ic.process_key_event(keyval, keycode, modmask | modifier.RELEASE_MASK) handled = handled_p or handled_r glib.idle_add(reply, id_no, handled)
def process_key_event(id_no, keyval, modmask, backslash, pressed=None): def update_keymap(): display._update_keymap(display.display.info.min_keycode, (display.display.info.max_keycode - display.display.info.min_keycode + 1)) def reply(id_no, handled): print_command('ibus_process_key_event_cb', id_no, handled) return False ic = imcontexts[id_no] if use_surrounding_text and pressed != False \ and ic.needs_surrounding_text() and not ic.surrouding_text_received: print_command('ibus_query_surrounding_text_cb', id_no, keyval, modmask, backslash, pressed) return if backslash: keycode = display.keysym_to_keycode(backslash) - 8 if keycode < 0: update_keymap() keycode = display.keysym_to_keycode(backslash) - 8 else: keycodes = display.keysym_to_keycodes(keyval) if not keycodes: update_keymap() keycodes = display.keysym_to_keycodes(keyval) or [(0, 0)] if modmask & modifier.SHIFT_MASK: for keycode_tuple in keycodes: if keycode_tuple[1] & 1: break else: keycode_tuple = keycodes[0] elif keyval < 0x100: for keycode_tuple in keycodes: if keycode_tuple[1]: if keycode_tuple[1] > 1: keycode_tuple = keycodes[0] break else: keycode_tuple = keycodes[0] if keycode_tuple[1] & 1: modmask |= modifier.SHIFT_MASK else: keycode_tuple = keycodes[0] keycode = keycode_tuple[0] - 8 if pressed != None: if not pressed: modmask |= modifier.RELEASE_MASK handled = ic.process_key_event(keyval, keycode, modmask) else: handled_p = ic.process_key_event(keyval, keycode, modmask) handled_r = ic.process_key_event(keyval, keycode, modmask | modifier.RELEASE_MASK) handled = handled_p or handled_r glib.idle_add(reply, id_no, handled)
def send_string(window, text):#{{{ """ Send a string to a window """ display = Xlib.display.Display() Xlib.XK.load_keysym_group("xkb") keysym_map = dict((Xlib.XK.keysym_to_string(x), x) for x in latin1.__dict__.values() if type(x) is int ) for char_index in range(len(text)): char = text[char_index] if char in keysym_map: key_sym = keysym_map[char] else: if char == "\t": char = "Tab" elif char == "\n": char = "Return" key_sym = Xlib.XK.string_to_keysym(char) if not key_sym: continue key_codes = display.keysym_to_keycodes(key_sym) if not key_codes: continue key_code, index = key_codes[0] m_state = 0 if index & 1: # Shift m_state = 1 if index & 4 | index & 2: # Alt Grid m_state = m_state | 0x80 for m_type in (Xlib.protocol.event.KeyPress, Xlib.protocol.event.KeyRelease): ev = m_type(time=0, child=0, state=m_state, root=window.query_tree().root, window=window, same_screen=1, \ root_x=0, root_y=0, event_x=0, event_y=1, detail=key_code) window.send_event(ev) display.sync()
def send_string(window, text): #{{{ """ Send a string to a window """ display = Xlib.display.Display() Xlib.XK.load_keysym_group("xkb") keysym_map = dict((Xlib.XK.keysym_to_string(x), x) for x in list(latin1.__dict__.values()) if type(x) is int) for char_index in range(len(text)): char = text[char_index] if char in keysym_map: key_sym = keysym_map[char] else: if char == "\t": char = "Tab" elif char == "\n": char = "Return" key_sym = Xlib.XK.string_to_keysym(char) if not key_sym: continue key_codes = list(display.keysym_to_keycodes(key_sym)) if not key_codes: continue key_code, index = key_codes[0] m_state = 0 if index & 1: # Shift m_state = 1 if index & 4 | index & 2: # Alt Grid m_state = m_state | 0x80 for m_type in (Xlib.protocol.event.KeyPress, Xlib.protocol.event.KeyRelease): ev = m_type(time=0, child=0, state=m_state, root=window.query_tree().root, window=window, same_screen=1, \ root_x=0, root_y=0, event_x=0, event_y=1, detail=key_code) window.send_event(ev) display.sync()