• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimodalInput.inputDevice (Input Device)
2
3
4The inputDevice module implements input device management functions such as listening for the connection and disconnection of input devices and querying input device information such as the device name.
5
6
7> **NOTE**
8>
9> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
10
11
12## Modules to Import
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
23Obtains the IDs of all input devices. This API uses an asynchronous callback to return the result.
24
25**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
26
27**Parameters**
28
29| Name    | Type                                    | Mandatory| Description                                    |
30| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
31| callback | AsyncCallback&lt;Array&lt;number&gt;&gt; | Yes  | Callback used to return the IDs of all input devices. **id** is the unique ID of an input device.|
32
33**Error codes**
34
35For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
36
37| ID | Error Message            |
38| ---- | --------------------- |
39| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
40
41**Example**
42
43```js
44try {
45  inputDevice.getDeviceList((error: Error, ids: Array<Number>) => {
46    if (error) {
47      console.error(`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.error(`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
61Obtains the IDs of all input devices. This API uses a promise to return the result.
62
63**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
64
65**Return value**
66
67| Type                              | Description                                       |
68| ---------------------------------- | ------------------------------------------- |
69| Promise&lt;Array&lt;number&gt;&gt; | Promise used to return the IDs of all input devices.|
70
71**Example**
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.error(`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
87Obtains information about the specified input device. This API uses an asynchronous callback to return the result.
88
89**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
90
91**Parameters**
92
93| Name    | Type                                                    | Mandatory| Description                                   |
94| -------- | -------------------------------------------------------- | ---- | --------------------------------------- |
95| deviceId | number                                                   | Yes  | ID of the input device.                 |
96| callback | AsyncCallback&lt;[InputDeviceData](#inputdevicedata)&gt; | Yes  | Callback used to return information about the input device, including device ID, name, supported source, physical address, version information, and product information.|
97
98**Error codes**
99
100For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
101
102| ID | Error Message            |
103| ---- | --------------------- |
104| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
105
106**Example**
107
108```js
109// Obtain the name of the device whose ID is 1.
110try {
111  inputDevice.getDeviceInfo(1, (error: Error, deviceData: inputDevice.InputDeviceData) => {
112    if (error) {
113      console.error(`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.error(`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
127Obtains the information about the input device with the specified ID. This API uses a promise to return the result.
128
129**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
130
131**Parameters**
132
133| Name    | Type  | Mandatory| Description                  |
134| -------- | ------ | ---- | ---------------------- |
135| deviceId | number | Yes  | ID of the input device.|
136
137**Return value**
138
139| Type                                              | Description                           |
140| -------------------------------------------------- | ------------------------------- |
141| Promise&lt;[InputDeviceData](#inputdevicedata)&gt; | Promise used to return the information about the input device.|
142
143**Error codes**
144
145For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
146
147| ID | Error Message            |
148| ---- | --------------------- |
149| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
150
151**Example**
152
153```js
154// Obtain the name of the device whose ID is 1.
155try {
156  inputDevice.getDeviceInfo(1).then((deviceData: inputDevice.InputDeviceData) => {
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## inputDevice.getDeviceInfoSync<sup>10+</sup>
165
166getDeviceInfoSync(deviceId: number): InputDeviceData
167
168Obtains information about the specified input device.
169
170**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
171
172**Parameters**
173
174| Name    | Type  | Mandatory| Description                  |
175| -------- | ------ | ---- | ---------------------- |
176| deviceId | number | Yes  | ID of the input device.|
177
178**Return value**
179
180| Type                                              | Description                           |
181| -------------------------------------------------- | ------------------------------- |
182| [InputDeviceData](#inputdevicedata) | Information about the input device, including device ID, name, supported source, physical address, version information, and product information.|
183
184**Error codes**
185
186For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
187
188| ID | Error Message            |
189| ---- | --------------------- |
190| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
191
192**Example**
193
194```js
195// Obtain the name of the device whose ID is 1.
196try {
197  let deviceData: inputDevice.InputDeviceData = inputDevice.getDeviceInfoSync(1);
198  console.log(`Device info: ${JSON.stringify(deviceData)}`);
199} catch (error) {
200  console.error(`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
208Enables listening for device hot swap events. When performing this operation, you need to connect to external devices such as a mouse, keyboard, and touchscreen.
209
210**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
211
212**Parameters**
213
214| Name      | Type                                      | Mandatory  | Description         |
215| -------- | ---------------------------------------- | ---- | ----------- |
216| type     | string                                   | Yes   | Event type. This field has a fixed value of **change**. |
217| listener | Callback&lt;[DeviceListener](#devicelistener9)&gt; | Yes   | Listener for events of the input device.|
218
219**Error codes**
220
221For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
222
223| ID | Error Message            |
224| ---- | --------------------- |
225| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
226
227**Example**
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        // The physical keyboard is connected.
238        isPhysicalKeyboardExist = true;
239      } else if (type == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'remove') {
240        // The physical keyboard is disconnected.
241        isPhysicalKeyboardExist = false;
242      }
243    });
244  });
245  // Check whether the soft keyboard is open based on the value of isPhysicalKeyboardExist.
246} catch (error) {
247  console.error(`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
255Disables listening for device hot swap events. This API is called before the application exits.
256
257**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
258
259**Parameters**
260
261| Name      | Type                                      | Mandatory  | Description         |
262| -------- | ---------------------------------------- | ---- | ----------- |
263| type     | string                                   | Yes   | Event type. This field has a fixed value of **change**. |
264| listener | Callback&lt;[DeviceListener](#devicelistener9)&gt; | No   | Callback to unregister. If this parameter is left unspecified, listening for hot swap events of all input devices will be canceled.|
265
266**Error codes**
267
268For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
269
270| ID | Error Message            |
271| ---- | --------------------- |
272| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
273
274**Example**
275
276```js
277let 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.error(`Listen device event failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
285}
286
287// Disable this listener.
288try {
289  inputDevice.off("change", callback);
290} catch (error) {
291  console.error(`Cancel listening device event failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
292}
293
294// Disable all listeners.
295try {
296  inputDevice.off("change");
297} catch (error) {
298  console.error(`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
306Obtains the IDs of all input devices. This API uses an asynchronous callback to return the result.
307
308> This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceList](#inputdevicegetdevicelist9) instead.
309
310**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
311
312**Parameters**
313
314| Name    | Type                                    | Mandatory| Description                                    |
315| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
316| callback | AsyncCallback&lt;Array&lt;number&gt;&gt; | Yes  | Callback used to return the IDs of all input devices.|
317
318**Example**
319
320```js
321inputDevice.getDeviceIds((error: Error, ids: Array<Number>) => {
322  if (error) {
323    console.error(`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
334Obtains the IDs of all input devices. This API uses a promise to return the result.
335
336> This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceList](#inputdevicegetdevicelist9) instead.
337
338**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
339
340**Return value**
341
342| Type                              | Description                                       |
343| ---------------------------------- | ------------------------------------------- |
344| Promise&lt;Array&lt;number&gt;&gt; | Promise used to return the IDs of all input devices.|
345
346**Example**
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
358Obtains the information about the input device with the specified ID. This API uses an asynchronous callback to return the result.
359
360> This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceInfo](#inputdevicegetdeviceinfo9) instead.
361
362**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
363
364**Parameters**
365
366| Name    | Type                                                    | Mandatory| Description                            |
367| -------- | -------------------------------------------------------- | ---- | -------------------------------- |
368| deviceId | number                                                   | Yes  | ID of the input device.                    |
369| callback | AsyncCallback&lt;[InputDeviceData](#inputdevicedata)&gt; | Yes  | Callback used to return the information about the input device.|
370
371**Example**
372
373```js
374// Obtain the name of the device whose ID is 1.
375inputDevice.getDevice(1, (error: Error, deviceData: inputDevice.InputDeviceData) => {
376  if (error) {
377    console.error(`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
388Obtains the information about the input device with the specified ID. This API uses a promise to return the result.
389
390> This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceInfo](#inputdevicegetdeviceinfo9) instead.
391
392**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
393
394**Parameters**
395
396| Name    | Type  | Mandatory| Description        |
397| -------- | ------ | ---- | ------------ |
398| deviceId | number | Yes  | ID of the input device.|
399
400**Return value**
401
402| Type                                              | Description                               |
403| -------------------------------------------------- | ----------------------------------- |
404| Promise&lt;[InputDeviceData](#inputdevicedata)&gt; | Promise used to return the information about the input device.|
405
406**Example**
407
408```js
409// Obtain the name of the device whose ID is 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
419Checks whether the input device supports the specified keys. This API uses an asynchronous callback to return the result.
420
421**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
422
423**Parameters**
424
425| Name    | Type                                     | Mandatory| Description                                                  |
426| -------- | ----------------------------------------- | ---- | ------------------------------------------------------ |
427| deviceId | number                                    | Yes  | ID of the input device. The device ID changes if the same physical device is repeatedly removed and inserted.|
428| keys     | Array[&lt;KeyCode&gt;](js-apis-keycode.md#keycode)  | Yes  | Keycodes to be queried. A maximum of five keycodes can be specified.               |
429| callback | AsyncCallback&lt;Array&lt;boolean&gt;&gt; | Yes  | Callback used to return the result.                          |
430
431**Error codes**
432
433For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
434
435| ID | Error Message            |
436| ---- | --------------------- |
437| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
438
439**Example**
440
441```js
442// Check whether the input device whose ID is 1 supports keycodes 17, 22, and 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.error(`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
456Checks whether the input device supports the specified keys. This API uses a promise to return the result.
457
458**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
459
460**Parameters**
461
462| Name    | Type                | Mandatory| Description                                                  |
463| -------- | -------------------- | ---- | ------------------------------------------------------ |
464| deviceId | number               | Yes  | ID of the input device. The device ID changes if the same physical device is repeatedly removed and inserted.|
465| keys     | Array[&lt;KeyCode&gt;](js-apis-keycode.md#keycode) | Yes  | Keycodes to be queried. A maximum of five keycodes can be specified.               |
466
467**Return value**
468
469| Type                               | Description                           |
470| ----------------------------------- | ------------------------------- |
471| Promise&lt;Array&lt;boolean&gt;&gt; | Promise used to return the result. The value **true** indicates that the keycodes are supported, and the value **false** indicates the opposite.|
472
473**Error codes**
474
475For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
476
477| ID | Error Message            |
478| ---- | --------------------- |
479| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
480
481**Example**
482
483```js
484// Check whether the input device whose ID is 1 supports keycodes 17, 22, and 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.error(`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
498Checks whether the input device supports the specified keys.
499
500**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
501
502**Parameters**
503
504| Name    | Type                | Mandatory| Description                                                  |
505| -------- | -------------------- | ---- | ------------------------------------------------------ |
506| deviceId | number               | Yes  | ID of the input device. The device ID changes if the same physical device is repeatedly removed and inserted.|
507| keys     | Array[&lt;KeyCode&gt;](js-apis-keycode.md#keycode) | Yes  | Keycodes to be queried. A maximum of five keycodes can be specified.               |
508
509**Return value**
510
511| Type                               | Description                           |
512| ----------------------------------- | ------------------------------- |
513| Array&lt;boolean&gt; | Result indicating whether the input device supports the keycode value. The value **true** indicates yes, and the value **false** indicates no.|
514
515**Error codes**
516
517For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
518
519| ID | Error Message            |
520| ---- | --------------------- |
521| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
522
523**Example**
524
525```js
526// Check whether the input device whose ID is 1 supports keycodes 17, 22, and 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.error(`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
539Obtains the keyboard type of the input device, such as full keyboard and numeric keypad. This API uses an asynchronous callback to return the result. The keyboard type of the input device is subject to the result returned by the API.
540
541**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
542
543**Parameters**
544
545| Name    | Type                                               | Mandatory| Description                                                        |
546| -------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ |
547| deviceId | number                                              | Yes  | Unique ID of the input device. If the same physical device is repeatedly reinstalled or restarted, its ID may change.|
548| callback | AsyncCallback&lt;[KeyboardType](#keyboardtype9)&gt; | Yes  | Callback used to return the result.                                |
549
550**Error codes**
551
552For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
553
554| ID | Error Message            |
555| ---- | --------------------- |
556| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
557
558**Example**
559
560```js
561// Query the keyboard type of the input device whose ID is 1.
562try {
563  inputDevice.getKeyboardType(1, (error: Error, type: Number) => {
564    if (error) {
565      console.error(`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.error(`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
579Obtains the keyboard type of an input device. This API uses a promise to return the result.
580
581**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
582
583**Parameters**
584
585| Name   | Type  | Mandatory| Description                                                        |
586| -------- | ------ | ---- | ------------------------------------------------------------ |
587| deviceId | number | Yes  | Unique ID of the input device. If the same physical device is repeatedly reinstalled or restarted, its ID may change.|
588
589**Return value**
590
591| Type                                         | Description                           |
592| --------------------------------------------- | ------------------------------- |
593| Promise&lt;[KeyboardType](#keyboardtype9)&gt; | Promise used to return the result.|
594
595**Error codes**
596
597For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
598
599| ID | Error Message            |
600| ---- | --------------------- |
601| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
602
603**Example**
604
605```js
606// Query the keyboard type of the input device whose ID is 1.
607try {
608  inputDevice.getKeyboardType(1).then((type: Number) => {
609    console.log(`Keyboard type: ${JSON.stringify(type)}`);
610  });
611} catch (error) {
612  console.error(`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
620Obtains the keyboard type of the input device.
621
622**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
623
624**Parameters**
625
626| Name    | Type  | Mandatory| Description                                                        |
627| -------- | ------ | ---- | ------------------------------------------------------------ |
628| deviceId | number | Yes  | Unique ID of the input device. If the same physical device is repeatedly reinstalled or restarted, its ID may change.|
629
630**Return value**
631
632| Type                                         | Description                           |
633| --------------------------------------------- | ------------------------------- |
634| [KeyboardType](#keyboardtype9) | Keyboard type.|
635
636**Error codes**
637
638For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
639
640| ID | Error Message            |
641| ---- | --------------------- |
642| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
643
644**Example**
645
646```js
647// Query the keyboard type of the input device whose ID is 1.
648try {
649  let type: number = inputDevice.getKeyboardTypeSync(1)
650  console.log(`Keyboard type: ${JSON.stringify(type)}`)
651} catch (error) {
652  console.error(`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
660Checks whether the specified function key (for example, **CapsLock**) is enabled. This API uses a promise to return the result.
661
662**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
663
664**Parameters**
665
666| Name    | Type  | Mandatory| Description                                                        |
667| -------- | ------ | ---- | ------------------------------------------------------------ |
668| functionKey | [FunctionKey](#functionkey15) | Yes  | Type of the function key.|
669
670**Return value**
671
672| Type                  | Description                                                        |
673| ---------------------- | ------------------------------------------------------------ |
674| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** indicates that the function key is enabled, and the value **false** indicates the opposite.|
675
676**Error codes**
677
678For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Input Device Error Codes](errorcode-inputdevice.md).
679
680| ID | Error Message            |
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**Example**
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.error(`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
703Specifies whether to enable a function key (for example, **CapsLock**). This API uses a promise to return the result.
704
705**Required permissions**: ohos.permission.INPUT_KEYBOARD_CONTROLLER
706
707**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
708
709**Parameters**
710
711| Name  | Type   | Mandatory| Description                     |
712| -------- | ------- | ---- | ------------------------- |
713| functionKey | [FunctionKey](#functionkey15) | Yes  | Type of the function key.|
714| enabled  | boolean | Yes  | Status of the function key. The value **true** indicates that the function key is enabled, and the value **false** indicates the opposite.|
715
716**Error codes**
717
718For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Input Device Error Codes](errorcode-inputdevice.md).
719
720
721| ID| Error Message                                                    |
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**Example**
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.error(`Set capslock state failed, error=${JSON.stringify(error)}`);
739  });
740} catch (error) {
741    console.error(`Set capslock enable error`);
742}
743```
744
745## inputDevice.getIntervalSinceLastInput<sup>14+</sup>
746
747getIntervalSinceLastInput(): Promise&lt;number&gt;
748
749Obtains the interval (including the device sleep time) elapsed since the last system input event. This API uses a promise to return the result.
750
751**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
752
753**Return value**
754
755| Parameters                                         | Description                           |
756| --------------------------------------------- | ------------------------------- |
757| Promise&lt;number&gt; | Promise used to return the interval since the last system input event, in μs.|
758
759**Example**
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
769Provides hot swap information about an input device.
770
771**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
772
773| Name       | Type  | Readable  | Writable  | Description     |
774| --------- | ------ | ---- | ---- | ------- |
775| type     | [ChangedType](#changedtype9)| Yes| No| Device change type, which indicates whether an input device is inserted or removed.|
776| deviceId | number                      | Yes| No| Unique ID of the input device. If a physical device is repeatedly reinstalled or restarted, its ID may change.|
777
778## InputDeviceData
779
780Provides information about an input device.
781
782**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
783
784| Name       | Type  | Readable  | Writable  | Description     |
785| --------- | ------ | ---- | ---- | ------- |
786| id                   | number                                 | Yes| No| Unique ID of the input device. If the same physical device is repeatedly reinstalled or restarted, its ID may change.|
787| name                 | string                                 | Yes| No| Name of the input device.                                            |
788| sources              | Array&lt;[SourceType](#sourcetype9)&gt; | Yes| No| Input sources supported by the input device. An input device can have multiple input sources. For example, if a keyboard is equipped with a touchpad, the input device supports both keyboard and touchpad input capabilities.|
789| axisRanges           | Array&lt;[AxisRange](#axisrange)&gt;  | Yes| No| Axis information of the input device.                                          |
790| bus<sup>9+</sup>     | number                                 | Yes| No| Bus type of the input device. By default, the bus type reported by the input device prevails.            |
791| product<sup>9+</sup> | number                                 | Yes| No| Product information of the input device.                                        |
792| vendor<sup>9+</sup>  | number                                 | Yes| No| Vendor information of the input device.                                        |
793| version<sup>9+</sup> | number                                 | Yes| No| Version information of the input device.                                        |
794| phys<sup>9+</sup>    | string                                 | Yes| No| Physical address of the input device.                                        |
795| uniq<sup>9+</sup>    | string                                 | Yes| No| Unique ID of the input device.                                        |
796
797## AxisType<sup>9+</sup>
798
799type AxisType = 'touchmajor' | 'touchminor' | 'orientation' | 'x' | 'y' | 'pressure' | 'toolminor' | 'toolmajor' | 'null'
800
801Defines the axis type of an input device.
802
803**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
804
805| Type     |Description     |
806| --------- | ------- |
807| 'touchmajor'  | Major axis of the elliptical touching area.|
808| 'touchminor'  | Minor axis of the elliptical touching area.|
809| 'toolminor'   | Minor axis of the tool area.|
810| 'toolmajor'   | Major axis of the tool area.|
811| 'orientation' | Orientation axis.|
812|'pressure'    | Pressure axis. |
813| 'x'          | Horizontal axis.        |
814| 'y'           | Vertical axis.        |
815|'null'        |  None.            |
816
817## AxisRange
818
819Defines the axis range of an input device.
820
821**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
822
823| Name       | Type  | Readable  | Writable  | Description     |
824| --------- | ------ | ---- | ---- | ------- |
825| source                  | [SourceType](#sourcetype9) | Yes| No| Input source of the axis.|
826| axis                    | [AxisType](#axistype9)    | Yes| No| Axis type.   |
827| max                     | number                    | Yes| No| Maximum value of the axis.  |
828| min                     | number                    | Yes| No| Minimum value of the axis.  |
829| fuzz<sup>9+</sup>       | number                    | Yes| No| Fuzzy value of the axis.  |
830| flat<sup>9+</sup>       | number                    | Yes| No| Benchmark value of the axis.  |
831| resolution<sup>9+</sup> | number                    | Yes| No| Resolution of the axis.  |
832
833## SourceType<sup>9+</sup>
834
835type SourceType = 'keyboard' | 'mouse' | 'touchpad' | 'touchscreen' | 'joystick' | 'trackball'
836
837Enumerates input sources of the axis. For example, if a mouse reports an x-axis event, the input source of the x-axis is the mouse.
838
839**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
840
841| Type      |Description     |
842| --------- |  ------- |
843| 'keyboard'    | The input device is a keyboard. |
844| 'touchscreen' | The input device is a touchscreen.|
845| 'mouse'       | The input device is a mouse. |
846| 'trackball'   | The input device is a trackball.|
847| 'touchpad'    | The input device is a touchpad.|
848| 'joystick'   | The input device is a joystick.|
849
850## ChangedType<sup>9+</sup>
851
852type ChangedType = 'add' | 'remove'
853
854Enumerates hot swap events.
855
856**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
857
858| Type       | Description     |
859| --------- | ------- |
860| 'add'    | Device insertion.|
861| 'remove' | Device removal.|
862
863## KeyboardType<sup>9+</sup>
864
865Enumerates keyboard types.
866
867**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
868
869| Name                 | Value   | Description       |
870| ------------------- | ---- | --------- |
871| NONE                | 0    | Keyboard without keys. |
872| UNKNOWN             | 1    | Keyboard with unknown keys.|
873| ALPHABETIC_KEYBOARD | 2    | Full keyboard. |
874| DIGITAL_KEYBOARD    | 3    | Keypad. |
875| HANDWRITING_PEN     | 4    | Stylus. |
876| REMOTE_CONTROL      | 5    | Remote control. |
877
878## FunctionKey<sup>15+</sup>
879
880Enumerates function key types.
881
882**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
883
884| Name                 | Value   | Description       |
885| ------------------- | ---- | --------- |
886| CAPS_LOCK                | 1    | CapsLock key. This key can be enabled or disabled only for the input keyboard extension.|
887