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.
NSTextInputClientwill send key event, and input context will invokesetMarkText.The problem is, when user delete the last character of the
markedText, IME window disappearing, key event is sent and input context won't invokesetMarkText. As a result,markedTextwon't be cleared either (which should be an empty string as I expected).Chromium's solution is very straight forward. Just clear the
markedTextevery time whenkeyDown, and letsetMarkTextdo the work. IfmarkedTextis empty, meaning input context deactivated input method and didn't invokesetMarkText.Well, I think the API is somewhat inconsistent though...