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.
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 API 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
inputDevice.getDeviceIds((ids)=>{
console.log("The device ID list is: " + ids);
});
inputDevice.getDeviceIds
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
Parameter | Description |
---|---|
Promise<Array<number>> | Promise used to return the result. |
Example
inputDevice.getDeviceIds().then((ids)=>{
console.log("The device ID list is: " + ids);
});
inputDevice.getDevice
getDevice(deviceId: number, callback: AsyncCallback<InputDeviceData>): void
Obtains the information about an input device. This API 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> | Yes | Callback used to return the InputDeviceData object. |
Example
// This example obtains the information about the device whose ID is 1.
inputDevice.getDevice(1, (inputDevice)=>{
console.log("The device name is: " + inputDevice.name);
});
inputDevice.getDevice
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
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
deviceId | number | Yes | ID of the input device. |
Return value
Parameter | Description |
---|---|
Promise<InputDeviceData> | Promise used to return the result. |
Example
// This example obtains the information about the device whose ID is 1.
inputDevice.getDevice(1).then((inputDevice)=>{
console.log("The device name is: " + inputDevice.name);
});
InputDeviceData
Defines the information about an input device.
System capability: SystemCapability.MultimodalInput.Input.InputDevice
Name | Type | Description |
---|---|---|
id | number | Unique ID of the 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> | 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. |
axisRanges | Array<axisRanges> | Axis information of the input device. |
AxisType9+
Defines the axis type of an input device.
AxisRange
Defines the axis information of an input device.
System capability: SystemCapability.MultimodalInput.Input.InputDevice
Name | Type | Description |
---|---|---|
source | SourceType | Input source type of the axis. |
axis | AxisType | Axis type. |
max | number | Maximum value reported by the axis. |
min | number | Minimum value reported by the axis. |
SourceType
Enumerates the input source types of the axis. For example, if a mouse reports an x-axis event, the source of the x-axis is the mouse.
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. |