def _english_mode_process_key_event (self, key): # ignore if key code is not a normal ascii char if key.code >= 128: return False c = unichr (key.code) if ascii.ispunct (key.code): # if key code is a punctation if self._full_width_punct[self._mode]: self.commit_string (self._convert_to_full_width (c)) return True else: self.commit_string (c) return True if self._full_width_letter[self._mode]: # if key code is a letter or digit self.commit_string (self._convert_to_full_width (c)) return True else: self.commit_string (c) return True # should not reach there return False
def chinese_process_key_event (self, key): if self._editor.is_empty() and not self.get_extra_string(): if key.code <= 127 and ascii.ispunct (chr (key.code)): self.commit_string (self.convert_to_full_width (unichr (key.code))) return True elif key.code == KeyCode.KEY_r and key.mask == KeyMask.ControlMask: if not self.pipe: self.pipe = popen2.Popen3("python -c" +'"import gtk; print gtk.clipboard_get(selection=\\"PRIMARY\\").wait_for_text()"') return True else: return False #~ print unichr (key.code) if key.code in (KeyCode.KEY_Control_L,KeyCode.KEY_Control_R, KeyCode.KEY_Alt_L, KeyCode.KEY_Alt_R): return True elif key.code in (KeyCode.KEY_KP_Space, KeyCode.KEY_space): #~ print self._editor.get_candidate_cursor() if self._editor.candidates and self._editor.lookup_table.is_cursor_visible(): self._editor.select_cursor() return True elif self._editor.pinyinlist: self._editor.convert_all () return True elif self._editor.cursor < len (self._editor.wordlist): self._editor.jump_to_next_word() return True else: self.commit_string (self._editor.commit()) return True elif key.code == KeyCode.KEY_BackSpace: if not self._editor.pinyinlist and self.get_extra_string(): raise InputException() self._editor.del_current() return True elif key.code == KeyCode.KEY_Delete: if self._editor.lookup_table.is_cursor_visible(): self._editor.delete_cursor_phrase () else: self._editor.del_next () return True elif key.code >= KeyCode.KEY_0 and key.code <= KeyCode.KEY_9 and key.mask & KeyMask.ControlMask: self._editor.delete_phrase (key.code - KeyCode.KEY_1) return True elif key.code >= KeyCode.KEY_0 and key.code <= KeyCode.KEY_9 and key.mask & KeyMask.AltMask: self._editor.move_cursor_to (key.code - KeyCode.KEY_0) return True elif key.code >= KeyCode.KEY_1 and key.code <= KeyCode.KEY_9: self._editor.select (key.code-KeyCode.KEY_1) return True elif key.code >= KeyCode.KEY_KP_1 and key.code <= KeyCode.KEY_KP_9: self._editor.select (key.code-KeyCode.KEY_KP_1) return True elif key.code == KeyCode.KEY_Shift_L: if not self._editor.is_end(): self._editor.select (0) self._shift_key = None return True elif key.code == KeyCode.KEY_Shift_R: if not self._editor.is_end(): self._editor.select (1) self._shift_key = None return True elif key.code in (KeyCode.KEY_equal, KeyCode.KEY_bracketright, KeyCode.KEY_Page_Down): if self._editor.candidates: self._editor.lookup_table.page_down (); return True else: raise InputException() elif key.code in (KeyCode.KEY_minus, KeyCode.KEY_bracketleft, KeyCode.KEY_Page_Up): if self._editor.candidates: self._editor.lookup_table.page_up (); return True else: raise InputException() elif key.code==KeyCode.KEY_Up: if self._editor.candidates: self._editor.lookup_table.cursor_up() self._editor.lookup_table.show_cursor(True) return True else: raise InputException() elif key.code==KeyCode.KEY_Down: if self._editor.candidates: self._editor.lookup_table.cursor_down() self._editor.lookup_table.show_cursor(True) return True else: raise InputException() elif key.code == KeyCode.KEY_Left or key.code == KeyCode.KEY_b and key.mask & KeyMask.ControlMask: self._editor.move_cursor (-1) return True elif key.code == KeyCode.KEY_Right or key.code == KeyCode.KEY_f and key.mask & KeyMask.ControlMask: if self.get_extra_string(): raise InputException() self._editor.move_cursor (1) return True elif key.code == KeyCode.KEY_h and key.mask & KeyMask.ControlMask or key.code == KeyCode.KEY_Home: if self.get_extra_string(): raise InputException() self._editor.move_cursor_to (1) return True elif key.code == KeyCode.KEY_e and key.mask & KeyMask.ControlMask or key.code == KeyCode.KEY_End: if self.get_extra_string(): raise InputException() self._editor.move_cursor_to (0) return True elif key.code in (KeyCode.KEY_Return, KeyCode.KEY_KP_Enter): self.commit_string (self._editor.commit() + self.get_extra_string()) self.clear() return True elif key.code == KeyCode.KEY_Escape or key.code == KeyCode.KEY_c and key.mask & KeyMask.ControlMask: if self.origin_string: self.commit_string(self.origin_string) self._editor.clear() self.origin_string = None elif self._editor.lookup_table.is_cursor_visible(): self._editor.lookup_table.show_cursor(False) self._editor.update() else: self.clear() self._editor.clear() return True elif key.code <= 127 and ascii.ispunct (chr (key.code)) and not self.get_extra_string(): if not self._editor.is_empty (): self.commit_string (self._editor.commit ()) self.commit_string (self.convert_to_full_width (unichr (key.code))) return True else: raise InputException ()
def _chinese_mode_process_key_event (self, key): # define a condition half to full width translate functions cond_letter_translate = lambda (c): \ self._convert_to_full_width (c) if self._full_width_letter [self._mode] else c cond_punct_translate = lambda (c): \ self._convert_to_full_width (c) if self._full_width_punct [self._mode] else c if key.code in (KeyCode.KEY_Return, KeyCode.KEY_KP_Enter): if len (self._user_input) == 0: # forward Return if inputed chars is empty return False chars = map (cond_letter_translate, self._user_input.get_chars ()) commit_string = u"".join (chars) self.commit_string (commit_string) return True elif key.code == KeyCode.KEY_Escape: if len (self._user_input) != 0: self.reset () return True return False elif key.code == KeyCode.KEY_Down: return self.lookup_table_cursor_down () elif key.code == KeyCode.KEY_Up: return self.lookup_table_cursor_up () elif key.code == KeyCode.KEY_BackSpace: return self._pop_char () elif key.code >= KeyCode.KEY_1 and key.code <= KeyCode.KEY_9: if not self._candidates: self.commit_string (cond_letter_translate (unichr (key.code))) else: index = key.code - KeyCode.KEY_1 if index >= PinYinEngine._page_size: return True index += self._lookup_table.get_current_page_start () result = self._commit_candidate (index) if result: if self._committed_special_phrase: self.commit_string (self._committed_special_phrase) else: commit_phrases = self._committed_phrases.get_phrases () commit_string = self._committed_phrases.get_string () self.commit_string (commit_string + self._user_input.get_invalid_string ()) # adjust phrase freq and create new phrase self._pydb.commit_phrases (commit_phrases) if len (commit_phrases) > 1: self._pydb.new_phrase (commit_phrases) return True elif key.code in (KeyCode.KEY_KP_Space, KeyCode.KEY_space): if not self._candidates: self.commit_string (cond_letter_translate (u" ")) else: index = self._lookup_table.get_cursor_pos () result = self._commit_candidate (index) if result: if self._committed_special_phrase: self.commit_string (self._committed_special_phrase) else: commit_phrases = self._committed_phrases.get_phrases () commit_string = self._committed_phrases.get_string () self.commit_string (commit_string + self._user_input.get_invalid_string ()) # adjust phrase freq and create new phrase self._pydb.commit_phrases (commit_phrases) if len (commit_phrases) > 1: self._pydb.new_phrase (commit_phrases) return True elif key.code == KeyCode.KEY_Page_Down and self._candidates: # press PageDown self.lookup_table_page_down () return True elif key.code == KeyCode.KEY_equal and self._candidates and PinYinEngine._equal_page_down_up: # press equal self.lookup_table_page_down () return True elif key.code == KeyCode.KEY_period and self._candidates and PinYinEngine._comma_page_down_up: # press period self.lookup_table_page_down () return True elif key.code == KeyCode.KEY_Page_Up and self._candidates: # press PageUp self.lookup_table_page_up () return True elif key.code == KeyCode.KEY_minus and self._candidates and PinYinEngine._equal_page_down_up: # press minus self.lookup_table_page_up () return True elif key.code == KeyCode.KEY_comma and self._candidates and PinYinEngine._comma_page_down_up: #press comma self.lookup_table_page_up () return True elif key.code in (KeyCode.KEY_bracketleft, KeyCode.KEY_bracketright) and self._candidates: cursor_pos = self._lookup_table.get_cursor_pos () candidate = self._candidates[cursor_pos] if key.code == KeyCode.KEY_bracketleft: i = 0 else: i = len (candidate[PYSQLiteDB.PHRASE]) - 1 char = candidate[PYSQLiteDB.PHRASE][i] if i < 4: pinyin_id = candidate[PYSQLiteDB.Y0 + i] shengmu_id = candidate[PYSQLiteDB.S0 + i] else: pinyin = candidate[PYSQLiteDB.YX].split ("'")[-1] word = PYUtil.PinYinWord (pinyin) pinyin_id = word.get_pinyin_id () shengmu_id = word.get_sheng_mu_id () self._pydb.commit_char (char, pinyin_id, shengmu_id) self.commit_string (char) return True elif PinYinEngine._uv_to_temp and not PinYinEngine._shuangpin \ and len (self._user_input) == 0 \ and key.code in (KeyCode.KEY_v, KeyCode.KEY_u): self._user_input.append (unichr (key.code)) self._temp_english_mode = True self._update () return True elif key.code >= KeyCode.KEY_A and key.code <= KeyCode.KEY_Z and len (self._user_input) == 0: self._user_input.append (unichr (key.code)) self._temp_english_mode = True self._update () return True elif not PinYinEngine._shuangpin \ and len (self._user_input) == 0 \ and key.code == KeyCode.KEY_i: self._user_input.append (unichr (key.code)) self._i_mode = True self._update () return True elif (key.code >= KeyCode.KEY_a and key.code <= KeyCode.KEY_z) or \ (key.code == KeyCode.KEY_apostrophe and len (self._user_input) != 0) or \ (key.code == KeyCode.KEY_semicolon and len (self._user_input) != 0 and PinYinEngine._shuangpin) : return self._append_char (unichr (key.code)) elif key.code <= 127: if len (self._user_input) != 0: if PinYinEngine._auto_commit: self._chinese_mode_process_key_event (scim.KeyEvent (KeyCode.KEY_space, 0, key.mask)) else: return True c = chr (key.code) if c == "." and self._prev_char and self._prev_char.isdigit () \ and self._prev_key and chr (self._prev_key.code) == self._prev_char: self.commit_string (u".") elif ascii.ispunct (key.code): self.commit_string (cond_punct_translate (unichr (key.code))) else: self.commit_string (cond_letter_translate (unichr (key.code))) return True return False