文档管理中心
您当前正在浏览新版开发者文档中心,目录分类和层级有所调整。点击左侧当前文档分类名称前的“☰”图标,可切换文档分类。 了解新版目录
指南与API参考指南系统基础功能Input Kit(多模输入服务)输入设备开发指导

输入设备开发指导

场景介绍

输入设备管理提供设备热插拔监听、查询指定设备的键盘类型等能力。使用场景例如:当用户需要输入文本时,输入法会根据当前是否插入了物理键盘来决定是否弹出虚拟键盘,开发者可以通过监听设备热插拔判断是否有物理键盘插入。

导入模块

收起
自动换行
深色代码主题
复制
  1. import { inputDevice } from '@kit.InputKit';

接口说明

输入设备管理常用接口如下表所示,接口详细介绍请参考@ohos.multimodalInput.inputDevice (输入设备)。

展开
接口名称 描述
getDeviceList(): Promise<Array<number>> 获取所有输入设备的ID列表。
getKeyboardType(deviceId: number): Promise<KeyboardType> 获取输入设备的键盘类型。
on(type: 'change', listener: Callback<DeviceListener>): void 注册监听输入设备的热插拔事件。
off(type: 'change', listener?: Callback<DeviceListener>): void 取消监听输入设备的热插拔事件。

虚拟键盘弹出检测

当用户需要输入文本时,输入法会根据当前是否插入了物理键盘来决定是否弹出虚拟键盘,开发者可以通过监听设备热插拔,判断是否有物理键盘插入。

开发步骤

  1. 调用getDeviceList方法查询所有连接的输入设备,并遍历所有连接的设备,对每个设备调用getKeyboardType方法,判断是否有物理键盘,若有则标记已有物理键盘连接,该步骤确保在监听设备热插拔之前,先检测所有已插入的输入设备。
  2. 调用on接口监听输入设备热插拔事件,若监听到有物理键盘插入,则标记已有物理键盘连接;若监听到有物理键盘拔出,则标记没有物理键盘连接。
收起
自动换行
深色代码主题
复制
  1. import { inputDevice } from '@kit.InputKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. const DOMAIN = 0x0000;
  4. @Entry
  5. @Component
  6. struct Index {
  7. @State isPhysicalKeyboardExist: boolean = false;
  8. @State message: string = "Click to obtain the device list and monitor device hot-plug events";
  9. keyboards: Map<number, inputDevice.KeyboardType> = new Map();
  10. // ...
  11. build() {
  12. RelativeContainer() {
  13. Column() {
  14. // ...
  15. Text(this.message)
  16. .onClick(() => {
  17. try {
  18. // 1.获取设备列表,判断是否有物理键盘连接
  19. inputDevice.getDeviceList().then(data => {
  20. for (let i = 0; i < data.length; ++i) {
  21. inputDevice.getKeyboardType(data[i]).then(type => {
  22. if (type === inputDevice.KeyboardType.ALPHABETIC_KEYBOARD) {
  23. // 物理键盘已连接
  24. this.isPhysicalKeyboardExist = true;
  25. this.keyboards.set(data[i], type);
  26. }
  27. });
  28. }
  29. });
  30. // 2.监听设备热插拔
  31. inputDevice.on("change", (data) => {
  32. hilog.info(DOMAIN, 'InputDevice', `Device event info: %{public}s`, JSON.stringify(data));
  33. inputDevice.getKeyboardType(data.deviceId).then((type) => {
  34. hilog.info(DOMAIN, 'InputDevice', 'The keyboard type is: %{public}d', type);
  35. if (type === inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type === 'add') {
  36. // 物理键盘已插入
  37. this.isPhysicalKeyboardExist = true;
  38. this.keyboards.set(data.deviceId, type);
  39. }
  40. });
  41. if (this.keyboards.get(data.deviceId) === inputDevice.KeyboardType.ALPHABETIC_KEYBOARD &&
  42. data.type === 'remove') {
  43. // 物理键盘已拔出
  44. this.isPhysicalKeyboardExist = false;
  45. this.keyboards.delete(data.deviceId);
  46. }
  47. });
  48. this.message = "Device monitoring enabled successfully"
  49. } catch (error) {
  50. hilog.error(DOMAIN, 'InputDevice', `Execute failed, error: %{public}s`,
  51. JSON.stringify(error, ["code", "message"]));
  52. this.message = `Failed to enable device monitoring. Click to retry. Error message:${JSON.stringify(error,
  53. ["code", "message"])}`
  54. }
  55. })
  56. // ...
  57. }
  58. // ...
  59. }
  60. }
  61. }