From the docs, I can see that the input context will send one of the following methods to the text view.
insertText:replacementRange:
setMarkedText:selectedRange:replacementRange:
doCommandBySelector:
In my testcase, when enabled an input method, keyDown
can trigger setMarkedText:selectedRange:replacementRange
, but if I keep deleting markedText
until the last character, then the input method will be deactivated without triggering any methods above.
Is there anything like a hook for the deactivation, so that I can do something immediately once input method deactivated?
I found the solution from Chromium.
When an input method enabled, user keep typing or deleting, with the IME window showing under the caret. NSTextInputClient
will send key event, and input context will invoke setMarkText
.
The problem is, when user delete the last character of the markedText
, IME window disappearing, key event is sent and input context won't invoke setMarkText
. As a result, markedText
won't be cleared either (which should be an empty string as I expected).
Chromium's solution is very straight forward. Just clear the markedText
every time when keyDown
, and let setMarkText
do the work. If markedText
is empty, meaning input context deactivated input method and didn't invoke setMarkText
.
Well, I think the API is somewhat inconsistent though...