1e41f4b71Sopenharmony_ci# Input Device Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciInput device management provides functions such as listening for device hot swap events and querying the keyboard type of a specified device. For example, as a user enters text, the input method determines whether to launch the virtual keyboard based on whether a physical keyboard is currently inserted. Your application can determine whether a physical keyboard is inserted by listening to device hot swap events. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Modules to Import 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci```js 10e41f4b71Sopenharmony_ciimport { inputDevice } from '@kit.InputKit'; 11e41f4b71Sopenharmony_ci``` 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## Available APIs 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciThe following table lists the common APIs for input device management. For details about the APIs, see [ohos.multimodalInput.inputDevice](../../reference/apis-input-kit/js-apis-inputdevice.md). 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci| API | Description| 18e41f4b71Sopenharmony_ci| ----------- | ------------------------------------------------------------ | 19e41f4b71Sopenharmony_ci| getDeviceList(): Promise\<Array\<number>> | Obtains the list of input devices.| 20e41f4b71Sopenharmony_ci| getKeyboardType(deviceId: number): Promise\<KeyboardType> | Obtains the keyboard type of the input device.| 21e41f4b71Sopenharmony_ci| on(type: "change", listener: Callback\<DeviceListener>): void | Enables listening for device hot swap events.| 22e41f4b71Sopenharmony_ci| off(type: "change", listener?: Callback\<DeviceListener>): void | Disables listening for device hot swap events.| 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci## Virtual Keyboard Detection 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ciWhen a user enters text, the input method determines whether to launch the virtual keyboard based on whether a physical keyboard is currently inserted. Your application can determine whether a physical keyboard is inserted by listening to device hot swap events. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci## How to Develop 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci1. Call the [getDeviceList](../../reference/apis-input-kit/js-apis-inputdevice.md#inputdevicegetdevicelist9) API to obtain the list of connected input devices. Call the [getKeyboardType](../../reference/apis-input-kit/js-apis-inputdevice.md#inputdevicegetkeyboardtype9) API to traverse all connected devices to check whether a physical keyboard exists. If a physical keyboard exists, mark the physical keyboard as connected. This step ensures that your application detects all inserted input devices before listening for device hot swap events. 31e41f4b71Sopenharmony_ci2. Call the [on](../../reference/apis-input-kit/js-apis-inputdevice.md#inputdeviceon9) API to listen for device hot swap events. If a physical keyboard is inserted, mark the physical keyboard as connected. If a physical keyboard is removed, mark the physical keyboard as disconnected. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci```js 35e41f4b71Sopenharmony_ciimport { inputDevice } from '@kit.InputKit'; 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_cilet isPhysicalKeyboardExist = true; 38e41f4b71Sopenharmony_citry { 39e41f4b71Sopenharmony_ci // 1. Obtain the list of input devices and check whether a physical keyboard is connected. 40e41f4b71Sopenharmony_ci inputDevice.getDeviceList().then(data => { 41e41f4b71Sopenharmony_ci for (let i = 0; i < data.length; ++i) { 42e41f4b71Sopenharmony_ci inputDevice.getKeyboardType(data[i]).then(type => { 43e41f4b71Sopenharmony_ci if (type === inputDevice.KeyboardType.ALPHABETIC_KEYBOARD) { 44e41f4b71Sopenharmony_ci // The physical keyboard is connected. 45e41f4b71Sopenharmony_ci isPhysicalKeyboardExist = true; 46e41f4b71Sopenharmony_ci } 47e41f4b71Sopenharmony_ci }); 48e41f4b71Sopenharmony_ci } 49e41f4b71Sopenharmony_ci }); 50e41f4b71Sopenharmony_ci // 2. Listen for device hot swap events. 51e41f4b71Sopenharmony_ci inputDevice.on("change", (data) => { 52e41f4b71Sopenharmony_ci console.log(`Device event info: ${JSON.stringify(data)}`); 53e41f4b71Sopenharmony_ci inputDevice.getKeyboardType(data.deviceId).then((type) => { 54e41f4b71Sopenharmony_ci console.log("The keyboard type is: " + type); 55e41f4b71Sopenharmony_ci if (type === inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'add') { 56e41f4b71Sopenharmony_ci // The physical keyboard is inserted. 57e41f4b71Sopenharmony_ci isPhysicalKeyboardExist = true; 58e41f4b71Sopenharmony_ci } else if (type == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'remove') { 59e41f4b71Sopenharmony_ci // The physical keyboard is removed. 60e41f4b71Sopenharmony_ci isPhysicalKeyboardExist = false; 61e41f4b71Sopenharmony_ci } 62e41f4b71Sopenharmony_ci }); 63e41f4b71Sopenharmony_ci }); 64e41f4b71Sopenharmony_ci} catch (error) { 65e41f4b71Sopenharmony_ci console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 66e41f4b71Sopenharmony_ci} 67e41f4b71Sopenharmony_ci``` 68