You need to sign in or sign up before continuing.
js-apis-inputdevice.md 3.6 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
# Input Device


The input device management module is used to listen for the connection, disconnection, and updates of input devices and display 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.


> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> 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.


## Modules to Import


```
import inputDevice from '@ohos.multimodalInput.inputDevice';
```


## inputDevice.getDeviceIds

getDeviceIds(callback: AsyncCallback<Array<number>>): void

Obtains the IDs of all input devices. This method uses an asynchronous callback to return the result.

**System capability**: SystemCapability.MultimodalInput.Input.InputDevice

  **Parameters**
  | Name | Type | Mandatory | Description | 
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<Array<number>> | Yes | Callback used to return the result. | 

  **Example**

```
data: {
    deviceIds: Array,
},
callback: function(ids) {
    this.deviceIds = ids;
},
testGetDeviceIds: function () {
    console.info("InputDeviceJsTest---start---testGetDeviceIds");
    inputDevice.getDeviceIds(this.callback);
    console.info("InputDeviceJsTest---end---testGetDeviceIds");
}
```


## inputDevice.getDevice

getDevice(deviceId: number, callback: AsyncCallback<InputDeviceData>): void

Obtains the information about an input device. This method uses an asynchronous callback to return the result.

**System capability**: SystemCapability.MultimodalInput.Input.InputDevice

  **Parameters**
  | Name | Type | Mandatory | Description | 
| -------- | -------- | -------- | -------- |
| deviceId | number | Yes | ID of the input device whose information is to be obtained. | 
| callback | AsyncCallback<[InputDeviceData](#inputdevicedata)> | Yes | Callback used to return the **InputDeviceData** object. | 

  **Example**

```
InputDeviceData {
    deviceId : 0,
    name : "NA",
    sources : Array,
    axisRanges : Array,
},
callback: function(deviceData) {
    this.InputDeviceData = deviceData;
},
testGetDevice: function () {
    // The example is used to obtain the information about the device whose ID is 1.
    console.info("InputDeviceJsTest---start---testGetDevice");
    inputDevice.getDevice(1, this.callback);
    console.info("InputDeviceJsTest---end---testGetDevice");
}
```


## InputDeviceData

Defines the information about an input device.

  **System capability**: SystemCapability.MultimodalInput.Input.InputDevice
  | Name | Type | Description | 
| -------- | -------- | -------- |
| id | number | Unique identifier of an input device. If the same physical device is repeatedly inserted and removed, its ID changes. | 
| name | string | Name of the input device. | 
| sources | Array<[SourceType](#sourcetype)> | Source types of the input device. For example, if a keyboard is attached with a touchpad, the device has two input sources: keyboard and touchpad. |


## SourceType

Enumerates the input source types.

**System capability**: SystemCapability.MultimodalInput.Input.InputDevice

  | Name | Type | Description | 
| -------- | -------- | -------- |
| keyboard | string | The input device is a keyboard. |
| touchscreen | string | The input device is a touchscreen. | 
| mouse | string | The input device is a mouse. |
| trackball | string | The input device is a trackball. |
| touchpad | string | The input device is a touchpad. |
| joystick | string | The input device is a joystick. |