• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimodalInput.inputDevice (Input Device)
2
3The **inputDevice** module implements listening for connection, disconnection, and update events of input devices and displays information about input devices. For example, it can be used to listen for mouse insertion and removal and obtain information such as the ID, name, and pointer speed of the mouse.
4
5> **NOTE**<br>
6> 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.
7
8## Modules to Import
9
10```js
11import inputDevice from '@ohos.multimodalInput.inputDevice';
12```
13## inputDevice.getDeviceList<sup>9+</sup>
14
15getDeviceList(callback: AsyncCallback&lt;Array&lt;number&gt;&gt;): void
16
17Obtains the IDs of all input devices. This API uses an asynchronous callback to return the result.
18
19**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
20
21**Parameters**
22
23| Name    | Type                                    | Mandatory| Description      |
24| -------- | ---------------------------------------- | ---- | ---------- |
25| callback | AsyncCallback&lt;Array&lt;number&gt;&gt; | Yes  | Callback used to return the result.|
26
27**Example**
28
29```js
30try {
31  inputDevice.getDeviceIds((error, ids) => {
32    if (error) {
33      console.log(`Failed to get device list.
34         error code=${JSON.stringify(err.code)} msg=${JSON.stringify(err.message)}`);
35      return;
36    }
37    this.data = ids;
38    console.log("The device ID list is: " + ids);
39  });
40} catch (error) {
41  console.info("getDeviceList " + error.code + " " + error.message);
42}
43```
44
45## inputDevice.getDeviceList<sup>9+</sup>
46
47getDeviceList(): Promise&lt;Array&lt;number&gt;&gt;
48
49Obtains the IDs of all input devices. This API uses a promise to return the result.
50
51**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
52
53**Return value**
54
55| Name                              | Description                           |
56| ---------------------------------- | ------------------------------- |
57| Promise&lt;Array&lt;number&gt;&gt; | Promise used to return the result.|
58
59**Example**
60
61```js
62try {
63  inputDevice.getDeviceIds().then((ids) => {
64    console.log("The device ID list is: " + ids);
65  });
66} catch (error) {
67  console.info("getDeviceList " + error.code + " " + error.message);
68}
69```
70
71## inputDevice.getDeviceInfo<sup>9+</sup>
72
73getDeviceInfo(deviceId: number, callback: AsyncCallback&lt;InputDeviceData&gt;): void
74
75Obtains information about an input device. This API uses an asynchronous callback to return the result.
76
77**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
78
79**Parameters**
80
81| Name    | Type                                                    | Mandatory| Description                                   |
82| -------- | -------------------------------------------------------- | ---- | --------------------------------------- |
83| deviceId | number                                                   | Yes  | ID of the input device.                 |
84| callback | AsyncCallback&lt;[InputDeviceData](#inputdevicedata)&gt; | Yes  | Callback used to return the result, which is an **InputDeviceData** object.|
85
86**Example**
87
88```js
89// Obtain the name of the device whose ID is 1.
90try {
91  inputDevice.getDeviceInfo(1, (error, inputDevice) => {
92    if (error) {
93      console.log(`Failed to get device information.
94          error code=${JSON.stringify(err.code)} msg=${JSON.stringify(err.message)}`);
95      return;
96    }
97    console.log("The device name is: " + inputDevice.name);
98  });
99} catch (error) {
100  console.info("getDeviceInfo " + error.code + " " + error.message);
101}
102```
103
104## inputDevice.getDeviceInfo<sup>9+</sup>
105
106getDeviceInfo(deviceId: number): Promise&lt;InputDeviceData&gt;
107
108Obtains information about an input device. This API uses a promise to return the result.
109
110**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
111
112**Parameters**
113
114| Name    | Type  | Mandatory| Description                  |
115| -------- | ------ | ---- | ---------------------- |
116| deviceId | number | Yes  | ID of the input device.|
117
118**Return value**
119
120| Name                                              | Description                           |
121| -------------------------------------------------- | ------------------------------- |
122| Promise&lt;[InputDeviceData](#inputdevicedata)&gt; | Promise used to return the result.|
123
124**Example**
125
126```js
127// Obtain the name of the device whose ID is 1.
128try {
129  inputDevice.getDeviceInfo(id).then((inputDevice) => {
130    console.log("The device name is: " + inputDevice.name);
131  });
132} catch (error) {
133  console.info("getDeviceInfo " + error.code + " " + error.message);
134}
135```
136
137
138## inputDevice.on<sup>9+</sup>
139
140on(type: "change", listener: Callback&lt;DeviceListener&gt;): void
141
142Enables listening for hot swap events of an input device.
143
144**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
145
146**Parameters**
147
148| Name      | Type                                      | Mandatory  | Description         |
149| -------- | ---------------------------------------- | ---- | ----------- |
150| type     | string                                   | Yes   | Event type of the input device. |
151| listener | Callback&lt;[DeviceListener](#devicelistener9)&gt; | Yes   | Listener for events of the input device.|
152
153**Example**
154
155```js
156let isPhysicalKeyboardExist = true;
157try {
158  inputDevice.on("change", (data) => {
159    console.log("type: " + data.type + ", deviceId: " + data.deviceId);
160    inputDevice.getKeyboardType(data.deviceId, (err, ret) => {
161      console.log("The keyboard type of the device is: " + ret);
162      if (ret == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'add') {
163        // The physical keyboard is connected.
164        isPhysicalKeyboardExist = true;
165      } else if (ret == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'remove') {
166        // The physical keyboard is disconnected.
167        isPhysicalKeyboardExist = false;
168      }
169    });
170  });
171  // Check whether the soft keyboard is open based on the value of isPhysicalKeyboardExist.
172} catch (error) {
173  console.info("oninputdevcie " + error.code + " " + error.message);
174}
175```
176
177## inputDevice.off<sup>9+</sup>
178
179off(type: "change", listener?: Callback&lt;DeviceListener&gt;): void
180
181Disables listening for hot swap events of an input device.
182
183**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
184
185**Parameters**
186
187| Name      | Type                                      | Mandatory  | Description         |
188| -------- | ---------------------------------------- | ---- | ----------- |
189| type     | string                                   | Yes   | Event type of the input device. |
190| listener | Callback&lt;[DeviceListener](#devicelistener9)&gt; | No   | Listener for events of the input device.|
191
192**Example**
193
194```js
195callback: function(data) {
196  console.log("type: " + data.type + ", deviceId: " + data.deviceId);
197}
198
199try {
200  inputDevice.on("change", this.callback);
201} catch (error) {
202  console.info("oninputdevcie " + error.code + " " + error.message)
203}
204
205// Enable listening for hot swap events of an input device.
206inputDevice.on("change", listener);
207
208// Disable this listener.
209try {
210  inputDevice.off("change", this.callback);
211} catch (error) {
212  console.info("offinputdevcie " + error.code + " " + error.message)
213}
214
215// Disable all listeners.
216try {
217  inputDevice.off("change");
218} catch (error) {
219  console.info("offinputdevcie " + error.code + " " + error.message);
220}
221// By default, the soft keyboard is closed when listening is disabled.
222```
223
224## inputDevice.getDeviceIds<sup>(deprecated)</sup>
225
226getDeviceIds(callback: AsyncCallback&lt;Array&lt;number&gt;&gt;): void
227
228Obtains the IDs of all input devices. This API uses an asynchronous callback to return the result.
229
230This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceList](#inputdevicegetdevicelist9) instead.
231
232**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
233
234**Parameters**
235
236| Name      | Type                                      | Mandatory  | Description   |
237| -------- | ---------------------------------------- | ---- | ----- |
238| callback | AsyncCallback&lt;Array&lt;number&gt;&gt; | Yes   | Callback used to return the result.|
239
240**Example**
241
242```js
243inputDevice.getDeviceIds((error, ids) => {
244  if (error) {
245    console.log(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`);
246    return;
247  }
248  console.log(`Device id list: ${JSON.stringify(ids)}`);
249});
250```
251```
252
253## inputDevice.getDeviceIds<sup>(deprecated)</sup>
254
255getDeviceIds(): Promise&lt;Array&lt;number&gt;&gt;
256
257Obtains the IDs of all input devices. This API uses a promise to return the result.
258
259This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceList](#inputdevicegetdevicelist9) instead.
260
261**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
262
263**Return value**
264
265| Parameter                                | Description                 |
266| ---------------------------------- | ------------------- |
267| Promise&lt;Array&lt;number&gt;&gt; | Promise used to return the result.|
268
269**Example**
270
271```js
272inputDevice.getDeviceIds().then((ids) => {
273  console.log(`Device id list: ${JSON.stringify(ids)}`);
274});
275```
276
277## inputDevice.getDevice<sup>(deprecated)</sup>
278
279getDevice(deviceId: number, callback: AsyncCallback&lt;InputDeviceData&gt;): void
280
281Obtains information about an input device. This API uses an asynchronous callback to return the result.
282
283This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceInfo](#inputdevicegetdeviceinfo9) instead.
284
285**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
286
287**Parameters**
288
289| Name      | Type                                      | Mandatory  | Description                         |
290| -------- | ---------------------------------------- | ---- | --------------------------- |
291| deviceId | number                                   | Yes   | ID of the input device.               |
292| callback | AsyncCallback&lt;[InputDeviceData](#inputdevicedata)&gt; | Yes   | Callback used to return the result, which is an **InputDeviceData** object.|
293
294**Example**
295
296```js
297// Obtain the name of the device whose ID is 1.
298inputDevice.getDevice(1, (error, deviceData) => {
299  if (error) {
300    console.log(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`);
301    return;
302  }
303  console.log(`Device info: ${JSON.stringify(deviceData)}`);
304});
305```
306
307## inputDevice.getDevice<sup>(deprecated)</sup>
308
309getDevice(deviceId: number): Promise&lt;InputDeviceData&gt;
310
311Obtains information about an input device. This API uses a promise to return the result.
312
313This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceInfo](#inputdevicegetdeviceinfo9) instead.
314
315**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
316
317**Parameters**
318
319| Name      | Type    | Mandatory  | Description          |
320| -------- | ------ | ---- | ------------ |
321| deviceId | number | Yes   | ID of the input device.|
322
323**Return value**
324
325| Parameter                                      | Description                 |
326| ---------------------------------------- | ------------------- |
327| Promise&lt;[InputDeviceData](#inputdevicedata)&gt; | Promise used to return the result.|
328
329**Example**
330
331```js
332// Obtain the name of the device whose ID is 1.
333inputDevice.getDevice(1).then((deviceData) => {
334  console.log(`Device info: ${JSON.stringify(deviceData)}`);
335});
336```
337
338## inputDevice.supportKeys<sup>9+</sup>
339
340supportKeys(deviceId: number, keys: Array&lt;KeyCode&gt;, callback: AsyncCallback &lt;Array&lt;boolean&gt;&gt;): void
341
342Obtains the key codes supported by the input device. This API uses an asynchronous callback to return the result.
343
344**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
345
346**Parameters**
347
348| Name      | Type                                  | Mandatory  | Description                               |
349| -------- | ------------------------------------ | ---- | --------------------------------- |
350| deviceId | number                               | Yes   | Unique ID of the input device. If the same physical device is repeatedly inserted and removed, its ID changes.|
351| keys     | Array&lt;KeyCode&gt;                 | Yes   | Key codes to be queried. A maximum of five key codes can be specified.             |
352| callback | AsyncCallback&lt;Array&lt;boolean&gt;&gt; | Yes   | Callback used to return the result.                   |
353
354**Example**
355
356```js
357// Check whether the input device whose ID is 1 supports key codes 17, 22, and 2055.
358try {
359  inputDevice.supportKeys(1, [17, 22, 2055], (error, ret) => {
360    console.log("The query result is as follows: " + ret);
361  });
362} catch (error) {
363  console.info("supportKeys " + error.code + " " + error.message);
364}
365```
366
367## inputDevice.supportKeys<sup>9+</sup>
368
369supportKeys(deviceId: number, keys: Array&lt;KeyCode&gt;): Promise&lt;Array&lt;boolean&gt;&gt;
370
371Obtains the key codes supported by the input device. This API uses a promise to return the result.
372
373**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
374
375**Parameters**
376
377| Name      | Type                  | Mandatory  | Description                               |
378| -------- | -------------------- | ---- | --------------------------------- |
379| deviceId | number               | Yes   | Unique ID of the input device. If the same physical device is repeatedly inserted and removed, its ID changes.|
380| keys     | Array&lt;KeyCode&gt; | Yes   | Key codes to be queried. A maximum of five key codes can be specified.             |
381
382**Return value**
383
384| Parameter                                 | Description                 |
385| ----------------------------------- | ------------------- |
386| Promise&lt;Array&lt;boolean&gt;&gt; | Promise used to return the result.|
387
388**Example**
389
390```js
391// Check whether the input device whose ID is 1 supports key codes 17, 22, and 2055.
392try {
393  inputDevice.supportKeys(1, [17, 22, 2055]).then((ret) => {
394    console.log("The query result is as follows: " + ret);
395  });
396} catch (error) {
397  console.info("supportKeys " + error.code + " " + error.message);
398}
399```
400
401## inputDevice.getKeyboardType<sup>9+</sup>
402
403getKeyboardType(deviceId: number, callback: AsyncCallback&lt;KeyboardType&gt;): void
404
405Obtains the keyboard type of an input device. This API uses an asynchronous callback to return the result.
406
407**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
408
409**Parameters**
410
411| Name      | Type                                      | Mandatory  | Description                               |
412| -------- | ---------------------------------------- | ---- | --------------------------------- |
413| deviceId | number                                   | Yes   | Unique ID of the input device. If the same physical device is repeatedly inserted and removed, its ID changes.|
414| callback | AsyncCallback&lt;[KeyboardType](#keyboardtype9)&gt; | Yes   | Callback used to return the result.                   |
415
416**Example**
417
418```js
419// Query the keyboard type of the input device whose ID is 1.
420try {
421  inputDevice.getKeyboardType(1, (error, number) => {
422    if (error) {
423      console.log(`Failed to get keyboardtype.
424          error code=${JSON.stringify(err.code)} msg=${JSON.stringify(err.message)}`);
425      return;
426    }
427    console.log("The keyboard type of the device is: " + number);
428  });
429} catch (error) {
430  console.info("getKeyboardType " + error.code + " " + error.message);
431}
432```
433
434## inputDevice.getKeyboardType<sup>9+</sup>
435
436getKeyboardType(deviceId: number,): Promise&lt;KeyboardType&gt;
437
438Obtains the keyboard type of an input device. This API uses a promise to return the result.
439
440**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
441
442**Return value**
443
444| Parameter                                      | Description                 |
445| ---------------------------------------- | ------------------- |
446| Promise&lt;[KeyboardType](#keyboardtype9)&gt; | Promise used to return the result.|
447
448**Example**
449
450```js
451// Query the keyboard type of the input device whose ID is 1.
452try {
453  inputDevice.getKeyboardType(1).then((number) => {
454    console.log("The keyboard type of the device is: " + number);
455  });
456} catch (error) {
457  console.info("getKeyboardType " + error.code + " " + error.message);
458}
459```
460
461## DeviceListener<sup>9+</sup>
462
463Defines the listener for hot swap events of an input device.
464
465**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
466
467| Name       | Type  | Readable  | Writable  | Description     |
468| --------- | ------ | ---- | ---- | ------- |
469| type     | [ChangedType](#changedtype) | Yes| No| Device change type, which indicates whether an input device is inserted or removed.|
470| deviceId | number                      | Yes| No| Unique ID of the input device. If the same physical device is repeatedly inserted and removed, its ID changes.|
471
472## InputDeviceData
473
474Defines the information about an input device.
475
476**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
477
478| Name       | Type  | Readable  | Writable  | Description     |
479| --------- | ------ | ---- | ---- | ------- |
480| id                   | number                                 | Yes| No| Unique ID of the input device. If the same physical device is repeatedly inserted and removed, its ID changes.|
481| name                 | string                                 | Yes| No| Name of the input device.                                            |
482| sources              | Array&lt;[SourceType](#sourcetype)&gt; | Yes| No| Source type of the input device. For example, if a keyboard is attached with a touchpad, the device has two input sources: keyboard and touchpad.|
483| axisRanges           | Array&lt;[AxisRange](#axisrange)&gt;  | Yes| No| Axis information of the input device.                                          |
484| bus<sup>9+</sup>     | number                                 | Yes| No| Bus type of the input device.                                        |
485| product<sup>9+</sup> | number                                 | Yes| No| Product information of the input device.                                        |
486| vendor<sup>9+</sup>  | number                                 | Yes| No| Vendor information of the input device.                                        |
487| version<sup>9+</sup> | number                                 | Yes| No| Version information of the input device.                                        |
488| phys<sup>9+</sup>    | string                                 | Yes| No| Physical address of the input device.                                        |
489| uniq<sup>9+</sup>    | string                                 | Yes| No| Unique ID of the input device.                                        |
490
491## AxisType<sup>9+</sup>
492
493Defines the axis type of an input device.
494
495**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
496
497| Name       | Type  | Readable  | Writable  | Description     |
498| --------- | ------ | ---- | ---- | ------- |
499| touchmajor  | string | Yes| No| touchmajor axis. |
500| touchminor  | string | Yes| No| touchminor axis. |
501| toolminor   | string | Yes| No| toolminor axis.  |
502| toolmajor   | string | Yes| No| toolmajor axis.  |
503| orientation | string | Yes| No| Orientation axis.|
504| pressure    | string | Yes| No| Pressure axis.   |
505| x           | string | Yes| No| X axis.          |
506| y           | string | Yes| No| Y axis.          |
507| NULL        | string | Yes| No| None.             |
508
509## AxisRange
510
511Defines the axis range of an input device.
512
513**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
514
515| Name       | Type  | Readable  | Writable  | Description     |
516| --------- | ------ | ---- | ---- | ------- |
517| source                  | [SourceType](#sourcetype) | Yes| No| Input source type of the axis.|
518| axis                    | [AxisType](#axistype9)    | Yes| No| Axis type.   |
519| max                     | number                    | Yes| No| Maximum value of the axis.  |
520| min                     | number                    | Yes| No| Minimum value of the axis.  |
521| fuzz<sup>9+</sup>       | number                    | Yes| No| Fuzzy value of the axis.  |
522| flat<sup>9+</sup>       | number                    | Yes| No| Benchmark value of the axis.  |
523| resolution<sup>9+</sup> | number                    | Yes| No| Resolution of the axis.  |
524
525## SourceType<sup>9+</sup>
526
527Enumerates the input source types. For example, if a mouse reports an x-axis event, the source of the x-axis is the mouse.
528
529**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
530
531| Name       | Type  | Readable  | Writable  | Description     |
532| --------- | ------ | ---- | ---- | ------- |
533| keyboard    | string | Yes| No| The input device is a keyboard. |
534| touchscreen | string | Yes| No| The input device is a touchscreen.|
535| mouse       | string | Yes| No| The input device is a mouse. |
536| trackball   | string | Yes| No| The input device is a trackball.|
537| touchpad    | string | Yes| No| The input device is a touchpad.|
538| joystick    | string | Yes| No| The input device is a joystick.|
539
540## ChangedType<sup>9+</sup>
541
542Defines the change type for the hot swap event of an input device.
543
544**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
545
546| Name       | Type  | Readable  | Writable  | Description     |
547| --------- | ------ | ---- | ---- | ------- |
548| add    | string | Yes| No| An input device is inserted.|
549| remove | string | Yes| No| An input device is removed.|
550
551## KeyboardType<sup>9+</sup>
552
553Enumerates the keyboard types.
554
555**System capability**: SystemCapability.MultimodalInput.Input.InputDevice
556
557| Name                 | Value   | Description       |
558| ------------------- | ---- | --------- |
559| NONE                | 0    | Keyboard without keys. |
560| UNKNOWN             | 1    | Keyboard with unknown keys.|
561| ALPHABETIC_KEYBOARD | 2    | Full keyboard. |
562| DIGITAL_KEYBOARD    | 3    | Keypad. |
563| HANDWRITING_PEN     | 4    | Stylus. |
564| REMOTE_CONTROL      | 5    | Remote control. |
565