Home > Blockchain >  How to detect the candidate window when using Japanese keyboard?
How to detect the candidate window when using Japanese keyboard?

Time:09-12

My application is working similarly to Spotlight. When the app is activated, I intercept keys like arrows and return to do some operations and navigate through the list of suggestions. When the user hits the return button I assume that the selected item from the list has been chosen.

Unfortunately, the same keys are used by the Candidate Window when a user is using a Japanese, Korean, or Chinese keyboard. Therefore, I've got conflicts.

I need to distinguish whether a user is currently interacting with the Candidate Window or with my application. Therefore, I wonder if there is any way to detect that the Candidate Window is shown?

CodePudding user response:

NSInputClient.hasMarkedText returns whether there is any marked text, which is text waiting to be converted to be one of the options in the candidate window, or the user can just choose the marked text itself.

Having marked text is a necessary, but not sufficient condition for the candidate window to appear, and for your purposes of preventing accidental user inputs from interacting with your application, I think detecting the presence of marked text is enough, or an even better user experience, as someone who types Chinese and Japanese every day. When there is mark text on the screen, I do not expect that I am able to interact with anything else other than the text.

You can call hasMarkedText on a NSTextField by getting the field editor (which is a NSTextView) of the text view (credits to this answer):

if let editor = textField.window?.fieldEditor(false, for: textField) as? NSTextView, 
    editor.hasMarkedText() {
    // has marked text
} else {
    // no marked text
}
  • Related