js-apis-inputdevice.md 5.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

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

Z
zengyawen 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

**示例:** 

```
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");
}
```

M
mayunteng_1 已提交
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
## inputDevice.getDeviceIds

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

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

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

**返回值:**

| 参数 | 说明 |
| -------- | -------- |
| Promise<Array<number>> | Promise实例,用于异步获取结果 |

**示例:**

```
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 已提交
81 82 83 84 85 86 87 88 89 90 91

## inputDevice.getDevice

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

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

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

**参数:** 

M
mayunteng_1 已提交
92
| 参数 | 类型 | 必填 | 说明 |
Z
zengyawen 已提交
93
| -------- | -------- | -------- | -------- |
M
mayunteng_1 已提交
94 95
| deviceId | number | 是 | 需要获取信息的设备id。 |
| callback | AsyncCallback&lt;[InputDeviceData](#inputdevicedata)&gt; | 是 | 回调函数,异步返回InputDeviceData对象。 |
Z
zengyawen 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

**示例:** 

```
InputDeviceData {
    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");
}
```

M
mayunteng_1 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
## inputDevice.getDevice

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

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

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

**返回值:**

| 参数 | 说明 |
| -------- | -------- |
| Promise<InputDeviceData> | Promise实例,用于异步获取结果 |

**示例:**

```
InputDeviceData {
    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));
 	});
}
```


Z
zengyawen 已提交
153 154 155 156 157

## InputDeviceData

输入设备的描述信息。

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

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

M
mayunteng_1 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
## AxisType

轴类型,当前不支持此信息。

## AxisRange

输入设备的轴信息

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

| 名称   | 参数类型                  | 说明             |
| ------ | ------------------------- | ---------------- |
| source | [SourceType](#sourcetype) | 轴的输入源类型。 |
| axis   | [AxisType](axistype)      | 轴的类型         |
| max    | number                    | 轴上报的最大值   |
| min    | number                    | 轴上报的最小值   |


Z
zengyawen 已提交
184 185 186 187 188

## SourceType

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

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

M
mayunteng_1 已提交
191
| 名称 | 参数类型 | 说明 |
Z
zengyawen 已提交
192 193
| -------- | -------- | -------- |
| keyboard | string | 表示输入设备是键盘。 |
M
mayunteng_1 已提交
194
| touchscreen | string | 表示输入设备是触摸屏。 |
Z
zengyawen 已提交
195 196 197 198
| mouse | string | 表示输入设备是鼠标。 |
| trackball | string | 表示输入设备是轨迹球。 |
| touchpad | string | 表示输入设备是触摸板。 |
| joystick | string | 表示输入设备是操纵杆。 |