1# @ohos.multimodalInput.inputDevice (输入设备) 2 3<!--Kit: Input Kit--> 4<!--Subsystem: MultimodalInput--> 5<!--Owner: @zhaoxueyuan--> 6<!--Designer: @hanruofei--> 7<!--Tester: @Lyuxin--> 8<!--Adviser: @Brilliantry_Rui--> 9 10本模块提供输入设备管理能力,包括监听输入设备的连接和断开状态,查询设备名称等输入设备信息。 11 12 13> **说明**: 14> 15> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 16 17 18## 导入模块 19 20 21```js 22import { inputDevice } from '@kit.InputKit'; 23``` 24 25## inputDevice.getDeviceList<sup>9+</sup> 26 27getDeviceList(callback: AsyncCallback<Array<number>>): void 28 29获取所有输入设备的id列表,使用Callback回调。 30 31**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 32 33**参数**: 34 35| 参数名 | 类型 | 必填 | 说明 | 36| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 37| callback | AsyncCallback<Array<number>> | 是 | 回调函数,返回所有输入设备的id列表。id是输入设备的唯一标识。 | 38 39**错误码**: 40 41以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 42 43| 错误码ID | 错误信息 | 44| ---- | --------------------- | 45| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 46 47**示例**: 48 49```js 50import { inputDevice } from '@kit.InputKit'; 51 52@Entry 53@Component 54struct Index { 55 build() { 56 RelativeContainer() { 57 Text() 58 .onClick(() => { 59 try { 60 inputDevice.getDeviceList((error: Error, ids: Array<Number>) => { 61 if (error) { 62 console.error(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`); 63 return; 64 } 65 console.log(`Device id list: ${JSON.stringify(ids)}`); 66 }); 67 } catch (error) { 68 console.error(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`); 69 } 70 }) 71 } 72 } 73} 74``` 75 76## inputDevice.getDeviceList<sup>9+</sup> 77 78getDeviceList(): Promise<Array<number>> 79 80获取所有输入设备的id列表,使用Promise异步回调。 81 82**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 83 84**返回值**: 85 86| 类型 | 说明 | 87| ---------------------------------- | ------------------------------------------- | 88| Promise<Array<number>> | Promise对象,返回所有输入设备的id列表。 | 89 90**示例**: 91 92```js 93import { inputDevice } from '@kit.InputKit'; 94 95@Entry 96@Component 97struct Index { 98 build() { 99 RelativeContainer() { 100 Text() 101 .onClick(() => { 102 try { 103 inputDevice.getDeviceList().then((ids: Array<Number>) => { 104 console.log(`Device id list: ${JSON.stringify(ids)}`); 105 }); 106 } catch (error) { 107 console.error(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`); 108 } 109 }) 110 } 111 } 112} 113``` 114 115## inputDevice.getDeviceInfo<sup>9+</sup> 116 117getDeviceInfo(deviceId: number, callback: AsyncCallback<InputDeviceData>): void 118 119获取指定输入设备的信息,使用Callback异步回调。 120 121**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 122 123**参数**: 124 125| 参数名 | 类型 | 必填 | 说明 | 126| -------- | -------------------------------------------------------- | ---- | --------------------------------------- | 127| deviceId | number | 是 | 输入设备id。 | 128| callback | AsyncCallback<[InputDeviceData](#inputdevicedata)> | 是 | 回调函数。返回输入设备信息,包括输入设备id、名称、支持的输入能力、物理地址、版本信息及产品信息。 | 129 130**错误码**: 131 132以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 133 134| 错误码ID | 错误信息 | 135| ---- | --------------------- | 136| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 137 138**示例**: 139 140```js 141import { inputDevice } from '@kit.InputKit'; 142 143@Entry 144@Component 145struct Index { 146 build() { 147 RelativeContainer() { 148 Text() 149 .onClick(() => { 150 // 获取输入设备id为1的设备信息。 151 try { 152 inputDevice.getDeviceInfo(1, (error: Error, deviceData: inputDevice.InputDeviceData) => { 153 if (error) { 154 console.error(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`); 155 return; 156 } 157 console.log(`Device info: ${JSON.stringify(deviceData)}`); 158 }); 159 } catch (error) { 160 console.error(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`); 161 } 162 }) 163 } 164 } 165} 166``` 167 168## inputDevice.getDeviceInfo<sup>9+</sup> 169 170getDeviceInfo(deviceId: number): Promise<InputDeviceData> 171 172获取指定id的输入设备信息,使用Promise异步回调。 173 174**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 175 176**参数**: 177 178| 参数名 | 类型 | 必填 | 说明 | 179| -------- | ------ | ---- | ---------------------- | 180| deviceId | number | 是 | 输入设备id。 | 181 182**返回值**: 183 184| 类型 | 说明 | 185| -------------------------------------------------- | ------------------------------- | 186| Promise<[InputDeviceData](#inputdevicedata)> | Promise对象,返回输入设备信息。 | 187 188**错误码**: 189 190以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 191 192| 错误码ID | 错误信息 | 193| ---- | --------------------- | 194| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 195 196**示例**: 197 198```js 199import { inputDevice } from '@kit.InputKit'; 200 201@Entry 202@Component 203struct Index { 204 build() { 205 RelativeContainer() { 206 Text() 207 .onClick(() => { 208 // 获取输入设备id为1的设备信息。 209 try { 210 inputDevice.getDeviceInfo(1).then((deviceData: inputDevice.InputDeviceData) => { 211 console.log(`Device info: ${JSON.stringify(deviceData)}`); 212 }); 213 } catch (error) { 214 console.error(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`); 215 } 216 }) 217 } 218 } 219} 220``` 221 222## inputDevice.getDeviceInfoSync<sup>10+</sup> 223 224getDeviceInfoSync(deviceId: number): InputDeviceData 225 226获取指定输入设备的信息。 227 228**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 229 230**参数**: 231 232| 参数名 | 类型 | 必填 | 说明 | 233| -------- | ------ | ---- | ---------------------- | 234| deviceId | number | 是 | 输入设备id。 | 235 236**返回值**: 237 238| 类型 | 说明 | 239| -------------------------------------------------- | ------------------------------- | 240| [InputDeviceData](#inputdevicedata) | 返回输入设备信息,包括输入设备id、名称、支持的源类型、物理地址、版本信息及产品信息。 | 241 242**错误码**: 243 244以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 245 246| 错误码ID | 错误信息 | 247| ---- | --------------------- | 248| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 249 250**示例**: 251 252```js 253import { inputDevice } from '@kit.InputKit'; 254 255@Entry 256@Component 257struct Index { 258 build() { 259 RelativeContainer() { 260 Text() 261 .onClick(() => { 262 // 获取输入设备id为1的设备信息。 263 try { 264 let deviceData: inputDevice.InputDeviceData = inputDevice.getDeviceInfoSync(1); 265 console.log(`Device info: ${JSON.stringify(deviceData)}`); 266 } catch (error) { 267 console.error(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`); 268 } 269 }) 270 } 271 } 272} 273``` 274 275## inputDevice.on<sup>9+</sup> 276 277on(type: "change", listener: Callback<DeviceListener>): void 278 279注册监听输入设备的热插拔事件,使用时需连接鼠标、键盘、触摸屏等外部设备。 280 281**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 282 283**参数**: 284 285| 参数名 | 类型 | 必填 | 说明 | 286| -------- | ---------------------------------------- | ---- | ----------- | 287| type | string | 是 | 输入设备的事件类型,固定值为'change'。 | 288| listener | Callback<[DeviceListener](#devicelistener9)> | 是 | 回调函数,异步上报输入设备热插拔事件。 | 289 290**错误码**: 291 292以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 293 294| 错误码ID | 错误信息 | 295| ---- | --------------------- | 296| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 297 298**示例**: 299 300```js 301import { inputDevice } from '@kit.InputKit'; 302 303@Entry 304@Component 305struct Index { 306 build() { 307 RelativeContainer() { 308 Text() 309 .onClick(() => { 310 let isPhysicalKeyboardExist = true; 311 try { 312 inputDevice.on("change", (data: inputDevice.DeviceListener) => { 313 console.log(`Device event info: ${JSON.stringify(data)}`); 314 inputDevice.getKeyboardType(data.deviceId, (err: Error, type: inputDevice.KeyboardType) => { 315 console.log("The keyboard type is: " + type); 316 if (type == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'add') { 317 // 监听物理键盘已连接。 318 isPhysicalKeyboardExist = true; 319 } else if (type == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'remove') { 320 // 监听物理键盘已断开。 321 isPhysicalKeyboardExist = false; 322 } 323 }); 324 }); 325 // 根据isPhysicalKeyboardExist的值决定软键盘是否弹出。 326 } catch (error) { 327 console.error(`Get device info failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 328 } 329 }) 330 } 331 } 332} 333``` 334 335## inputDevice.off<sup>9+</sup> 336 337off(type: "change", listener?: Callback<DeviceListener>): void 338 339取消监听输入设备的热插拔事件。在应用退出前调用,取消监听。 340 341**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 342 343**参数**: 344 345| 参数名 | 类型 | 必填 | 说明 | 346| -------- | ---------------------------------------- | ---- | ----------- | 347| type | string | 是 | 输入设备的事件类型,固定值为'change'。 | 348| listener | Callback<[DeviceListener](#devicelistener9)> | 否 | 取消监听的回调函数,缺省时取消所有输入设备热插拔事件的监听。 | 349 350**错误码**: 351 352以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 353 354| 错误码ID | 错误信息 | 355| ---- | --------------------- | 356| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 357 358**示例**: 359 360```js 361import { inputDevice } from '@kit.InputKit'; 362 363@Entry 364@Component 365struct Index { 366 build() { 367 RelativeContainer() { 368 Text() 369 .onClick(() => { 370 let callback = (data: inputDevice.DeviceListener) => { 371 console.log(`Report device event info: ${JSON.stringify(data, [`type`, `deviceId`])}`); 372 }; 373 374 try { 375 inputDevice.on("change", callback); 376 } catch (error) { 377 console.error(`Listen device event failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 378 } 379 380 // 取消指定的监听。 381 try { 382 inputDevice.off("change", callback); 383 } catch (error) { 384 console.error(`Cancel listening device event failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 385 } 386 387 // 取消所有监听。 388 try { 389 inputDevice.off("change"); 390 } catch (error) { 391 console.error(`Cancel all listening device event failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 392 } 393 }) 394 } 395 } 396} 397``` 398 399## inputDevice.getDeviceIds<sup>(deprecated)</sup> 400 401getDeviceIds(callback: AsyncCallback<Array<number>>): void 402 403获取所有输入设备的id列表,使用Callback异步回调。 404 405> 从API version 9 开始不再维护,建议使用[inputDevice.getDeviceList](#inputdevicegetdevicelist9)代替。 406 407**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 408 409**参数**: 410 411| 参数名 | 类型 | 必填 | 说明 | 412| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 413| callback | AsyncCallback<Array<number>> | 是 | 回调函数,返回输入设备的id列表。 | 414 415**示例**: 416 417```js 418import { inputDevice } from '@kit.InputKit'; 419 420@Entry 421@Component 422struct Index { 423 build() { 424 RelativeContainer() { 425 Text() 426 .onClick(() => { 427 inputDevice.getDeviceIds((error: Error, ids: Array<Number>) => { 428 if (error) { 429 console.error(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`); 430 return; 431 } 432 console.log(`Device id list: ${JSON.stringify(ids)}`); 433 }); 434 }) 435 } 436 } 437} 438``` 439 440## inputDevice.getDeviceIds<sup>(deprecated)</sup> 441 442getDeviceIds(): Promise<Array<number>> 443 444获取所有输入设备的id列表,使用Promise异步回调。 445 446> 从API version 9 开始不再维护,建议使用[inputDevice.getDeviceList](#inputdevicegetdevicelist9)代替。 447 448**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 449 450**返回值**: 451 452| 类型 | 说明 | 453| ---------------------------------- | ------------------------------------------- | 454| Promise<Array<number>> | Promise对象,返回所有输入设备的id列表。 | 455 456**示例**: 457 458```js 459import { inputDevice } from '@kit.InputKit'; 460 461@Entry 462@Component 463struct Index { 464 build() { 465 RelativeContainer() { 466 Text() 467 .onClick(() => { 468 inputDevice.getDeviceIds().then((ids: Array<Number>) => { 469 console.log(`Device id list: ${JSON.stringify(ids)}`); 470 }); 471 }) 472 } 473 } 474} 475``` 476 477## inputDevice.getDevice<sup>(deprecated)</sup> 478 479getDevice(deviceId: number, callback: AsyncCallback<InputDeviceData>): void 480 481获取指定id的输入设备信息,使用Callback异步回调。 482 483> 从API version 9 开始不再维护,建议使用[inputDevice.getDeviceInfo](#inputdevicegetdeviceinfo9)代替。 484 485**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 486 487**参数**: 488 489| 参数名 | 类型 | 必填 | 说明 | 490| -------- | -------------------------------------------------------- | ---- | -------------------------------- | 491| deviceId | number | 是 | 输入设备id。 | 492| callback | AsyncCallback<[InputDeviceData](#inputdevicedata)> | 是 | 回调函数,返回输入设备信息。 | 493 494**示例**: 495 496```js 497import { inputDevice } from '@kit.InputKit'; 498 499@Entry 500@Component 501struct Index { 502 build() { 503 RelativeContainer() { 504 Text() 505 .onClick(() => { 506 // 获取输入设备id为1的设备信息。 507 inputDevice.getDevice(1, (error: Error, deviceData: inputDevice.InputDeviceData) => { 508 if (error) { 509 console.error(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`); 510 return; 511 } 512 console.log(`Device info: ${JSON.stringify(deviceData)}`); 513 }); 514 }) 515 } 516 } 517} 518``` 519 520## inputDevice.getDevice<sup>(deprecated)</sup> 521 522getDevice(deviceId: number): Promise<InputDeviceData> 523 524获取指定id的输入设备信息,使用Promise异步回调。 525 526> 从API version 9 开始不再维护,建议使用[inputDevice.getDeviceInfo](#inputdevicegetdeviceinfo9)代替。 527 528**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 529 530**参数**: 531 532| 参数名 | 类型 | 必填 | 说明 | 533| -------- | ------ | ---- | ------------ | 534| deviceId | number | 是 | 输入设备id。 | 535 536**返回值**: 537 538| 类型 | 说明 | 539| -------------------------------------------------- | ----------------------------------- | 540| Promise<[InputDeviceData](#inputdevicedata)> | Promise对象,返回输入设备信息。 | 541 542**示例**: 543 544```js 545import { inputDevice } from '@kit.InputKit'; 546 547@Entry 548@Component 549struct Index { 550 build() { 551 RelativeContainer() { 552 Text() 553 .onClick(() => { 554 // 获取输入设备id为1的设备信息。 555 inputDevice.getDevice(1).then((deviceData: inputDevice.InputDeviceData) => { 556 console.log(`Device info: ${JSON.stringify(deviceData)}`); 557 }); 558 }) 559 } 560 } 561} 562``` 563 564## inputDevice.supportKeys<sup>9+</sup> 565 566supportKeys(deviceId: number, keys: Array<KeyCode>, callback: AsyncCallback <Array<boolean>>): void 567 568查询指定输入设备是否支持指定按键,使用Callback异步回调。 569 570**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 571 572**参数**: 573 574| 参数名 | 类型 | 必填 | 说明 | 575| -------- | ----------------------------------------- | ---- | ------------------------------------------------------ | 576| deviceId | number | 是 | 输入设备的id。对于同一个物理设备,反复插拔会导致设备id发生变化。 | 577| keys | Array[<KeyCode>](js-apis-keycode.md#keycode) | 是 | 需要查询的键码值,最多支持5个按键查询。 | 578| callback | AsyncCallback<Array<boolean>> | 是 | 回调函数,返回查询结果。 | 579 580**错误码**: 581 582以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 583 584| 错误码ID | 错误信息 | 585| ---- | --------------------- | 586| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 587 588**示例**: 589 590```js 591import { inputDevice } from '@kit.InputKit'; 592 593@Entry 594@Component 595struct Index { 596 build() { 597 RelativeContainer() { 598 Text() 599 .onClick(() => { 600 // 查询id为1的输入设备对于17、22和2055按键的支持情况。 601 try { 602 inputDevice.supportKeys(1, [17, 22, 2055], (error: Error, supportResult: Array<Boolean>) => { 603 console.log(`Query result: ${JSON.stringify(supportResult)}`); 604 }); 605 } catch (error) { 606 console.error(`Query failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 607 } 608 }) 609 } 610 } 611} 612``` 613 614## inputDevice.supportKeys<sup>9+</sup> 615 616supportKeys(deviceId: number, keys: Array<KeyCode>): Promise<Array<boolean>> 617 618查询指定输入设备是否支持指定按键,使用Promise异步回调。 619 620**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 621 622**参数**: 623 624| 参数名 | 类型 | 必填 | 说明 | 625| -------- | -------------------- | ---- | ------------------------------------------------------ | 626| deviceId | number | 是 | 输入设备的id。对于同一个物理设备,反复插拔会导致设备id发生变化。 | 627| keys | Array[<KeyCode>](js-apis-keycode.md#keycode) | 是 | 需要查询的键码值,最多支持查询5个按键。 | 628 629**返回值**: 630 631| 类型 | 说明 | 632| ----------------------------------- | ------------------------------- | 633| Promise<Array<boolean>> | Promise对象,返回查询结果。true 表示支持,false表示不支持。 | 634 635**错误码**: 636 637以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 638 639| 错误码ID | 错误信息 | 640| ---- | --------------------- | 641| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 642 643**示例**: 644 645```js 646import { inputDevice } from '@kit.InputKit'; 647 648@Entry 649@Component 650struct Index { 651 build() { 652 RelativeContainer() { 653 Text() 654 .onClick(() => { 655 // 查询id为1的输入设备对于17、22和2055按键的支持情况。 656 try { 657 inputDevice.supportKeys(1, [17, 22, 2055]).then((supportResult: Array<Boolean>) => { 658 console.log(`Query result: ${JSON.stringify(supportResult)}`); 659 }); 660 } catch (error) { 661 console.error(`Query failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 662 } 663 }) 664 } 665 } 666} 667``` 668 669## inputDevice.supportKeysSync<sup>10+</sup> 670 671supportKeysSync(deviceId: number, keys: Array<KeyCode>): Array<boolean> 672 673查询指定id的输入设备对指定键码值的支持情况。 674 675**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 676 677**参数**: 678 679| 参数名 | 类型 | 必填 | 说明 | 680| -------- | -------------------- | ---- | ------------------------------------------------------ | 681| deviceId | number | 是 | 输入设备的id。对于同一个物理设备,反复插拔会导致设备id发生变化。 | 682| keys | Array[<KeyCode>](js-apis-keycode.md#keycode) | 是 | 需要查询的键码值,最多支持查询5个按键。 | 683 684**返回值**: 685 686| 类型 | 说明 | 687| ----------------------------------- | ------------------------------- | 688| Array<boolean> | 返回查询结果。true表示支持,false表示不支持。 | 689 690**错误码**: 691 692以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 693 694| 错误码ID | 错误信息 | 695| ---- | --------------------- | 696| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 697 698**示例**: 699 700```js 701import { inputDevice } from '@kit.InputKit'; 702 703@Entry 704@Component 705struct Index { 706 build() { 707 RelativeContainer() { 708 Text() 709 .onClick(() => { 710 // 查询id为1的输入设备对于17、22和2055按键的支持情况。 711 try { 712 let supportResult: Array<Boolean> = inputDevice.supportKeysSync(1, [17, 22, 2055]) 713 console.log(`Query result: ${JSON.stringify(supportResult)}`) 714 } catch (error) { 715 console.error(`Query failed, error: ${JSON.stringify(error, [`code`, `message`])}`) 716 } 717 }) 718 } 719 } 720} 721``` 722 723## inputDevice.getKeyboardType<sup>9+</sup> 724 725getKeyboardType(deviceId: number, callback: AsyncCallback<KeyboardType>): void 726 727获取输入设备的键盘类型,如全键盘、小键盘等,使用callback异步回调。输入设备的键盘类型以接口返回结果为准。 728 729**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 730 731**参数**: 732 733| 参数名 | 类型 | 必填 | 说明 | 734| -------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 735| deviceId | number | 是 | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 | 736| callback | AsyncCallback<[KeyboardType](#keyboardtype9)> | 是 | 回调函数,返回查询结果。 | 737 738**错误码**: 739 740以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 741 742| 错误码ID | 错误信息 | 743| ---- | --------------------- | 744| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 745 746**示例**: 747 748```js 749import { inputDevice } from '@kit.InputKit'; 750 751@Entry 752@Component 753struct Index { 754 build() { 755 RelativeContainer() { 756 Text() 757 .onClick(() => { 758 // 查询id为1的输入设备的键盘类型。 759 try { 760 inputDevice.getKeyboardType(1, (error: Error, type: Number) => { 761 if (error) { 762 console.error(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`); 763 return; 764 } 765 console.log(`Keyboard type: ${JSON.stringify(type)}`); 766 }); 767 } catch (error) { 768 console.error(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`); 769 } 770 }) 771 } 772 } 773} 774``` 775 776## inputDevice.getKeyboardType<sup>9+</sup> 777 778getKeyboardType(deviceId: number): Promise<KeyboardType> 779 780获取输入设备的键盘类型,使用Promise异步回调。 781 782**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 783 784**参数**: 785 786| 参数名 | 类型 | 必填 | 说明 | 787| -------- | ------ | ---- | ------------------------------------------------------------ | 788| deviceId | number | 是 | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 | 789 790**返回值**: 791 792| 类型 | 说明 | 793| --------------------------------------------- | ------------------------------- | 794| Promise<[KeyboardType](#keyboardtype9)> | Promise对象,返回查询结果。 | 795 796**错误码**: 797 798以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 799 800| 错误码ID | 错误信息 | 801| ---- | --------------------- | 802| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 803 804**示例**: 805 806```js 807import { inputDevice } from '@kit.InputKit'; 808 809@Entry 810@Component 811struct Index { 812 build() { 813 RelativeContainer() { 814 Text() 815 .onClick(() => { 816 // 示例查询设备id为1的设备键盘类型。 817 try { 818 inputDevice.getKeyboardType(1).then((type: Number) => { 819 console.log(`Keyboard type: ${JSON.stringify(type)}`); 820 }); 821 } catch (error) { 822 console.error(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`); 823 } 824 }) 825 } 826 } 827} 828``` 829 830## inputDevice.getKeyboardTypeSync<sup>10+</sup> 831 832getKeyboardTypeSync(deviceId: number): KeyboardType 833 834获取输入设备的键盘类型。 835 836**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 837 838**参数**: 839 840| 参数名 | 类型 | 必填 | 说明 | 841| -------- | ------ | ---- | ------------------------------------------------------------ | 842| deviceId | number | 是 | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 | 843 844**返回值**: 845 846| 类型 | 说明 | 847| --------------------------------------------- | ------------------------------- | 848| [KeyboardType](#keyboardtype9) | 返回查询结果。 | 849 850**错误码**: 851 852以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 853 854| 错误码ID | 错误信息 | 855| ---- | --------------------- | 856| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 857 858**示例**: 859 860```js 861import { inputDevice } from '@kit.InputKit'; 862 863@Entry 864@Component 865struct Index { 866 build() { 867 RelativeContainer() { 868 Text() 869 .onClick(() => { 870 // 示例查询设备id为1的设备键盘类型。 871 try { 872 let type: number = inputDevice.getKeyboardTypeSync(1) 873 console.log(`Keyboard type: ${JSON.stringify(type)}`) 874 } catch (error) { 875 console.error(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`) 876 } 877 }) 878 } 879 } 880} 881``` 882 883## inputDevice.isFunctionKeyEnabled<sup>15+</sup> 884 885isFunctionKeyEnabled(functionKey: FunctionKey): Promise<boolean> 886 887检查功能键(如:CapsLock键)是否使能。使用Promise异步回调。 888 889**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 890 891**参数**: 892 893| 参数名 | 类型 | 必填 | 说明 | 894| -------- | ------ | ---- | ------------------------------------------------------------ | 895| functionKey | [FunctionKey](#functionkey15) | 是 | 需要设置的功能键类型。 | 896 897**返回值**: 898 899| 类型 | 说明 | 900| ---------------------- | ------------------------------------------------------------ | 901| Promise<boolean> | Promise对象。返回查询结果,true表示功能键使能,false表示功能键未使能。 | 902 903**错误码**: 904 905以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[输入设备错误码](errorcode-inputdevice.md)。 906 907| 错误码ID | 错误信息 | 908| ---- | --------------------- | 909| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 910| 3900002 | There is currently no keyboard device connected. | 911 912**示例**: 913 914```js 915import { inputDevice } from '@kit.InputKit'; 916 917@Entry 918@Component 919struct Index { 920 build() { 921 RelativeContainer() { 922 Text() 923 .onClick(() => { 924 try { 925 inputDevice.isFunctionKeyEnabled(inputDevice.FunctionKey.CAPS_LOCK).then((state: boolean) => { 926 console.log(`capslock state: ${JSON.stringify(state)}`); 927 }); 928 } catch (error) { 929 console.error(`Failed to get capslock state, error: ${JSON.stringify(error, [`code`, `message`])}`); 930 } 931 }) 932 } 933 } 934} 935``` 936 937## inputDevice.setFunctionKeyEnabled<sup>15+</sup> 938 939setFunctionKeyEnabled(functionKey: FunctionKey, enabled: boolean): Promise<void> 940 941设置功能键(如:CapsLock键)使能状态。使用Promise异步回调。 942 943**需要权限**:ohos.permission.INPUT_KEYBOARD_CONTROLLER 944 945**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 946 947**参数**: 948 949| 参数名 | 类型 | 必填 | 说明 | 950| -------- | ------- | ---- | ------------------------- | 951| functionKey | [FunctionKey](#functionkey15) | 是 | 需要设置的功能键类型。 | 952| enabled | boolean | 是 | 功能键使能状态。取值为true表示使能功能键,取值为false表示不使能功能键。 | 953 954**返回值**: 955 956| 类型 | 说明 | 957| ---------------------- | ------------------------------------------------------------ | 958| Promise<void> | 无返回结果的Promise对象。 | 959 960**错误码**: 961 962以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[输入设备错误码](errorcode-inputdevice.md)。 963 964 965| 错误码ID | 错误信息 | 966| -------- | ------------------------------------------------------------ | 967| 201 | Permission denied. | 968| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 969| 3900002 | There is currently no keyboard device connected. | 970| 3900003 | It is prohibited for non-input applications. | 971 972**示例**: 973 974```js 975import { inputDevice } from '@kit.InputKit'; 976import { BusinessError } from '@kit.BasicServicesKit'; 977 978@Entry 979@Component 980struct Index { 981 build() { 982 RelativeContainer() { 983 Text() 984 .onClick(() => { 985 try { 986 inputDevice.setFunctionKeyEnabled(inputDevice.FunctionKey.CAPS_LOCK, true).then(() => { 987 console.info(`Set capslock state success`); 988 }).catch((error: BusinessError) => { 989 console.error(`Set capslock state failed, error=${JSON.stringify(error)}`); 990 }); 991 } catch (error) { 992 console.error(`Set capslock enable error`); 993 } 994 }) 995 } 996 } 997} 998``` 999 1000## inputDevice.getIntervalSinceLastInput<sup>14+</sup> 1001 1002getIntervalSinceLastInput(): Promise<number> 1003 1004获取距离上次系统输入事件的时间间隔(包含设备休眠时间),使用Promise异步回调。 1005 1006**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 1007 1008**返回值**: 1009 1010| 类型 | 说明 | 1011| --------------------------------------------- | ------------------------------- | 1012| Promise<number> | Promise对象,返回距离上次系统输入事件的时间间隔,单位为微秒(μs)。| 1013 1014**示例**: 1015 1016```js 1017import { inputDevice } from '@kit.InputKit'; 1018 1019@Entry 1020@Component 1021struct Index { 1022 build() { 1023 RelativeContainer() { 1024 Text() 1025 .onClick(() => { 1026 inputDevice.getIntervalSinceLastInput().then((timeInterval: number) => { 1027 console.log(`Interval since last input: ${JSON.stringify(timeInterval)}`); 1028 }); 1029 }) 1030 } 1031 } 1032} 1033``` 1034 1035## DeviceListener<sup>9+</sup> 1036 1037描述输入设备热插拔的信息。 1038 1039**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 1040 1041| 名称 | 类型 | 只读 | 可选 | 说明 | 1042| --------- | ------ | ---- | ---- | ------- | 1043| type | [ChangedType](#changedtype9)| 否 | 否 | 输入设备插入或者移除。| 1044| deviceId | number | 否 | 否 | 输入设备的唯一标识,同一个物理设备反复插拔或重启,设备id会发生变化。 | 1045 1046## InputDeviceData 1047 1048描述输入设备的信息。 1049 1050**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 1051 1052| 名称 | 类型 | 只读 | 可选 | 说明 | 1053| --------- | ------ | ---- | ---- | ------- | 1054| id | number | 否 | 否 | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 | 1055| name | string | 否 | 否 | 输入设备的名称。 | 1056| sources | Array<[SourceType](#sourcetype9)> | 否 | 否 | 输入设备支持的输入能力。一个输入设备可以同时具备多种输入能力,如有的键盘上附带触摸板,则此设备有键盘和触摸板两种输入能力。 | 1057| axisRanges | Array<[AxisRange](#axisrange)> | 否 | 否 | 输入设备的轴信息。 | 1058| bus<sup>9+</sup> | number | 否 | 否 | 输入设备的总线类型,该值以输入设备上报为准。 | 1059| product<sup>9+</sup> | number | 否 | 否 | 输入设备的产品信息。 | 1060| vendor<sup>9+</sup> | number | 否 | 否 | 输入设备的厂商信息。 | 1061| version<sup>9+</sup> | number | 否 | 否 | 输入设备的版本信息。 | 1062| phys<sup>9+</sup> | string | 否 | 否 | 输入设备的物理地址。 | 1063| uniq<sup>9+</sup> | string | 否 | 否 | 输入设备的唯一标识。 | 1064 1065## AxisType<sup>9+</sup> 1066 1067type AxisType = 'touchmajor' | 'touchminor' | 'orientation' | 'x' | 'y' | 'pressure' | 'toolminor' | 'toolmajor' | 'null' 1068 1069输入设备的轴类型。 1070 1071**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 1072 1073| 类型 |说明 | 1074| --------- | ------- | 1075| 'touchmajor' | 椭圆触摸区域长轴。 | 1076| 'touchminor' | 椭圆触摸区域短轴。 | 1077| 'toolminor' | 工具区域短轴。 | 1078| 'toolmajor' | 工具区域长轴。 | 1079| 'orientation' | 方向轴。 | 1080|'pressure' | 压力轴。 | 1081| 'x' | 横坐标轴。 | 1082| 'y' | 纵坐标轴。 | 1083|'null' | 无。 | 1084 1085## AxisRange 1086 1087输入设备的轴信息。 1088 1089**系统能力**: SystemCapability.MultimodalInput.Input.InputDevice 1090 1091| 名称 | 类型 | 只读 | 可选 | 说明 | 1092| --------- | ------ | ---- | ---- | ------- | 1093| source | [SourceType](#sourcetype9) | 否 | 否 | 轴的输入能力。 | 1094| axis | [AxisType](#axistype9) | 否 | 否 | 轴的类型。 | 1095| max | number | 否 | 否 | 轴的最大值。 | 1096| min | number | 否 | 否 | 轴的最小值。 | 1097| fuzz<sup>9+</sup> | number | 否 | 否 | 轴的模糊值。 | 1098| flat<sup>9+</sup> | number | 否 | 否 | 轴的基准值。 | 1099| resolution<sup>9+</sup> | number | 否 | 否 | 轴的分辨率。 | 1100 1101## SourceType<sup>9+</sup> 1102 1103type SourceType = 'keyboard' | 'mouse' | 'touchpad' | 'touchscreen' | 'joystick' | 'trackball' 1104 1105轴的输入能力。比如鼠标设备可上报x轴事件,则x轴的输入源就是鼠标。 1106 1107**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 1108 1109| 类型 |说明 | 1110| --------- | ------- | 1111| 'keyboard' | 表示输入设备是键盘。 | 1112| 'touchscreen' | 表示输入设备是触摸屏。 | 1113| 'mouse' | 表示输入设备是鼠标。 | 1114| 'trackball' | 表示输入设备是轨迹球。 | 1115| 'touchpad' | 表示输入设备是触摸板。 | 1116| 'joystick' | 表示输入设备是操纵杆。 | 1117 1118## ChangedType<sup>9+</sup> 1119 1120type ChangedType = 'add' | 'remove' 1121 1122监听设备热插拔事件类型。 1123 1124**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 1125 1126| 类型 | 说明 | 1127| --------- | ------- | 1128| 'add' | 插入输入设备。 | 1129| 'remove' | 移除输入设备。 | 1130 1131## KeyboardType<sup>9+</sup> 1132 1133键盘输入设备的类型。 1134 1135**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 1136 1137| 名称 | 值 | 说明 | 1138| ------------------- | ---- | --------- | 1139| NONE | 0 | 表示无按键设备。 | 1140| UNKNOWN | 1 | 表示未知按键设备。 | 1141| ALPHABETIC_KEYBOARD | 2 | 表示全键盘设备。 | 1142| DIGITAL_KEYBOARD | 3 | 表示小键盘设备。 | 1143| HANDWRITING_PEN | 4 | 表示手写笔设备。 | 1144| REMOTE_CONTROL | 5 | 表示遥控器设备。 | 1145 1146## FunctionKey<sup>15+</sup> 1147 1148功能键的类型。 1149 1150**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice 1151 1152| 名称 | 值 | 说明 | 1153| ------------------- | ---- | --------- | 1154| CAPS_LOCK | 1 | CapsLock键,仅支持对输入键盘扩展的CapsLock键设置使能。 |