@Entry
@Component
struct SearchExample {
controller: SearchController = new SearchController();
@State inputValue: string = '';
// 自定义键盘组件
@Builder
CustomKeyboardBuilder() {
Column() {
Grid() {
ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item: number | string) => {
GridItem() {
Button(item + '')
.width(110)
.onClick(() => {
this.inputValue += item;
});
};
});
}.maxCount(3).columnsGap(10).rowsGap(10).padding(5);
Button('搜索')
.onClick(() => {
// 关闭自定义键盘
this.controller.stopEditing();
// 执行搜索事件,此处弹窗事件替代搜索
this.getUIContext().getPromptAction().showToast({
message: '触发搜索功能,搜索:' + this.inputValue,
duration: 2000
});
});
}
.backgroundColor(Color.Gray);
}
build() {
Column() {
Search({ controller: this.controller, value: this.inputValue })
.customKeyboard(this.CustomKeyboardBuilder()) // 绑定自定义键盘
.margin(10)
.border({ width: 1 });
};
}
} 实现效果如下:

@Entry
@Component
struct Index {
controller: SearchController = new SearchController();
inputValue: string = '';
build() {
Column() {
Search({ controller: this.controller, value: $$this.inputValue })
.margin(10)
.border({ width: 1 })
.onSubmit((value: string) => {
// 关闭自定义键盘
this.controller.stopEditing();
// 执行搜索事件,此处用弹窗代替
this.getUIContext().getPromptAction().showToast({
message: '触发搜索功能,搜索:' + value,
duration: 2000
});
});
};
}
} 参考应用示例搜索功能修改如下:
public search(value: number): void {
if (this.textInputClient) {
try {
this.textInputClient.sendKeyFunction(value, (err: BusinessError, result: boolean) => {
if (err) {
console.error(`Failed to sendKeyFunction: ${JSON.stringify(err)}`);
return;
}
if (result) {
console.info('Succeeded in sending key function.');
} else {
console.error('Failed to sendKeyFunction.');
}
});
} catch (err) {
console.error(`Failed to sendKeyFunction: ${JSON.stringify(err)}`);
}
}
}
.onClick(() => {
keyboardController.search(3);
}); 实现效果如下:

注意:若场景二中三方输入法无法触发输入框的搜索事件,请检查sendKeyFunction中的参数是否是对应的功能键常量值。