• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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&lt;Array&lt;number&gt;&gt;): void
22
23获取所有输入设备的id列表,使用AsyncCallback异步方式返回结果。
24
25**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
26
27**参数**:
28
29| 参数名     | 类型                                     | 必填 | 说明                                     |
30| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
31| callback | AsyncCallback&lt;Array&lt;number&gt;&gt; | 是   | 回调函数,异步返回所有输入设备的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&lt;Array&lt;number&gt;&gt;
60
61获取所有输入设备的id列表,使用Promise异步方式返回结果。
62
63**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
64
65**返回值**:
66
67| 参数                               | 说明                                        |
68| ---------------------------------- | ------------------------------------------- |
69| Promise&lt;Array&lt;number&gt;&gt; | 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&lt;InputDeviceData&gt;): void
86
87获取指定输入设备的信息,使用AsyncCallback异步方式返回结果。
88
89**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
90
91**参数**:
92
93| 参数名     | 类型                                                     | 必填 | 说明                                    |
94| -------- | -------------------------------------------------------- | ---- | --------------------------------------- |
95| deviceId | number                                                   | 是   | 输入设备id。                  |
96| callback | AsyncCallback&lt;[InputDeviceData](#inputdevicedata)&gt; | 是   | 回调函数,异步返回输入设备信息。 |
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&lt;InputDeviceData&gt;
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&lt;[InputDeviceData](#inputdevicedata)&gt; | 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&lt;DeviceListener&gt;): void
207
208监听输入设备的热插拔事件,使用时需连接鼠标键盘等外部设备。
209
210**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
211
212**参数**:
213
214| 参数名       | 类型                                       | 必填   | 说明          |
215| -------- | ---------------------------------------- | ---- | ----------- |
216| type     | string                                   | 是    | 输入设备的事件【鼠标、键盘、触摸屏等】类型。  |
217| listener | Callback&lt;[DeviceListener](#devicelistener9)&gt; | 是    | 回调函数,异步上报输入设备热插拔事件。 |
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&lt;DeviceListener&gt;): void
254
255取消监听输入设备的热插拔事件。在应用退出前调用,取消监听。
256
257**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
258
259**参数**:
260
261| 参数名       | 类型                                       | 必填   | 说明          |
262| -------- | ---------------------------------------- | ---- | ----------- |
263| type     | string                                   | 是    | 输入设备的事件【鼠标、键盘、触摸屏等】类型。  |
264| listener | Callback&lt;[DeviceListener](#devicelistener9)&gt; | 否    | 取消监听的回调函数。 |
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&lt;Array&lt;number&gt;&gt;): 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&lt;Array&lt;number&gt;&gt; | 是   | 回调函数,异步返回所有输入设备的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&lt;Array&lt;number&gt;&gt;
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&lt;Array&lt;number&gt;&gt; | 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&lt;InputDeviceData&gt;): 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&lt;[InputDeviceData](#inputdevicedata)&gt; | 是   | 回调函数,异步返回输入设备信息。 |
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&lt;InputDeviceData&gt;
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&lt;[InputDeviceData](#inputdevicedata)&gt; | 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&lt;KeyCode&gt;, callback: AsyncCallback &lt;Array&lt;boolean&gt;&gt;): void
418
419获取输入设备是否支持指定的键码值,使用AsyncCallback异步方式返回结果。
420
421**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
422
423**参数**:
424
425| 参数名     | 类型                                      | 必填 | 说明                                                   |
426| -------- | ----------------------------------------- | ---- | ------------------------------------------------------ |
427| deviceId | number                                    | 是   | 输入设备id,同一个物理设备反复插拔,设备id会发生变化。 |
428| keys     | Array[&lt;KeyCode&gt;](js-apis-keycode.md#keycode)  | 是   | 需要查询的键码值,最多支持5个按键查询。                |
429| callback | AsyncCallback&lt;Array&lt;boolean&gt;&gt; | 是   | 回调函数,异步返回查询结果。                           |
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&lt;KeyCode&gt;): Promise&lt;Array&lt;boolean&gt;&gt;
455
456获取输入设备是否支持指定的键码值,使用Promise异步方式返回结果。
457
458**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
459
460**参数**:
461
462| 参数名     | 类型                 | 必填 | 说明                                                   |
463| -------- | -------------------- | ---- | ------------------------------------------------------ |
464| deviceId | number               | 是   | 输入设备id,同一个物理设备反复插拔,设备id会发生变化。 |
465| keys     | Array[&lt;KeyCode&gt;](js-apis-keycode.md#keycode) | 是   | 需要查询的键码值,最多支持5个按键查询。                |
466
467**返回值**:
468
469| 参数                                | 说明                            |
470| ----------------------------------- | ------------------------------- |
471| Promise&lt;Array&lt;boolean&gt;&gt; | Promise对象,异步返回查询结果。true 表示支持,false表示不支持。 |
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&lt;KeyCode&gt;): Array&lt;boolean&gt;
497
498获取输入设备是否支持指定的键码值。
499
500**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
501
502**参数**:
503
504| 参数名     | 类型                 | 必填 | 说明                                                   |
505| -------- | -------------------- | ---- | ------------------------------------------------------ |
506| deviceId | number               | 是   | 输入设备id,同一个物理设备反复插拔,设备id会发生变化。 |
507| keys     | Array[&lt;KeyCode&gt;](js-apis-keycode.md#keycode) | 是   | 需要查询的键码值,最多支持5个按键查询。                |
508
509**返回值**:
510
511| 参数                                | 说明                            |
512| ----------------------------------- | ------------------------------- |
513| Array&lt;boolean&gt; | 返回查询结果。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&lt;KeyboardType&gt;): void
538
539获取输入设备的键盘类型,使用AsyncCallback异步方式返回结果。
540
541**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
542
543**参数**:
544
545| 参数名     | 类型                                                | 必填 | 说明                                                         |
546| -------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ |
547| deviceId | number                                              | 是   | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 |
548| callback | AsyncCallback&lt;[KeyboardType](#keyboardtype9)&gt; | 是   | 回调函数,异步返回查询结果。                                 |
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&lt;KeyboardType&gt;
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&lt;[KeyboardType](#keyboardtype9)&gt; | 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.isFunctionKeyEnabled<sup>15+</sup>
657
658isFunctionKeyEnabled(functionKey: FunctionKey): Promise&lt;boolean&gt;
659
660检查功能键是否使能。使用Promise异步回调。
661
662**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
663
664**参数**:
665
666| 参数名     | 类型   | 必填 | 说明                                                         |
667| -------- | ------ | ---- | ------------------------------------------------------------ |
668| functionKey | [FunctionKey](#functionkey15) | 是   | 需要设置的功能键类型。 |
669
670**返回值**:
671
672| 参数                   | 说明                                                         |
673| ---------------------- | ------------------------------------------------------------ |
674| Promise&lt;boolean&gt; | Promise对象。返回查询结果,true表示功能键使能,false表示功能键未使能。 |
675
676**错误码**:
677
678以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[输入设备错误码](errorcode-inputdevice.md)。
679
680| 错误码ID  | 错误信息             |
681| ---- | --------------------- |
682| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
683| 3900002      | There is currently no keyboard device connected. |
684
685**示例**:
686
687```js
688import { inputDevice } from '@kit.InputKit';
689
690try {
691  inputDevice.isFunctionKeyEnabled(inputDevice.FunctionKey.CAPS_LOCK).then((state: boolean) => {
692    console.log(`capslock state: ${JSON.stringify(state)}`);
693  });
694} catch (error) {
695  console.log(`Failed to get capslock state, error: ${JSON.stringify(error, [`code`, `message`])}`);
696}
697```
698
699## inputDevice.setFunctionKeyEnabled<sup>15+</sup>
700
701setFunctionKeyEnabled(functionKey: FunctionKey, enabled: boolean): Promise&lt;void&gt;
702
703设置功能键使能状态。使用Promise异步回调。
704
705**需要权限**:ohos.permission.INPUT_KEYBOARD_CONTROLLER
706
707**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
708
709**参数**:
710
711| 参数名   | 类型    | 必填 | 说明                      |
712| -------- | ------- | ---- | ------------------------- |
713| functionKey | [FunctionKey](#functionkey15) | 是   | 需要设置的功能键类型。 |
714| enabled  | boolean | 是   | 功能键使能状态。取值为true表示使能功能键,取值为false表示不使能功能键。 |
715
716**错误码**:
717
718以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[输入设备错误码](errorcode-inputdevice.md)。
719
720
721| 错误码ID | 错误信息                                                     |
722| -------- | ------------------------------------------------------------ |
723| 201      | Permission denied.                                           |
724| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
725| 3900002      | There is currently no keyboard device connected. |
726| 3900003      | It is prohibited for non-input applications. |
727
728**示例**:
729
730```js
731import { inputDevice } from '@kit.InputKit';
732import { BusinessError } from '@kit.BasicServicesKit';
733
734try {
735  inputDevice.setFunctionKeyEnabled(inputDevice.FunctionKey.CAPS_LOCK, true).then(() => {
736    console.info(`Set capslock state success`);
737  }).catch((error: BusinessError) => {
738    console.info(`Set capslock state failed, error=${JSON.stringify(error)}`);
739  });
740} catch (error) {
741    console.info(`Set capslock enable error`);
742}
743```
744
745## inputDevice.getIntervalSinceLastInput<sup>14+</sup>
746
747getIntervalSinceLastInput(): Promise&lt;number&gt;
748
749获取距离上次系统输入事件的时间间隔,使用Promise异步回调。
750
751**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
752
753**返回值**:
754
755| 参数                                          | 说明                            |
756| --------------------------------------------- | ------------------------------- |
757| Promise&lt;number&gt; | Promise对象,异步返回获取的时间间隔,单位为微秒(μs)。|
758
759**示例**:
760
761```js
762  inputDevice.getIntervalSinceLastInput().then((timeInterval: number) => {
763    console.log(`Interval since last input: ${JSON.stringify(timeInterval)}`);
764  });
765```
766
767## DeviceListener<sup>9+</sup>
768
769输入设备热插拔的描述信息。
770
771**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
772
773| 名称        | 类型   | 可读   | 可写   | 说明      |
774| --------- | ------ | ---- | ---- | ------- |
775| type     | [ChangedType](#changedtype9)| 是 | 否 | 输入设备插入或者移除。|
776| deviceId | number                      | 是 | 否 | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 |
777
778## InputDeviceData
779
780输入设备的描述信息。
781
782**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
783
784| 名称        | 类型   | 可读   | 可写   | 说明      |
785| --------- | ------ | ---- | ---- | ------- |
786| id                   | number                                 | 是 | 否 | 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。 |
787| name                 | string                                 | 是 | 否 | 输入设备的名字。                                             |
788| sources              | Array&lt;[SourceType](#sourcetype9)&gt; | 是 | 否 | 输入设备支持的源类型。比如有的键盘上附带触摸板,则此设备有keyboard和touchpad两种输入源。 |
789| axisRanges           | Array&lt;[AxisRange](#axisrange)&gt;  | 是 | 否 | 输入设备的轴信息。                                           |
790| bus<sup>9+</sup>     | number                                 | 是 | 否 | 输入设备的总线类型。                                         |
791| product<sup>9+</sup> | number                                 | 是 | 否 | 输入设备的产品信息。                                         |
792| vendor<sup>9+</sup>  | number                                 | 是 | 否 | 输入设备的厂商信息。                                         |
793| version<sup>9+</sup> | number                                 | 是 | 否 | 输入设备的版本信息。                                         |
794| phys<sup>9+</sup>    | string                                 | 是 | 否 | 输入设备的物理地址。                                         |
795| uniq<sup>9+</sup>    | string                                 | 是 | 否 | 输入设备的唯一标识。                                         |
796
797## AxisType<sup>9+</sup>
798
799type AxisType = 'touchmajor' | 'touchminor' | 'orientation' | 'x' | 'y' | 'pressure' | 'toolminor' | 'toolmajor' | 'null'
800
801输入设备的轴类型。
802
803**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
804
805| 类型      |说明      |
806| --------- | ------- |
807| 'touchmajor'  | 椭圆触摸区域长轴。 |
808| 'touchminor'  | 椭圆触摸区域短轴。 |
809| 'toolminor'   | 工具区域短轴。 |
810| 'toolmajor'   | 工具区域长轴。 |
811| 'orientation' | 方向轴。 |
812|'pressure'    | 压力轴。  |
813| 'x'          | 横坐标轴。         |
814| 'y'           | 纵坐标轴。         |
815|'null'        |  无。             |
816
817## AxisRange
818
819输入设备的轴信息。
820
821**系统能力**: SystemCapability.MultimodalInput.Input.InputDevice
822
823| 名称        | 类型   | 可读   | 可写   | 说明      |
824| --------- | ------ | ---- | ---- | ------- |
825| source                  | [SourceType](#sourcetype9) | 是 | 否 | 轴的输入源类型。 |
826| axis                    | [AxisType](#axistype9)    | 是 | 否 | 轴的类型。    |
827| max                     | number                    | 是 | 否 | 轴的最大值。   |
828| min                     | number                    | 是 | 否 | 轴的最小值。   |
829| fuzz<sup>9+</sup>       | number                    | 是 | 否 | 轴的模糊值。   |
830| flat<sup>9+</sup>       | number                    | 是 | 否 | 轴的基准值。   |
831| resolution<sup>9+</sup> | number                    | 是 | 否 | 轴的分辨率。   |
832
833## SourceType<sup>9+</sup>
834
835type SourceType = 'keyboard' | 'mouse' | 'touchpad' | 'touchscreen' | 'joystick' | 'trackball'
836
837轴的输入源类型。比如鼠标设备可上报x轴事件,则x轴的输入源就是鼠标。
838
839**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
840
841| 类型       |说明      |
842| --------- |  ------- |
843| 'keyboard'    | 表示输入设备是键盘。  |
844| 'touchscreen' | 表示输入设备是触摸屏。 |
845| 'mouse'       | 表示输入设备是鼠标。  |
846| 'trackball'   | 表示输入设备是轨迹球。 |
847| 'touchpad'    | 表示输入设备是触摸板。 |
848| 'joystick'   | 表示输入设备是操纵杆。 |
849
850## ChangedType<sup>9+</sup>
851
852type ChangedType = 'add' | 'remove'
853
854定义监听设备热插拔事件。
855
856**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
857
858| 类型        | 说明      |
859| --------- | ------- |
860| 'add'    | 表示输入设备插入。 |
861| 'remove' | 表示输入设备移除。 |
862
863## KeyboardType<sup>9+</sup>
864
865定义键盘输入设备的类型。
866
867**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
868
869| 名称                  | 值    | 说明        |
870| ------------------- | ---- | --------- |
871| NONE                | 0    | 表示无按键设备。  |
872| UNKNOWN             | 1    | 表示未知按键设备。 |
873| ALPHABETIC_KEYBOARD | 2    | 表示全键盘设备。  |
874| DIGITAL_KEYBOARD    | 3    | 表示小键盘设备。  |
875| HANDWRITING_PEN     | 4    | 表示手写笔设备。  |
876| REMOTE_CONTROL      | 5    | 表示遥控器设备。  |
877
878## FunctionKey<sup>15+</sup>
879
880定义功能键的类型。
881
882**系统能力**:SystemCapability.MultimodalInput.Input.InputDevice
883
884| 名称                  | 值    | 说明        |
885| ------------------- | ---- | --------- |
886| CAPS_LOCK                | 1    | CapsLock键,仅支持对输入键盘扩展的CapsLock键设置使能。 |