Before using an input method, its host application must have focus. You need to handle focus correctly to ensure the proper functioning of input methods.
For example, during application development, you can use setWindowFocusable to set the focusable property of the created window (such as floating window and auxiliary interaction window) to false, and draw an input box (such as TextInput, TextArea, or a custom edit box) in the window to launch the system keyboard for inputs.
When a window is set to be non-focusable via setWindowFocusable, the system imposes restrictions on it. Since the window is not focusable, input events (such as key information) cannot be correctly received and processed by the window. The input content cannot be synchronized to the input box in the window, resulting in abnormal interaction between the input box and the keyboard.
If you need to draw an input box in a non-focusable window and expect the input box to interact with the keyboard, you are advised to develop the input box in the following way (using TextInput as an example):
Create a subwindow in the main window and set the subwindow to be non-focusable.
Effect: When you tap the input component in the main window, the subwindow pops up, and the focus remains on the input box in the main window.
// NotFocusableWindowDemo.ets implements the layout of the main window.
import { window } from '@kit.ArkUI';
import { BusinessError } from '@ohos.base';
import Log from '../model/Log';
const TAG: string = 'NotFocusableWindowDemo->';
@Entry
@Component
struct NotFocusableWindowDemo {
private subWindow: window.Window | null = null;
async createSubWindow(): Promise<void> {
try {
// If the subwindow has already been created, show it again.
if (this.subWindow !== null) {
await this.subWindow.showWindow();
return;
}
// 1. Create a subwindow and set the subwindow ID.
let windowStage: window.WindowStage | undefined = AppStorage.get<window.WindowStage>('windowStage');
if (windowStage === undefined) {
Log.showError(TAG, 'Failed to get windowStage');
return;
}
let title: string | undefined = this.getUIContext().getHostContext()?.resourceManager.getStringSync($r('app.string.subwindow_title').id);
let options: window.SubWindowOptions = { title: title ? title : '', decorEnabled: true };
let subWindow: window.Window = await windowStage.createSubWindowWithOptions('mySubWindow', options);
this.subWindow = subWindow;
const subWindowId: number = subWindow.getWindowProperties().id;
AppStorage.setOrCreate('subWindowId', subWindowId);
// 2. Set the subwindow to be non-focusable. true allows focus, and false prevents it.
await subWindow.resize(500, 500);
await subWindow.setUIContent('pages/SubWindowIndex');
await subWindow.setWindowFocusable(false);
// 3. Display the subwindow.
await subWindow.showWindow();
Log.showInfo(TAG, 'Succeeded in creating and showing the subWindow.');
} catch (exception) {
const err = exception as BusinessError;
Log.showError(TAG, `Failed to create the subWindow. Cause code: ${err.code}, message: ${err.message}`);
}
}
async destroySubWindow(): Promise<void> {
try {
if (this.subWindow === null) {
return;
}
await this.subWindow.destroyWindow();
this.subWindow = null;
Log.showInfo(TAG, 'Succeeded in destroying the subWindow.');
} catch (exception) {
const err = exception as BusinessError;
Log.showError(TAG, `Failed to destroy the subWindow. Cause code: ${err.code}, message: ${err.message}`);
}
}
aboutToDisappear(): void {
// Destroy the subwindow when leaving the page so that it does not remain open.
this.destroySubWindow();
}
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text($r('app.string.not_focusable_window_title'))
.fontSize(18)
.fontWeight(500)
.margin(20)
TextInput({ placeholder: $r('app.string.subwindow_placeholder') })
.width('90%')
.height(48)
.margin(10)
.onClick(() => {
this.createSubWindow();
})
Button($r('app.string.subwindow_destroy'))
.width('90%')
.height(48)
.margin(10)
.onClick(() => {
this.destroySubWindow();
})
}
.width('100%')
.height('100%')
}
} When a user taps the input box component in the subwindow, you can set the subwindow to be focusable via setWindowFocusable, and then switch the focus from the main window to the subwindow using shiftAppWindowFocus. After this, the input method can be used normally in the input box of the subwindow.
async shiftFocusToSubWindow(): Promise<void> {
try {
let windowStage: window.WindowStage | undefined = AppStorage.get<window.WindowStage>('windowStage');
if (windowStage === undefined) {
Log.showError(TAG, 'Failed to get the windowStage.');
return;
}
let subWindowList: window.Window[] = await windowStage.getSubWindow();
let subWindow: window.Window = subWindowList[0];
// 1. Set the subwindow to be focusable.
await subWindow.setWindowFocusable(true);
// 2. Switch the focus to the subwindow.
const mainWindowId: number = AppStorage.get<number>('mainWindowId') ?? 0;
const subWindowId: number = AppStorage.get<number>('subWindowId') ?? 0;
await window.shiftAppWindowFocus(mainWindowId, subWindowId);
Log.showInfo(TAG, 'Succeeded in shifting focus to subWindow.');
} catch (exception) {
const err = exception as BusinessError;
Log.showError(TAG, `Failed to shift focus to subWindow. Cause code: ${err.code}, message: ${err.message}`);
}
} When the user taps a non-input component in the subwindow again, the subwindow can be reset to be non-focusable via setWindowFocusable, and the main window will be focused.
async shiftFocusToMainWindow(): Promise<void> {
try {
let windowStage: window.WindowStage | undefined = AppStorage.get<window.WindowStage>('windowStage');
if (windowStage === undefined) {
Log.showError(TAG, 'Failed to get the windowStage.');
return;
}
let subWindowList: window.Window[] = await windowStage.getSubWindow();
let subWindow: window.Window = subWindowList[0];
// Set the subwindow to be non-focusable. true allows focus, and false prevents it.
await subWindow.setWindowFocusable(false);
Log.showInfo(TAG, 'Succeeded in shifting focus to main window.');
} catch (exception) {
const err = exception as BusinessError;
Log.showError(TAG, `Failed to shift focus to main window. Cause code: ${err.code}, message: ${err.message}`);
}
}