js-apis-inputdevice.md 7.8 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6
# 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.


S
shawn_he 已提交
7
> **NOTE**<br>
Z
zengyawen 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
> 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&lt;Array&lt;number&gt;&gt;): void

Z
zengyawen 已提交
23
Obtains the IDs of all input devices. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
24 25 26

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

Z
zengyawen 已提交
27
**Parameters**
Z
zengyawen 已提交
28

Z
zengyawen 已提交
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
| Name      | Type                                      | Mandatory  | Description   |
| -------- | ---------------------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;Array&lt;number&gt;&gt; | Yes   | Callback used to return the result.|


**Example**

```
export default {
    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.getDeviceIds

function getDeviceIds(): Promise<Array<number>>

Obtains the IDs of all input devices. This API uses a promise to return the result.

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

**Return value**

S
shawn_he 已提交
62
| Parameter                    | Description                |
Z
zengyawen 已提交
63 64 65 66
| ---------------------- | ------------------ |
| Promise<Array<number>> | Promise used to return the result.|

**Example**
Z
zengyawen 已提交
67 68

```
Z
zengyawen 已提交
69 70 71 72 73 74 75 76 77 78
export default {
    testGetDeviceIds: function () {
        console.info("InputDeviceJsTest---start---testGetDeviceIds");
        let promise = inputDevice.getDeviceIds();
        promise.then((data)=> {
            console.info('GetDeviceIds successed, Data: ' + JSON.stringify(data))
        }).catch((err)=>{
            console.error('Failed GetDeviceIds. Cause: ' + JSON.stringify(err));
        });
    }
Z
zengyawen 已提交
79 80 81 82
}
```


Z
zengyawen 已提交
83 84 85



Z
zengyawen 已提交
86 87 88 89
## inputDevice.getDevice

getDevice(deviceId: number, callback: AsyncCallback&lt;InputDeviceData&gt;): void

Z
zengyawen 已提交
90
Obtains the information about an input device. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
91 92 93

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

Z
zengyawen 已提交
94 95 96 97 98 99
**Parameters**

| Name      | Type                                      | Mandatory  | Description                         |
| -------- | ---------------------------------------- | ---- | --------------------------- |
| deviceId | number                                   | Yes   | ID of the input device whose information is to be obtained.               |
| callback | AsyncCallback&lt;[InputDeviceData](#inputdevicedata)&gt; | Yes   | Callback used to return the **InputDeviceData** object.|
Z
zengyawen 已提交
100

Z
zengyawen 已提交
101
**Example**
Z
zengyawen 已提交
102 103

```
Z
zengyawen 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
export default {
    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");
    }
Z
zengyawen 已提交
120 121 122
}
```

Z
zengyawen 已提交
123 124 125 126 127 128 129 130 131 132
## inputDevice.getDevice

function getDevice(deviceId: number): Promise<InputDeviceData>

Obtains the information about an input device. This API uses a promise to return the result.

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

**Return value**

S
shawn_he 已提交
133
| Parameter                      | Description                |
Z
zengyawen 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| ------------------------ | ------------------ |
| Promise<InputDeviceData> | Promise used to return the result.|

**Example**

```
export default {
    InputDeviceData: {
        deviceId : 0,
        name : "NA",
        sources : Array,
        axisRanges : Array,
    },
    testGetDevice: function () {
        // The example is used to obtain the information about the device whose ID is 1.
        console.info("InputDeviceJsTest---start---testGetDevice");
        let promise = inputDevice.getDevice(1);
        promise.then((data)=> {
            console.info('GetDeviceId successed, Data: ' + JSON.stringify(data))
        }).catch((err)=>{
            console.error('Failed GetDeviceId. Cause: ' + JSON.stringify(err));
        });
    }
}
```


Z
zengyawen 已提交
161 162 163 164 165

## InputDeviceData

Defines the information about an input device.

Z
zengyawen 已提交
166 167
**System capability**: SystemCapability.MultimodalInput.Input.InputDevice

S
shawn_he 已提交
168 169 170 171 172 173 174 175 176 177 178 179
| 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&lt;[SourceType](#sourcetype)&gt; | 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.|
| axisRanges | Array&lt;[axisRanges](#axisrange)&gt;  | Axis information of the input device.                                          |
| bus        | number                                 | Bus type of the input device.                                        |
| product    | number                                 | Product information of the input device.                                        |
| vendor     | number                                 | Vendor information of the input device.                                        |
| version    | number                                 | Version information of the input device.                                        |
| phys       | string                                 | Physical address of the input device.                                        |
| uniq       | string                                 | Unique ID of the input device.                                        |
Z
zengyawen 已提交
180 181 182

## AxisType

S
shawn_he 已提交
183
Defines the axis type of an input device, which is **NULL**.
Z
zengyawen 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197

## AxisRange

Defines the axis information of an input device.

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

| Name    | Type                     | Description      |
| ------ | ------------------------- | -------- |
| source | [SourceType](#sourcetype) | Input source type of the axis.|
| axis   | [AxisType](axistype)      | Axis type.    |
| max    | number                    | Maximum value reported by the axis. |
| min    | number                    | Minimum value reported by the axis. |

Z
zengyawen 已提交
198 199 200 201


## SourceType

Z
zengyawen 已提交
202
Enumerates the input source types. For example, if a mouse reports an x-axis event, the source of the x-axis is the mouse.
Z
zengyawen 已提交
203 204 205

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

Z
zengyawen 已提交
206 207 208 209 210 211 212 213
| 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.|