def process_key_event(self, keyval, modifiers): if self.is_processable_key(keyval, modifiers): logger.debug("Key pressed: %c", chr(keyval)) logger.debug("Raw string: %s", self.raw_string) logger.debug("Old string: %s", self.editing_string) # Brace shift for TELEX's ][ keys. # When typing with capslock on, ][ won't get shifted to }{ resulting # in weird capitalization in "TưởNG". So we have to shift them # manually. keyval, brace_shift = self.do_brace_shift(keyval, modifiers) # Invoke BoGo to process the input new_string, self.raw_string = \ bogo.process_key(string=self.editing_string, key=chr(keyval), fallback_sequence=self.raw_string, config=self.config) # Revert the brace shift if brace_shift and new_string and new_string[-1] in "{}": logger.debug("Reverting brace shift") new_string = new_string[:-1] + \ chr(ord(new_string[-1]) - 0x20) logger.debug("New string: %s", new_string) self.update_composition(new_string) self.editing_string = new_string return True else: return False
def process_seq(self, seq): string = "" raw = string for i in seq: string, raw = bogo.process_key(string, i, fallback_sequence=raw) return string
def process_key_event(self, keyval, modifiers): last_action = self.last_action() editing_string = last_action["editing-string"] raw_string = last_action["raw-string"] if self.is_processable_key(keyval, modifiers): logger.debug("Key pressed: %c", chr(keyval)) logger.debug("Previous raw string: %s", raw_string) logger.debug("Previous editing string: %s", editing_string) # Brace shift for TELEX's ][ keys. # When typing with capslock on, ][ won't get shifted to }{ # resulting in weird capitalization in "TưởNG". So we have to # shift them manually. keyval, brace_shift = self.do_brace_shift(keyval, modifiers) # Invoke BoGo to process the input new_string, new_raw_string = \ bogo.process_key( string=editing_string, key=chr(keyval), fallback_sequence=raw_string, rules=self.config["input-method-definition"], skip_non_vietnamese=self.config["skip-non-vietnamese"]) # Revert the brace shift if brace_shift and new_string and new_string[-1] in "{}": logger.debug("Reverting brace shift") new_string = new_string[:-1] + \ chr(ord(new_string[-1]) - 0x20) logger.debug("New string: %s", new_string) self.update_composition( string=new_string, raw_string=new_raw_string) return True else: return self.on_special_key_pressed(keyval)
def process_key_event(self, keyval, modifiers): last_action = self.last_action() editing_string = last_action["editing-string"] raw_string = last_action["raw-string"] if self.is_processable_key(keyval, modifiers): logger.debug("Key pressed: %c", chr(keyval)) logger.debug("Previous raw string: %s", raw_string) logger.debug("Previous editing string: %s", editing_string) # Brace shift for TELEX's ][ keys. # When typing with capslock on, ][ won't get shifted to }{ # resulting in weird capitalization in "TưởNG". So we have to # shift them manually. keyval, brace_shift = self.do_brace_shift(keyval, modifiers) # Invoke BoGo to process the input new_string, new_raw_string = \ bogo.process_key( string=editing_string, key=chr(keyval), fallback_sequence=raw_string, rules=self.config["input-method-definition"], skip_non_vietnamese=self.config["skip-non-vietnamese"]) # Revert the brace shift if brace_shift and new_string and new_string[-1] in "{}": logger.debug("Reverting brace shift") new_string = new_string[:-1] + \ chr(ord(new_string[-1]) - 0x20) logger.debug("New string: %s", new_string) self.update_composition(string=new_string, raw_string=new_raw_string) return True else: return self.on_special_key_pressed(keyval)
def do_process_key_event(self, keyval, keycode, state): """Implement IBus.Engine's process_key_event default signal handler. Args: keyval - The keycode, transformed through a keymap, stays the same for every keyboard keycode - Keyboard-dependant key code state - The state of modifier keys like Shift, Control, etc. Return: True - if successfully process the keyevent False - otherwise This function gets called whenever a key is pressed. """ if self.is_in_unity == True: return False logging.debug("%s | %s | %s", keyval, keycode, state) # ignore key release events # is_press = ((state & IBus.ModifierType.RELEASE_MASK) == 0) is_press = (state & (1 << 30)) == 0 # There's a strange overflow bug with Python3-gi and IBus if not is_press: return False if keyval == IBus.Return: return self.on_return_pressed() if keyval in [IBus.Up, IBus.Down]: return self.on_updown_pressed(keyval) if keyval == keysyms.BackSpace: return self.on_backspace_pressed() if self.is_processable_key(keyval, state): # Process entered key here # self.__raw_string = self.__raw_string + chr(keyval) logging.debug("\nRaw string: %s" % self.__raw_string) case = 0 cap = state & IBus.ModifierType.LOCK_MASK shift = state & IBus.ModifierType.SHIFT_MASK if (cap or shift) and not (cap and shift): case = 1 logging.debug("case: %d", case) # logging.debug("keyval: %s", chr(keyval)) brace_shift = False if chr(keyval) in ['[', ']'] and case == 1: # This is for TELEX's ][ keys. # When typing with capslock on, ][ won't get shifted to }{ # so we have to shift them manually. keyval = keyval + 0x20 brace_shift = True logging.debug("Key pressed: %c", chr(keyval)) self.old_string = self.new_string logging.debug("Old string: %s", self.old_string) self.new_string, self.__raw_string = bogo.process_key(self.old_string, chr(keyval), fallback_sequence=self.__raw_string, config=self.__config) if self.__config['skip-non-vietnamese']: if not self.stubborn_old_string: self.stubborn_old_string = self.old_string else: self.stubborn_old_string = self.stubborn_new_string stubborn_config = dict(self.__config.items()) stubborn_config['skip-non-vietnamese'] = False self.stubborn_new_string = bogo.process_key(self.stubborn_old_string, chr(keyval), config=stubborn_config)[0] if self.stubborn_new_string != self.new_string: # The key sequence cannot generate a correct Vietnamese # word. But we will offer the user the incorrect word # as an option. self.lookup_table.clear() self.lookup_table.append_candidate( string_to_text(self.new_string)) self.lookup_table.append_candidate( string_to_text(self.stubborn_new_string)) # Don't worry, this is just for the lookup table to appear # at the correct position. No pre-editing is used. self.show_preedit_text() self.update_lookup_table(self.lookup_table, True) self.show_lookup_table() self.is_lookup_table_shown = True if brace_shift and self.new_string and self.new_string[-1] in "{}": logging.debug("Reverting brace shift") self.new_string = self.new_string[:-1] + \ chr(ord(self.new_string[-1]) - 0x20) logging.debug("New string: %s", self.new_string) self.commit_result(self.new_string) return True self.reset_engine() return False
def do_process_key_event(self, keyval, keycode, modifiers): """Implement IBus.Engine's process_key_event default signal handler. Args: keyval - The keycode, transformed through a keymap, stays the same for every keyboard keycode - Keyboard-dependant key code modifiers - The state of IBus.ModifierType keys like Shift, Control, etc. Return: True - if successfully process the keyevent False - otherwise This function gets called whenever a key is pressed. """ if is_in_unity() and is_in_unity_dash(): return False # Ignore key release events event_is_key_press = (modifiers & (1 << 30)) == 0 # There is a strange overflow bug with python3-gi here so the above # line is used instead # is_press = ((modifiers & IBus.ModifierType.RELEASE_MASK) == 0) if not event_is_key_press: return False if keyval == IBus.Return: return self.on_return_pressed() if keyval == IBus.BackSpace: return self.on_backspace_pressed() if keyval == IBus.space: if self.config["enable-text-expansion"]: expanded_string = self.abbr_expander.expand(self.new_string) self.commit_result(expanded_string) self.reset_engine() return False if self.is_processable_key(keyval, modifiers): logging.debug("Key pressed: %c", chr(keyval)) logging.debug("Raw string: %s", self.__raw_string) logging.debug("Old string: %s", self.old_string) # Brace shift for TELEX's ][ keys. # When typing with capslock on, ][ won't get shifted to }{ resulting # in weird capitalization in "TưởNG". So we have to shift them # manually. keyval, brace_shift = self.do_brace_shift(keyval, modifiers) # Call Bogo engine to process the input self.new_string, self.__raw_string = \ bogo.process_key(self.old_string, chr(keyval), fallback_sequence=self.__raw_string, config=self.config) # Revert the brace shift if brace_shift and self.new_string and self.new_string[-1] in "{}": logging.debug("Reverting brace shift") self.new_string = self.new_string[:-1] + \ chr(ord(self.new_string[-1]) - 0x20) logging.debug("New string: %s", self.new_string) self.commit_result(self.new_string) self.old_string = self.new_string return True self.reset_engine() return False
def OnTestKeyDown(self, this, input_context, virtual_key_code, key_info, out_eaten): logging.debug("OnTestKeyDown: %s", virtual_key_code) # out_eaten[0] = self.we_will_eat(virtual_key_code) self.input_context = input_context if virtual_key_code == VK_SPACE: self.reset() out_eaten[0] = False return if virtual_key_code == VK_BACK: # Logic copied from ibus-bogo if self.old_string != "": deleted_char = self.old_string[-1] self.old_string = self.old_string[:-1] self.raw_string = self.raw_string[:-1] if len(self.old_string) == 0: self.reset() else: index = self.raw_string.rfind(deleted_char) self.raw_string = self.raw_string[:-2] if index < 0 else \ self.raw_string[:index] + \ self.raw_string[(index + 1):] out_eaten[0] = False return if self.we_will_eat(virtual_key_code): # FIXME: Refactor the ToAscii code to a function/method keyboard_state = (ctypes.c_ubyte * 256)() if ctypes.windll.user32.GetKeyboardState(keyboard_state) == 0: ctypes.memset(keyboard_state, 0, 256) error = ctypes.windll.kernel32.GetLastError() logging.debug("GetKeyboardState() Error: %x", error) scan_code = (key_info >> 16) & 0xFF buff = ctypes.create_string_buffer(2) output = ctypes.windll.user32.ToAscii(virtual_key_code, scan_code, keyboard_state, buff, 0) logging.debug("ToAscii() - %s - %s", output, buff.value) logging.debug("CTRL: %s ALT: %s", keyboard_state[VK_CONTROL], keyboard_state[VK_MENU]) def is_key_down(key_state): return key_state & (1 << 7) != 0 if is_key_down(keyboard_state[VK_MENU]) or \ is_key_down(keyboard_state[VK_CONTROL]): self.reset() out_eaten[0] = False return new_string, raw_string = bogo.process_key(self.old_string, buff.value, self.raw_string) same_initial_chars = list(takewhile(unicode.__eq__, zip(self.old_string, self.new_string))) n_backspace = len(self.old_string) - len(same_initial_chars) self.delete_prev_chars(n_backspace) self.commit_text(new_string) self.old_string = new_string self.raw_string = raw_string out_eaten[0] = True else: out_eaten[0] = False