@@ -79,8 +79,9 @@ def loadFuzzySoundexEncodedData(self):
7979class HallelujahTextService (TextService ):
8080 def __init__ (self , client ):
8181 TextService .__init__ (self , client )
82- # Only load and build the dict/trie once! 因为每次切换输入法都会导致HallelujahTextService出现初始化 。
82+ # Only load and build the dict/trie once! 因为每次切换输入法都会导致HallelujahTextService重新初始化 。
8383 self .__dict__ .update (imeService .__dict__ )
84+ self .showIPA = True
8485
8586 def onActivate (self ):
8687 TextService .onActivate (self )
@@ -91,9 +92,9 @@ def onDeactivate(self):
9192 self .db_connection .close ()
9293 TextService .onDeactivate (self )
9394
94- # 使用者按下按鍵 ,在 app 收到前先過濾那些鍵是輸入法需要的 。
95- # return True,系統會呼叫 onKeyDown() 進一步處理這個按鍵
96- # return False,表示我們不需要這個鍵,系統會原封不動把按鍵傳給應用程式
95+ # 使用者按下按键 ,在 app 收到前先处理输入法需要处理的 。
96+ # return True,系统会调用 onKeyDown() 进一步处理此按键。
97+ # return False,表示输入法不需要处理,系统会把事件传递给应用程序处理。
9798 def filterKeyDown (self , keyEvent ):
9899 # 使用者開始輸入,還沒送出前的編輯區內容稱 composition string
99100 # isComposing() 是 False,表示目前沒有正在編輯
@@ -105,9 +106,9 @@ def filterKeyDown(self, keyEvent):
105106 if keyEvent .isKeyDown (VK_MENU ):
106107 return False
107108
108- # 如果按下 Ctrl 鍵
109- if keyEvent .isKeyDown ( VK_CONTROL ) :
110- return False
109+ # 如果按下的是 Ctrl 鍵本身
110+ if keyEvent .keyCode == VK_CONTROL :
111+ return True
111112
112113 if keyEvent .isChar () and chr (keyEvent .charCode ).isalpha ():
113114 return True
@@ -170,14 +171,19 @@ def getCandidates(self, prefix):
170171
171172 candidateList2 = []
172173 if self .substitutions .get (input ):
173- candidateList2 .append (self .substitutions .get (input ))
174+ candidateList2 .append (self .substitutions .get (input )) #自定义短语优先级最高
174175 translations_and_ipa = self .get_translations_and_ipa (candidateList )
175176 for word in candidateList :
176177 translation , ipa = translations_and_ipa .get (word , ('' , '' ))
177178 ipa2 = f"{ [ipa ]} " if ipa else ' '
178179 word_ipa_translation = f"{ word } { ipa2 } { translation } "
180+ if not self .showIPA :
181+ word_ipa_translation = word
179182 if word .lower ().startswith (prefix .lower ()):
180- word_ipa_translation = f"{ prefix + word [len (prefix ):]} { ipa2 } { translation } "
183+ if self .showIPA :
184+ word_ipa_translation = f"{ prefix + word [len (prefix ):]} { ipa2 } { translation } "
185+ else :
186+ word_ipa_translation = f"{ prefix + word [len (prefix ):]} "
181187 candidateList2 .append (word_ipa_translation [0 :50 ])
182188
183189 return candidateList2
@@ -217,15 +223,20 @@ def getOutputFromCandidate(self, candidate):
217223 return word .strip ()
218224
219225 def onKeyDown (self , keyEvent ):
226+ # Check for Ctrl+` key combination (VK_OEM_3 is the virtual key code for `)
227+ if keyEvent .isKeyDown (VK_CONTROL ) and keyEvent .keyCode == VK_OEM_3 :
228+ logger .info ("Ctrl+` pressed" )
229+ self .showIPA = not self .showIPA
230+ self .inputWithCandidates (self .compositionString )
231+ return True # Key handled, prevent further processing
232+ if keyEvent .isKeyDown (VK_CONTROL ) and keyEvent .keyCode != VK_OEM_3 :
233+ return False
234+
220235 # print('halle keyEvent, charCode: ', keyEvent.charCode, '-- keyCode: ', keyEvent.keyCode)
221236 charStr = chr (keyEvent .charCode )
222-
237+
223238 # handle candidate selection
224239 if self .showCandidates :
225- if keyEvent .isKeyDown (VK_CONTROL ):
226- self .setCommitString (self .compositionString )
227- self .clear ()
228- return False
229240 if keyEvent .keyCode == VK_ESCAPE :
230241 self .setCommitString (self .compositionString )
231242 self .clear ()
0 commit comments