Skip to content

Commit 2b7911e

Browse files
committed
user can toggle the IPA and translation with ctrl + ` key combination
1 parent 0fafb3b commit 2b7911e

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
5. 支持按英文单词的模糊音来输入。 如输入 `kerrage` 可以得到 `courage` 候选词,也可以输入 `aosome` 或者 `ausome` 来得 `awesome` 候选词。
1616
6. 具备 Text-Expander 功能。 本输入法会自动读取定义在用户目录下的`C:\Users\<user>\hallelujah.json` 文件,你可以定义自己常用的词组,比如 `{"yem":"you expand me"}`,那么当输入 `yem` 时会显示 `you expand me`
1717
7. 选词方式:数字键 1~9 及 `Enter` 回车键和 `Space` 空格键均可选词提交。`Space` 空格键选词默认会自动附加一个空格在单词后面。`Enter` 回车键选词则不会附加空格。
18+
8. User can toggle the IPA and translation with ctrl + ` key combination.
1819

1920
# 下载安装
2021

python/input_methods/hallelujah/ime_hallelujah.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ def loadFuzzySoundexEncodedData(self):
7979
class 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()

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.3.12
1+
1.3.13

0 commit comments

Comments
 (0)