# Input Device The Input Device module implements listening for connection, disconnection, and update events of input devices and displays 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 ```js import inputDevice from '@ohos.multimodalInput.inputDevice'; ``` ## inputDevice.getDeviceList9+ getDeviceList(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** ```js try { inputDevice.getDeviceList((error, ids) => { if (error) { console.log(`Failed to get device list. error code=${JSON.stringify(err.code)} msg=${JSON.stringify(err.message)}`); return; } this.data = ids; console.log("The device ID list is: " + ids); }); } catch (error) { console.info("getDeviceList " + error.code + " " + error.message); } ``` ## inputDevice.getDeviceList9+ getDeviceList(): 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** | Name | Description | | ---------------------------------- | ------------------------------- | | Promise<Array<number>> | Promise used to return the result.| **Example** ```js try { inputDevice.getDeviceList().then((ids) => { console.log("The device ID list is: " + ids); }); } catch (error) { console.info("getDeviceList " + error.code + " " + error.message); } ``` ## inputDevice.getDeviceInfo9+ getDeviceInfo(deviceId: number, callback: AsyncCallback<InputDeviceData>): void Obtains 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. | | callback | AsyncCallback<[InputDeviceData](#inputdevicedata)> | Yes | Callback used to return the result, which is an **InputDeviceData** object.| **Example** ```js // Obtain the name of the device whose ID is 1. try { inputDevice.getDeviceInfo(1, (error, inputDevice) => { if (error) { console.log(`Failed to get device information. error code=${JSON.stringify(err.code)} msg=${JSON.stringify(err.message)}`); return; } console.log("The device name is: " + inputDevice.name); }); } catch (error) { console.info("getDeviceInfo " + error.code + " " + error.message); } ``` ## inputDevice.getDeviceInfo9+ getDeviceInfo(deviceId: number): Promise<InputDeviceData> Obtains 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** | Name | Description | | -------------------------------------------------- | ------------------------------- | | Promise<[InputDeviceData](#inputdevicedata)> | Promise used to return the result.| **Example** ```js // Obtain the name of the device whose ID is 1. try { inputDevice.getDeviceInfo(id).then((inputDevice) => { console.log("The device name is: " + inputDevice.name); }); } catch (error) { console.info("getDeviceInfo " + error.code + " " + error.message); } ``` ## inputDevice.on9+ on(type: "change", listener: Callback<DeviceListener>): void Enables listening for hot swap events of an input device. **System capability**: SystemCapability.MultimodalInput.Input.InputDevice **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ----------- | | type | string | Yes | Event type of the input device. | | listener | Callback<[DeviceListener](#devicelistener9)> | Yes | Listener for events of the input device.| **Example** ```js let isPhysicalKeyboardExist = true; try { inputDevice.on("change", (data) => { console.log("type: " + data.type + ", deviceId: " + data.deviceId); inputDevice.getKeyboardType(data.deviceId, (err, ret) => { console.log("The keyboard type of the device is: " + ret); if (ret == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'add') { // The physical keyboard is connected. isPhysicalKeyboardExist = true; } else if (ret == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'remove') { // The physical keyboard is disconnected. isPhysicalKeyboardExist = false; } }); }); // Check whether the soft keyboard is open based on the value of isPhysicalKeyboardExist. } catch (error) { console.info("oninputdevcie " + error.code + " " + error.message); } ``` ## inputDevice.off9+ off(type: "change", listener?: Callback<DeviceListener>): void Disables listening for hot swap events of an input device. **System capability**: SystemCapability.MultimodalInput.Input.InputDevice **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ----------- | | type | string | Yes | Event type of the input device. | | listener | Callback<[DeviceListener](#devicelistener9)> | No | Listener for events of the input device.| **Example** ```js callback: function(data) { console.log("type: " + data.type + ", deviceId: " + data.deviceId); } try { inputDevice.on("change", this.callback); } catch (error) { console.info("oninputdevcie " + error.code + " " + error.message) } // Enable listening for hot swap events of an input device. inputDevice.on("change", listener); // Disable this listener. try { inputDevice.off("change", this.callback); } catch (error) { console.info("offinputdevcie " + error.code + " " + error.message) } // Disable all listeners. try { inputDevice.off("change"); } catch (error) { console.info("offinputdevcie " + error.code + " " + error.message); } // By default, the soft keyboard is closed when listening is disabled. ``` ## inputDevice.getDeviceIds(deprecated) getDeviceIds(callback: AsyncCallback<Array<number>>): void Obtains the IDs of all input devices. This API uses an asynchronous callback to return the result. This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceList](#inputdevicegetdevicelist9) instead. **System capability**: SystemCapability.MultimodalInput.Input.InputDevice **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ----- | | callback | AsyncCallback<Array<number>> | Yes | Callback used to return the result.| **Example** ```js inputDevice.getDeviceIds((ids)=>{ console.log("The device ID list is: " + ids); }); ``` ## inputDevice.getDeviceIds(deprecated) getDeviceIds(): Promise<Array<number>> Obtains the IDs of all input devices. This API uses a promise to return the result. This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceList](#inputdevicegetdevicelist9) instead. **System capability**: SystemCapability.MultimodalInput.Input.InputDevice **Return value** | Parameter | Description | | ---------------------------------- | ------------------- | | Promise<Array<number>> | Promise used to return the result.| **Example** ```js inputDevice.getDeviceIds().then((ids)=>{ console.log("The device ID list is: " + ids); }); ``` ## inputDevice.getDevice(deprecated) getDevice(deviceId: number, callback: AsyncCallback<InputDeviceData>): void Obtains information about an input device. This API uses an asynchronous callback to return the result. This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceInfo](#inputdevicegetdeviceinfo9) instead. **System capability**: SystemCapability.MultimodalInput.Input.InputDevice **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | --------------------------- | | deviceId | number | Yes | ID of the input device. | | callback | AsyncCallback<[InputDeviceData](#inputdevicedata)> | Yes | Callback used to return the result, which is an **InputDeviceData** object.| **Example** ```js // Obtain the name of the device whose ID is 1. inputDevice.getDevice(1, (inputDevice)=>{ console.log("The device name is: " + inputDevice.name); }); ``` ## inputDevice.getDevice(deprecated) getDevice(deviceId: number): Promise<InputDeviceData> Obtains information about an input device. This API uses a promise to return the result. This API is deprecated since API version 9. You are advised to use [inputDevice.getDeviceInfo](#inputdevicegetdeviceinfo9) instead. **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](#inputdevicedata)> | Promise used to return the result.| **Example** ```js // Obtain the name of the device whose ID is 1. inputDevice.getDevice(1).then((inputDevice)=>{ console.log("The device name is: " + inputDevice.name); }); ``` ## inputDevice.supportKeys9+ supportKeys(deviceId: number, keys: Array<KeyCode>, callback: AsyncCallback <Array<boolean>>): void Obtains the key codes supported by the 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 | Unique ID of the input device. If the same physical device is repeatedly inserted and removed, its ID changes.| | keys | Array<KeyCode> | Yes | Key codes to be queried. A maximum of five key codes can be specified. | | callback | AsyncCallback<Array<boolean>> | Yes | Callback used to return the result. | **Example** ```js // Check whether the input device whose ID is 1 supports key codes 17, 22, and 2055. try { inputDevice.supportKeys(1, [17, 22, 2055], (error, ret) => { console.log("The query result is as follows: " + ret); }); } catch (error) { console.info("supportKeys " + error.code + " " + error.message); } ``` ## inputDevice.supportKeys9+ supportKeys(deviceId: number, keys: Array<KeyCode>): Promise<Array<boolean>> Obtains the key codes supported by the 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 | Unique ID of the input device. If the same physical device is repeatedly inserted and removed, its ID changes.| | keys | Array<KeyCode> | Yes | Key codes to be queried. A maximum of five key codes can be specified. | **Return value** | Parameter | Description | | ----------------------------------- | ------------------- | | Promise<Array<boolean>> | Promise used to return the result.| **Example** ```js // Check whether the input device whose ID is 1 supports key codes 17, 22, and 2055. try { inputDevice.supportKeys(1, [17, 22, 2055]).then((ret) => { console.log("The query result is as follows: " + ret); }); } catch (error) { console.info("supportKeys " + error.code + " " + error.message); } ``` ## inputDevice.getKeyboardType9+ getKeyboardType(deviceId: number, callback: AsyncCallback<KeyboardType>): void Obtains the keyboard type of 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 | Unique ID of the input device. If the same physical device is repeatedly inserted and removed, its ID changes.| | callback | AsyncCallback<[KeyboardType](#keyboardtype9)> | Yes | Callback used to return the result. | **Example** ```js // Query the keyboard type of the input device whose ID is 1. try { inputDevice.getKeyboardType(1, (error, number) => { if (error) { console.log(`Failed to get keyboardtype. error code=${JSON.stringify(err.code)} msg=${JSON.stringify(err.message)}`); return; } console.log("The keyboard type of the device is: " + number); }); } catch (error) { console.info("getKeyboardType " + error.code + " " + error.message); } ``` ## inputDevice.getKeyboardType9+ getKeyboardType(deviceId: number,): Promise<KeyboardType> Obtains the keyboard type of an input device. This API uses a promise to return the result. **System capability**: SystemCapability.MultimodalInput.Input.InputDevice **Return value** | Parameter | Description | | ---------------------------------------- | ------------------- | | Promise<[KeyboardType](#keyboardtype9)> | Promise used to return the result.| **Example** ```js // Query the keyboard type of the input device whose ID is 1. try { inputDevice.getKeyboardType(1).then((number) => { console.log("The keyboard type of the device is: " + number); }); } catch (error) { console.info("getKeyboardType " + error.code + " " + error.message); } ``` ## DeviceListener9+ Defines the listener for hot swap events of an input device. **System capability**: SystemCapability.MultimodalInput.Input.InputDevice | Name | Type | Description | | -------- | ------------------------- | --------------------------------- | | type | [ChangedType](#changedtype) | Device change type, which indicates whether an input device is inserted or removed. | | deviceId | number | Unique ID of the input device. If the same physical device is repeatedly inserted and removed, its ID changes.| ## 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](#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](#axisrange)> | Axis information of the input device. | | bus9+ | number | Bus type of the input device. | | product9+ | number | Product information of the input device. | | vendor9+ | number | Vendor information of the input device. | | version9+ | number | Version information of the input device. | | phys9+ | string | Physical address of the input device. | | uniq9+ | string | Unique ID of the input device. | ## AxisType9+ Defines the axis type of an input device. **System capability**: SystemCapability.MultimodalInput.Input.InputDevice | Name | Type | Description | | ----------- | ------ | --------------- | | touchMajor | string | touchMajor axis. | | touchMinor | string | touchMinor axis. | | toolMinor | string | toolMinor axis. | | toolMajor | string | toolMajor axis. | | orientation | string | Orientation axis.| | pressure | string | Pressure axis. | | x | string | X axis. | | y | string | Y axis. | | NULL | string | None. | ## AxisRange Defines the axis range of an input device. **System capability**: SystemCapability.MultimodalInput.Input.InputDevice | Name | Type | Description | | ----------------------- | ------------------------- | -------- | | source | [SourceType](#sourcetype) | Input source type of the axis.| | axis | [AxisType](#axistype9) | Axis type. | | max | number | Maximum value of the axis. | | min | number | Minimum value of the axis. | | fuzz9+ | number | Fuzzy value of the axis. | | flat9+ | number | Benchmark value of the axis. | | resolution9+ | number | Resolution of the axis. | ## SourceType Enumerates the input source types. 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.| ## ChangedType Defines the change type for the hot swap event of an input device. **System capability**: SystemCapability.MultimodalInput.Input.InputDevice | Name | Type | Description | | ------ | ------ | --------- | | add | string | An input device is inserted.| | remove | string | An input device is removed.| ## KeyboardType9+ Enumerates the keyboard types. **System capability**: SystemCapability.MultimodalInput.Input.InputDevice | Name | Type | Value | Description | | ------------------- | ------ | ---- | --------- | | NONE | number | 0 | Keyboard without keys. | | UNKNOWN | number | 1 | Keyboard with unknown keys.| | ALPHABETIC_KEYBOARD | number | 2 | Full keyboard. | | DIGITAL_KEYBOARD | number | 3 | Keypad. | | HANDWRITING_PEN | number | 4 | Stylus. | | REMOTE_CONTROL | number | 5 | Remote control. |