1# @ohos.multimodalInput.inputDevice (输入设备) 2 3 4输入设备管理模块,用于监听输入设备连接和断开状态,查询输入设备相关信息。 5 6 7> **说明**: 8> 9> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10 11 12## 导入模块 13 14 15```js 16import { inputDevice } from '@kit.InputKit'; 17``` 18 19## inputDevice.getDeviceList<sup>9+</sup> 20 21getDeviceList(callback: AsyncCallback<Array<number>>): void 22 23获取所有输入设备的id列表,使用AsyncCallback异步方式返回结果。 24 25**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 26 27**参数**: 28 29| 参数名 | 类型 | 必填 | 说明 | 30| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 31| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回所有输入设备的id列表。 | 32 33**错误码**: 34 35以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 36 37| 错误码ID | 错误信息 | 38| ---- | --------------------- | 39| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 40 41**示例**: 42 43```js 44try { 45 inputDevice.getDeviceList((error: Error, ids: Array<Number>) => { 46 if (error) { 47 console.log(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`); 48 return; 49 } 50 console.log(`Device id list: ${JSON.stringify(ids)}`); 51 }); 52} catch (error) { 53 console.log(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`); 54} 55``` 56 57## inputDevice.getDeviceList<sup>9+</sup> 58 59getDeviceList(): Promise<Array<number>> 60 61获取所有输入设备的id列表,使用Promise异步方式返回结果。 62 63**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 64 65**返回值**: 66 67| 参数 | 说明 | 68| ---------------------------------- | ------------------------------------------- | 69| Promise<Array<number>> | Promise对象,异步返回所有输入设备的id列表。 | 70 71**示例**: 72 73```js 74try { 75 inputDevice.getDeviceList().then((ids: Array<Number>) => { 76 console.log(`Device id list: ${JSON.stringify(ids)}`); 77 }); 78} catch (error) { 79 console.log(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`); 80} 81``` 82 83## inputDevice.getDeviceInfo<sup>9+</sup> 84 85getDeviceInfo(deviceId: number, callback: AsyncCallback<InputDeviceData>): void 86 87获取指定输入设备的信息,使用AsyncCallback异步方式返回结果。 88 89**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 90 91**参数**: 92 93| 参数名 | 类型 | 必填 | 说明 | 94| -------- | -------------------------------------------------------- | ---- | --------------------------------------- | 95| deviceId | number | 是 | 输入设备id。 | 96| callback | AsyncCallback<[InputDeviceData](#inputdevicedata)> | 是 | 回调函数,异步返回输入设备信息。 | 97 98**错误码**: 99 100以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 101 102| 错误码ID | 错误信息 | 103| ---- | --------------------- | 104| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 105 106**示例**: 107 108```js 109// 获取输入设备id为1的设备信息。 110try { 111 inputDevice.getDeviceInfo(1, (error: Error, deviceData: inputDevice.InputDeviceData) => { 112 if (error) { 113 console.log(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`); 114 return; 115 } 116 console.log(`Device info: ${JSON.stringify(deviceData)}`); 117 }); 118} catch (error) { 119 console.log(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`); 120} 121``` 122 123## inputDevice.getDeviceInfo<sup>9+</sup> 124 125getDeviceInfo(deviceId: number): Promise<InputDeviceData> 126 127获取指定输入设备的信息,使用Promise异步方式返回结果。 128 129**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 130 131**参数**: 132 133| 参数名 | 类型 | 必填 | 说明 | 134| -------- | ------ | ---- | ---------------------- | 135| deviceId | number | 是 | 输入设备id。 | 136 137**返回值**: 138 139| 参数 | 说明 | 140| -------------------------------------------------- | ------------------------------- | 141| Promise<[InputDeviceData](#inputdevicedata)> | Promise对象,异步返回输入设备信息。 | 142 143**错误码**: 144 145以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 146 147| 错误码ID | 错误信息 | 148| ---- | --------------------- | 149| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 150 151**示例**: 152 153```js 154// 获取输入设备id为1的设备信息。 155try { 156 inputDevice.getDeviceInfo(1).then((deviceData: inputDevice.InputDeviceData) => { 157 console.log(`Device info: ${JSON.stringify(deviceData)}`); 158 }); 159} catch (error) { 160 console.log(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`); 161} 162``` 163 164## inputDevice.getDeviceInfoSync<sup>10+</sup> 165 166getDeviceInfoSync(deviceId: number): InputDeviceData 167 168获取指定输入设备的信息。 169 170**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 171 172**参数**: 173 174| 参数名 | 类型 | 必填 | 说明 | 175| -------- | ------ | ---- | ---------------------- | 176| deviceId | number | 是 | 输入设备id。 | 177 178**返回值**: 179 180| 参数 | 说明 | 181| -------------------------------------------------- | ------------------------------- | 182| [InputDeviceData](#inputdevicedata) | 返回输入设备信息。 | 183 184**错误码**: 185 186以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 187 188| 错误码ID | 错误信息 | 189| ---- | --------------------- | 190| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 191 192**示例**: 193 194```js 195// 获取输入设备id为1的设备信息。 196try { 197 let deviceData: inputDevice.InputDeviceData = inputDevice.getDeviceInfoSync(1) 198 console.log(`Device info: ${JSON.stringify(deviceData)}`) 199} catch (error) { 200 console.log(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`) 201} 202``` 203 204## inputDevice.on<sup>9+</sup> 205 206on(type: "change", listener: Callback<DeviceListener>): void 207 208监听输入设备的热插拔事件,使用时需连接鼠标键盘等外部设备。 209 210**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 211 212**参数**: 213 214| 参数名 | 类型 | 必填 | 说明 | 215| -------- | ---------------------------------------- | ---- | ----------- | 216| type | string | 是 | 输入设备的事件【鼠标、键盘、触摸屏等】类型。 | 217| listener | Callback<[DeviceListener](#devicelistener9)> | 是 | 回调函数,异步上报输入设备热插拔事件。 | 218 219**错误码**: 220 221以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 222 223| 错误码ID | 错误信息 | 224| ---- | --------------------- | 225| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 226 227**示例**: 228 229```js 230let isPhysicalKeyboardExist = true; 231try { 232 inputDevice.on("change", (data: inputDevice.DeviceListener) => { 233 console.log(`Device event info: ${JSON.stringify(data)}`); 234 inputDevice.getKeyboardType(data.deviceId, (err: Error, type: inputDevice.KeyboardType) => { 235 console.log("The keyboard type is: " + type); 236 if (type == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'add') { 237 // 监听物理键盘已连接。 238 isPhysicalKeyboardExist = true; 239 } else if (type == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'remove') { 240 // 监听物理键盘已断开。 241 isPhysicalKeyboardExist = false; 242 } 243 }); 244 }); 245 // 根据isPhysicalKeyboardExist的值决定软键盘是否弹出。 246} catch (error) { 247 console.log(`Get device info failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 248} 249``` 250 251## inputDevice.off<sup>9+</sup> 252 253off(type: "change", listener?: Callback<DeviceListener>): void 254 255取消监听输入设备的热插拔事件。在应用退出前调用,取消监听。 256 257**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 258 259**参数**: 260 261| 参数名 | 类型 | 必填 | 说明 | 262| -------- | ---------------------------------------- | ---- | ----------- | 263| type | string | 是 | 输入设备的事件【鼠标、键盘、触摸屏等】类型。 | 264| listener | Callback<[DeviceListener](#devicelistener9)> | 否 | 取消监听的回调函数。 | 265 266**错误码**: 267 268以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 269 270| 错误码ID | 错误信息 | 271| ---- | --------------------- | 272| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 273 274**示例**: 275 276```js 277function callback(data: inputDevice.DeviceListener) { 278 console.log(`Report device event info: ${JSON.stringify(data, [`type`, `deviceId`])}`); 279}; 280 281try { 282 inputDevice.on("change", callback); 283} catch (error) { 284 console.log(`Listen device event failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 285} 286 287// 取消指定的监听。 288try { 289 inputDevice.off("change", callback); 290} catch (error) { 291 console.log(`Cancel listening device event failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 292} 293 294// 取消所有监听。 295try { 296 inputDevice.off("change"); 297} catch (error) { 298 console.log(`Cancel all listening device event failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 299} 300``` 301 302## inputDevice.getDeviceIds<sup>(deprecated)</sup> 303 304getDeviceIds(callback: AsyncCallback<Array<number>>): void 305 306获取所有输入设备的id列表,使用AsyncCallback异步方式返回结果。 307 308> 从API version 9 开始不再维护,建议使用[inputDevice.getDeviceList](#inputdevicegetdevicelist9)代替。 309 310**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 311 312**参数**: 313 314| 参数名 | 类型 | 必填 | 说明 | 315| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 316| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回所有输入设备的id列表。 | 317 318**示例**: 319 320```js 321inputDevice.getDeviceIds((error: Error, ids: Array<Number>) => { 322 if (error) { 323 console.log(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`); 324 return; 325 } 326 console.log(`Device id list: ${JSON.stringify(ids)}`); 327}); 328``` 329 330## inputDevice.getDeviceIds<sup>(deprecated)</sup> 331 332getDeviceIds(): Promise<Array<number>> 333 334获取所有输入设备的id列表,使用Promise异步方式返回结果。 335 336> 从API version 9 开始不再维护,建议使用[inputDevice.getDeviceList](#inputdevicegetdevicelist9)代替。 337 338**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 339 340**返回值**: 341 342| 参数 | 说明 | 343| ---------------------------------- | ------------------------------------------- | 344| Promise<Array<number>> | Promise对象,异步返回所有输入设备的id列表。 | 345 346**示例**: 347 348```js 349inputDevice.getDeviceIds().then((ids: Array<Number>) => { 350 console.log(`Device id list: ${JSON.stringify(ids)}`); 351}); 352``` 353 354## inputDevice.getDevice<sup>(deprecated)</sup> 355 356getDevice(deviceId: number, callback: AsyncCallback<InputDeviceData>): void 357 358获取指定输入设备的信息,使用AsyncCallback异步方式返回结果。 359 360> 从API version 9 开始不再维护,建议使用[inputDevice.getDeviceInfo](#inputdevicegetdeviceinfo9)代替。 361 362**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 363 364**参数**: 365 366| 参数名 | 类型 | 必填 | 说明 | 367| -------- | -------------------------------------------------------- | ---- | -------------------------------- | 368| deviceId | number | 是 | 输入设备id。 | 369| callback | AsyncCallback<[InputDeviceData](#inputdevicedata)> | 是 | 回调函数,异步返回输入设备信息。 | 370 371**示例**: 372 373```js 374// 获取输入设备id为1的设备信息。 375inputDevice.getDevice(1, (error: Error, deviceData: inputDevice.InputDeviceData) => { 376 if (error) { 377 console.log(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`); 378 return; 379 } 380 console.log(`Device info: ${JSON.stringify(deviceData)}`); 381}); 382``` 383 384## inputDevice.getDevice<sup>(deprecated)</sup> 385 386getDevice(deviceId: number): Promise<InputDeviceData> 387 388获取指定输入设备的信息,使用Promise异步方式返回结果。 389 390> 从API version 9 开始不再维护,建议使用[inputDevice.getDeviceInfo](#inputdevicegetdeviceinfo9)代替。 391 392**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 393 394**参数**: 395 396| 参数名 | 类型 | 必填 | 说明 | 397| -------- | ------ | ---- | ------------ | 398| deviceId | number | 是 | 输入设备id。 | 399 400**返回值**: 401 402| 参数 | 说明 | 403| -------------------------------------------------- | ----------------------------------- | 404| Promise<[InputDeviceData](#inputdevicedata)> | Promise对象,异步返回输入设备信息。 | 405 406**示例**: 407 408```js 409// 获取输入设备id为1的设备信息。 410inputDevice.getDevice(1).then((deviceData: inputDevice.InputDeviceData) => { 411 console.log(`Device info: ${JSON.stringify(deviceData)}`); 412}); 413``` 414 415## inputDevice.supportKeys<sup>9+</sup> 416 417supportKeys(deviceId: number, keys: Array<KeyCode>, callback: AsyncCallback <Array<boolean>>): void 418 419获取输入设备是否支持指定的键码值,使用AsyncCallback异步方式返回结果。 420 421**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 422 423**参数**: 424 425| 参数名 | 类型 | 必填 | 说明 | 426| -------- | ----------------------------------------- | ---- | ------------------------------------------------------ | 427| deviceId | number | 是 | 输入设备id,同一个物理设备反复插拔,设备id会发生变化。 | 428| keys | Array[<KeyCode>](js-apis-keycode.md#keycode) | 是 | 需要查询的键码值,最多支持5个按键查询。 | 429| callback | AsyncCallback<Array<boolean>> | 是 | 回调函数,异步返回查询结果。 | 430 431**错误码**: 432 433以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 434 435| 错误码ID | 错误信息 | 436| ---- | --------------------- | 437| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 438 439**示例**: 440 441```js 442// 查询id为1的输入设备对于17、22和2055按键的支持情况。 443try { 444 inputDevice.supportKeys(1, [17, 22, 2055], (error: Error, supportResult: Array<Boolean>) => { 445 console.log(`Query result: ${JSON.stringify(supportResult)}`); 446 }); 447} catch (error) { 448 console.log(`Query failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 449} 450``` 451 452## inputDevice.supportKeys<sup>9+</sup> 453 454supportKeys(deviceId: number, keys: Array<KeyCode>): Promise<Array<boolean>> 455 456获取输入设备是否支持指定的键码值,使用Promise异步方式返回结果。 457 458**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 459 460**参数**: 461 462| 参数名 | 类型 | 必填 | 说明 | 463| -------- | -------------------- | ---- | ------------------------------------------------------ | 464| deviceId | number | 是 | 输入设备id,同一个物理设备反复插拔,设备id会发生变化。 | 465| keys | Array[<KeyCode>](js-apis-keycode.md#keycode) | 是 | 需要查询的键码值,最多支持5个按键查询。 | 466 467**返回值**: 468 469| 参数 | 说明 | 470| ----------------------------------- | ------------------------------- | 471| Promise<Array<boolean>> | Promise对象,异步返回查询结果。 | 472 473**错误码**: 474 475以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 476 477| 错误码ID | 错误信息 | 478| ---- | --------------------- | 479| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 480 481**示例**: 482 483```js 484// 查询id为1的输入设备对于17、22和2055按键的支持情况。 485try { 486 inputDevice.supportKeys(1, [17, 22, 2055]).then((supportResult: Array<Boolean>) => { 487 console.log(`Query result: ${JSON.stringify(supportResult)}`); 488 }); 489} catch (error) { 490 console.log(`Query failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 491} 492``` 493 494## inputDevice.supportKeysSync<sup>10+</sup> 495 496supportKeysSync(deviceId: number, keys: Array<KeyCode>): Array<boolean> 497 498获取输入设备是否支持指定的键码值。 499 500**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 501 502**参数**: 503 504| 参数名 | 类型 | 必填 | 说明 | 505| -------- | -------------------- | ---- | ------------------------------------------------------ | 506| deviceId | number | 是 | 输入设备id,同一个物理设备反复插拔,设备id会发生变化。 | 507| keys | Array[<KeyCode>](js-apis-keycode.md#keycode) | 是 | 需要查询的键码值,最多支持5个按键查询。 | 508 509**返回值**: 510 511| 参数 | 说明 | 512| ----------------------------------- | ------------------------------- | 513| Array<boolean> | 返回查询结果。true表示支持,false表示不支持。 | 514 515**错误码**: 516 517以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 518 519| 错误码ID | 错误信息 | 520| ---- | --------------------- | 521| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 522 523**示例**: 524 525```js 526// 查询id为1的输入设备对于17、22和2055按键的支持情况。 527try { 528 let supportResult: Array<Boolean> = inputDevice.supportKeysSync(1, [17, 22, 2055]) 529 console.log(`Query result: ${JSON.stringify(supportResult)}`) 530} catch (error) { 531 console.log(`Query failed, error: ${JSON.stringify(error, [`code`, `message`])}`) 532} 533``` 534 535## inputDevice.getKeyboardType<sup>9+</sup> 536 537getKeyboardType(deviceId: number, callback: AsyncCallback<KeyboardType>): void 538 539获取输入设备的键盘类型,使用AsyncCallback异步方式返回结果。 540 541**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 542 543**参数**: 544 545| 参数名 | 类型 | 必填 | 说明 | 546| -------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 547| deviceId | number | 是 | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 | 548| callback | AsyncCallback<[KeyboardType](#keyboardtype9)> | 是 | 回调函数,异步返回查询结果。 | 549 550**错误码**: 551 552以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 553 554| 错误码ID | 错误信息 | 555| ---- | --------------------- | 556| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 557 558**示例**: 559 560```js 561// 查询id为1的输入设备的键盘类型。 562try { 563 inputDevice.getKeyboardType(1, (error: Error, type: Number) => { 564 if (error) { 565 console.log(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`); 566 return; 567 } 568 console.log(`Keyboard type: ${JSON.stringify(type)}`); 569 }); 570} catch (error) { 571 console.log(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`); 572} 573``` 574 575## inputDevice.getKeyboardType<sup>9+</sup> 576 577getKeyboardType(deviceId: number): Promise<KeyboardType> 578 579获取输入设备的键盘类型,使用Promise异步方式返回结果。 580 581**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 582 583**参数**: 584 585| 参数名 | 类型 | 必填 | 说明 | 586| -------- | ------ | ---- | ------------------------------------------------------------ | 587| deviceId | number | 是 | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 | 588 589**返回值**: 590 591| 参数 | 说明 | 592| --------------------------------------------- | ------------------------------- | 593| Promise<[KeyboardType](#keyboardtype9)> | Promise对象,异步返回查询结果。 | 594 595**错误码**: 596 597以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 598 599| 错误码ID | 错误信息 | 600| ---- | --------------------- | 601| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 602 603**示例**: 604 605```js 606// 示例查询设备id为1的设备键盘类型。 607try { 608 inputDevice.getKeyboardType(1).then((type: Number) => { 609 console.log(`Keyboard type: ${JSON.stringify(type)}`); 610 }); 611} catch (error) { 612 console.log(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`); 613} 614``` 615 616## inputDevice.getKeyboardTypeSync<sup>10+</sup> 617 618getKeyboardTypeSync(deviceId: number): KeyboardType 619 620获取输入设备的键盘类型。 621 622**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 623 624**参数**: 625 626| 参数名 | 类型 | 必填 | 说明 | 627| -------- | ------ | ---- | ------------------------------------------------------------ | 628| deviceId | number | 是 | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 | 629 630**返回值**: 631 632| 参数 | 说明 | 633| --------------------------------------------- | ------------------------------- | 634| [KeyboardType](#keyboardtype9) | 返回查询结果。 | 635 636**错误码**: 637 638以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 639 640| 错误码ID | 错误信息 | 641| ---- | --------------------- | 642| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 643 644**示例**: 645 646```js 647// 示例查询设备id为1的设备键盘类型。 648try { 649 let type: number = inputDevice.getKeyboardTypeSync(1) 650 console.log(`Keyboard type: ${JSON.stringify(type)}`) 651} catch (error) { 652 console.log(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`) 653} 654``` 655 656## inputDevice.getIntervalSinceLastInput<sup>14+</sup> 657 658getIntervalSinceLastInput(): Promise<number> 659 660获取距离上次系统输入事件的时间间隔,使用Promise异步回调。 661 662**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 663 664**返回值**: 665 666| 参数 | 说明 | 667| --------------------------------------------- | ------------------------------- | 668| Promise<number> | Promise对象,异步返回获取的时间间隔,单位为微秒(μs)。| 669 670**示例**: 671 672```js 673 inputDevice.getIntervalSinceLastInput().then((timeInterval: number) => { 674 console.log(`Interval since last input: ${JSON.stringify(timeInterval)}`); 675 }); 676``` 677 678## DeviceListener<sup>9+</sup> 679 680输入设备热插拔的描述信息。 681 682**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 683 684| 名称 | 类型 | 可读 | 可写 | 说明 | 685| --------- | ------ | ---- | ---- | ------- | 686| type | [ChangedType](#changedtype9)| 是 | 否 | 输入设备插入或者移除。| 687| deviceId | number | 是 | 否 | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 | 688 689## InputDeviceData 690 691输入设备的描述信息。 692 693**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 694 695| 名称 | 类型 | 可读 | 可写 | 说明 | 696| --------- | ------ | ---- | ---- | ------- | 697| id | number | 是 | 否 | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 | 698| name | string | 是 | 否 | 输入设备的名字。 | 699| sources | Array<[SourceType](#sourcetype9)> | 是 | 否 | 输入设备支持的源类型。比如有的键盘上附带触摸板,则此设备有keyboard和touchpad两种输入源。 | 700| axisRanges | Array<[AxisRange](#axisrange)> | 是 | 否 | 输入设备的轴信息。 | 701| bus<sup>9+</sup> | number | 是 | 否 | 输入设备的总线类型。 | 702| product<sup>9+</sup> | number | 是 | 否 | 输入设备的产品信息。 | 703| vendor<sup>9+</sup> | number | 是 | 否 | 输入设备的厂商信息。 | 704| version<sup>9+</sup> | number | 是 | 否 | 输入设备的版本信息。 | 705| phys<sup>9+</sup> | string | 是 | 否 | 输入设备的物理地址。 | 706| uniq<sup>9+</sup> | string | 是 | 否 | 输入设备的唯一标识。 | 707 708## AxisType<sup>9+</sup> 709 710type AxisType = 'touchmajor' | 'touchminor' | 'orientation' | 'x' | 'y' | 'pressure' | 'toolminor' | 'toolmajor' | 'null' 711 712输入设备的轴类型。 713 714**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 715 716| 类型 |说明 | 717| --------- | ------- | 718| 'touchmajor' | 表示touchmajor轴。| 719| 'touchminor' | 表示touchminor轴。| 720| 'toolminor' | 表示toolminor轴。 | 721| 'toolmajor' | 表示toolmajor轴。 | 722| 'orientation' | 表示orientation轴。| 723|'pressure' | 表示pressure轴。 | 724| 'x' | 表示x轴。 | 725| 'y' | 表示y轴。 | 726|'null' | 无。 | 727 728## AxisRange 729 730输入设备的轴信息。 731 732**系统能力**: SystemCapability.MultimodalInput.Input.InputDevice 733 734| 名称 | 类型 | 可读 | 可写 | 说明 | 735| --------- | ------ | ---- | ---- | ------- | 736| source | [SourceType](#sourcetype9) | 是 | 否 | 轴的输入源类型。 | 737| axis | [AxisType](#axistype9) | 是 | 否 | 轴的类型。 | 738| max | number | 是 | 否 | 轴的最大值。 | 739| min | number | 是 | 否 | 轴的最小值。 | 740| fuzz<sup>9+</sup> | number | 是 | 否 | 轴的模糊值。 | 741| flat<sup>9+</sup> | number | 是 | 否 | 轴的基准值。 | 742| resolution<sup>9+</sup> | number | 是 | 否 | 轴的分辨率。 | 743 744## SourceType<sup>9+</sup> 745 746type SourceType = 'keyboard' | 'mouse' | 'touchpad' | 'touchscreen' | 'joystick' | 'trackball' 747 748轴的输入源类型。比如鼠标设备可上报x轴事件,则x轴的输入源就是鼠标。 749 750**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 751 752| 类型 |说明 | 753| --------- | ------- | 754| 'keyboard' | 表示输入设备是键盘。 | 755| 'touchscreen' | 表示输入设备是触摸屏。 | 756| 'mouse' | 表示输入设备是鼠标。 | 757| 'trackball' | 表示输入设备是轨迹球。 | 758| 'touchpad' | 表示输入设备是触摸板。 | 759| 'joystick' | 表示输入设备是操纵杆。 | 760 761## ChangedType<sup>9+</sup> 762 763type ChangedType = 'add' | 'remove' 764 765定义监听设备热插拔事件。 766 767**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 768 769| 类型 | 说明 | 770| --------- | ------- | 771| 'add' | 表示输入设备插入。 | 772| 'remove' | 表示输入设备移除。 | 773 774## KeyboardType<sup>9+</sup> 775 776定义键盘输入设备的类型。 777 778**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 779 780| 名称 | 值 | 说明 | 781| ------------------- | ---- | --------- | 782| NONE | 0 | 表示无按键设备。 | 783| UNKNOWN | 1 | 表示未知按键设备。 | 784| ALPHABETIC_KEYBOARD | 2 | 表示全键盘设备。 | 785| DIGITAL_KEYBOARD | 3 | 表示小键盘设备。 | 786| HANDWRITING_PEN | 4 | 表示手写笔设备。 | 787| REMOTE_CONTROL | 5 | 表示遥控器设备。 | 788