sandbox causes the input method switch to fail to take effect

Dear Apple developers: Hello, recently I want to develop an application for macos that automatically switches input methods. The function is that when you switch applications, it can automatically switch to the input method you set, thus eliminating the trouble of manual switching. All the functions have been implemented, but only when the sandbox is closed. When I opened the sandbox, I found a very strange phenomenon. Suppose wechat was set to the Chinese input method. When I switched to wechat, wechat automatically got the focus of the input box. The input method icon in the upper right corner of the screen had actually switched successfully, but when I actually input, it was still the previous input method. If you switch to an application that does not have a built-in focus, the automatic switching of the input method will take effect when you click the input box with the mouse to regain the focus. This phenomenon is too difficult for my current technical level. I have tried many methods but none of them worked. I hope the respected experts can offer some ideas. Below is a snippet of the code switching I provided:

DispatchQueue. Main. AsyncAfter (deadline: now () + 0.1) {
let result = TISSelectInputSource(inputSource)
if result == noErr {
print(" Successfully switched to input method: \(targetInputMethod)")
} else {
print(" Input method switch failed. Error code: \(result)")
}

// Verify the switching result if let newInputSource = getCurrentInputSource() { print(" Switched input method: (newInputSource)") } }

When the sandbox is opened, the synchronous switching does not take effect. The input method icon in the status bar will flash for a moment, unable to compete with system events. Even if it is set to DispatchQueue.main.async, it still does not work. It seems that there is a timing issue with the input method switching.

Development environment

  • macOS version: 15.4.1
  • Xcode version: 16.2
sandbox causes the input method switch to fail to take effect
 
 
Q