js-apis-inputdevice.md 6.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
# 输入设备


输入设备管理模块,用于监听输入设备连接、断开和变化,并查看输入设备相关信息。比如监听鼠标插拔,并获取鼠标的id、name和指针移动速度等信息。


> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。


## 导入模块


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


## inputDevice.getDeviceIds

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

获取所有输入设备的id列表,使用callback方式作为异步方法。

**系统能力:** SystemCapability.MultimodalInput.Input.InputDevice

M
mayunteng_1 已提交
27
**参数:**
Z
zengyawen 已提交
28

H
HelloCrease 已提交
29 30 31
| 参数       | 类型                                       | 必填   | 说明    |
| -------- | ---------------------------------------- | ---- | ----- |
| callback | AsyncCallback<Array<number>> | 是    | 回调函数。 |
M
mayunteng_1 已提交
32

Z
zengyawen 已提交
33 34 35 36

**示例:** 

```
H
HelloCrease 已提交
37 38 39 40 41 42 43 44 45 46 47 48
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");
    }
Z
zengyawen 已提交
49 50 51
}
```

M
mayunteng_1 已提交
52 53 54 55 56 57 58 59 60 61
## inputDevice.getDeviceIds

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

获取所有输入设备的id列表,使用Promise方式作为异步方法。

**系统能力:** SystemCapability.MultimodalInput.Input.InputDevice

**返回值:**

H
HelloCrease 已提交
62 63
| 参数                     | 说明                 |
| ---------------------- | ------------------ |
M
mayunteng_1 已提交
64 65 66 67 68
| Promise<Array<number>> | Promise实例,用于异步获取结果 |

**示例:**

```
H
HelloCrease 已提交
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));
        });
    }
M
mayunteng_1 已提交
79 80 81 82 83 84
}
```




Z
zengyawen 已提交
85 86 87 88 89 90 91 92 93 94 95

## inputDevice.getDevice

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

获取输入设备的描述信息,使用callback方式作为异步方法。

**系统能力:** SystemCapability.MultimodalInput.Input.InputDevice

**参数:** 

H
HelloCrease 已提交
96 97 98 99
| 参数       | 类型                                       | 必填   | 说明                          |
| -------- | ---------------------------------------- | ---- | --------------------------- |
| deviceId | number                                   | 是    | 需要获取信息的设备id。                |
| callback | AsyncCallback&lt;[InputDeviceData](#inputdevicedata)&gt; | 是    | 回调函数,异步返回InputDeviceData对象。 |
Z
zengyawen 已提交
100 101 102 103

**示例:** 

```
H
HelloCrease 已提交
104
export default {
105
    InputDeviceData: {
H
HelloCrease 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119
        deviceId : 0,
        name : "NA",
        sources : Array,
        axisRanges : Array,
    },
    callback: function(deviceData) {
        this.InputDeviceData = deviceData;
    },
    testGetDevice: function () {
        // 示例获取设备id为1的设备信息。
        console.info("InputDeviceJsTest---start---testGetDevice");
        inputDevice.getDevice(1, this.callback);
        console.info("InputDeviceJsTest---end---testGetDevice");
    }
Z
zengyawen 已提交
120 121 122
}
```

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

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

获取输入设备的描述信息,使用Promise方式作为异步方法。

**系统能力:** SystemCapability.MultimodalInput.Input.InputDevice

**返回值:**

H
HelloCrease 已提交
133 134
| 参数                       | 说明                 |
| ------------------------ | ------------------ |
M
mayunteng_1 已提交
135 136 137 138 139
| Promise<InputDeviceData> | Promise实例,用于异步获取结果 |

**示例:**

```
H
HelloCrease 已提交
140
export default {
141
    InputDeviceData: {
H
HelloCrease 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        deviceId : 0,
        name : "NA",
        sources : Array,
        axisRanges : Array,
    },
    testGetDevice: function () {
        // 示例获取设备id为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));
        });
    }
M
mayunteng_1 已提交
157 158 159 160
}
```


Z
zengyawen 已提交
161 162 163 164 165

## InputDeviceData

输入设备的描述信息。

M
mayunteng_1 已提交
166
**系统能力:**  以下各项对应的系统能力均为SystemCapability.MultimodalInput.Input.InputDevice
Z
zengyawen 已提交
167

H
HelloCrease 已提交
168 169 170 171
| 名称      | 参数类型                                   | 说明                                       |
| ------- | -------------------------------------- | ---------------------------------------- |
| id      | number                                 | 输入设备的唯一标识,同一个物理设备反复插拔,其设备id会发生变化。        |
| name    | string                                 | 输入设备的名字。                                 |
Z
zengyawen 已提交
172 173
| sources | Array&lt;[SourceType](#sourcetype)&gt; | 输入设备支持的源类型。比如有的键盘上附带触摸板,则此设备有keyboard和touchpad两种输入源。 |

M
mayunteng_1 已提交
174 175
## AxisType

176
轴类型,本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。
M
mayunteng_1 已提交
177 178 179 180 181 182 183

## AxisRange

输入设备的轴信息

**系统能力:**  以下各项对应的系统能力均为SystemCapability.MultimodalInput.Input.InputDevice

H
HelloCrease 已提交
184 185
| 名称     | 参数类型                      | 说明       |
| ------ | ------------------------- | -------- |
M
mayunteng_1 已提交
186
| source | [SourceType](#sourcetype) | 轴的输入源类型。 |
H
HelloCrease 已提交
187 188 189
| axis   | [AxisType](axistype)      | 轴的类型     |
| max    | number                    | 轴上报的最大值  |
| min    | number                    | 轴上报的最小值  |
M
mayunteng_1 已提交
190 191


Z
zengyawen 已提交
192 193 194 195 196

## SourceType

定义这个轴的输入源类型。比如鼠标设备可上报x轴事件,则x轴的源就是鼠标。

M
mayunteng_1 已提交
197
**系统能力:**  以下各项对应的系统能力均为SystemCapability.MultimodalInput.Input.InputDevice
Z
zengyawen 已提交
198

H
HelloCrease 已提交
199 200 201
| 名称          | 参数类型   | 说明          |
| ----------- | ------ | ----------- |
| keyboard    | string | 表示输入设备是键盘。  |
M
mayunteng_1 已提交
202
| touchscreen | string | 表示输入设备是触摸屏。 |
H
HelloCrease 已提交
203 204 205 206
| mouse       | string | 表示输入设备是鼠标。  |
| trackball   | string | 表示输入设备是轨迹球。 |
| touchpad    | string | 表示输入设备是触摸板。 |
| joystick    | string | 表示输入设备是操纵杆。 |