• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Input Device Development
2
3## When to Use
4
5Input 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.
6
7## Modules to Import
8
9```js
10import inputDevice from '@ohos.multimodalInput.inputDevice';
11```
12
13## Available APIs
14
15The following table lists the common APIs for input device management. For details about the APIs, see [ohos.multimodalInput.inputDevice](../reference/apis/js-apis-inputdevice.md).
16
17| Instance| API | Description|
18| ----------- | ------------------------------------------------------------ | -------------------------- |
19| inputDevice | function getDeviceList(): Promise\<Array\<number>>; | Obtains the list of input devices.|
20| inputDevice | function getKeyboardType(deviceId: number): Promise\<KeyboardType>; | Obtains the keyboard type of the input device.|
21| inputDevice | function on(type: "change", listener: Callback\<DeviceListener>): void; | Enables listening for device hot swap events.|
22| inputDevice | function off(type: "change", listener?: Callback\<DeviceListener>): void; | Disables listening for device hot swap events.|
23
24## Virtual Keyboard Detection
25
26When 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.
27
28## How to Develop
29
301. Call the [getDeviceList](../reference/apis/js-apis-inputdevice.md#inputdevicegetdevicelist9) API to obtain the list of connected input devices. Call the [getKeyboardType](../reference/apis/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.
312. Call the [on](../reference/apis/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.
32
33
34```js
35import inputDevice from '@ohos.multimodalInput.inputDevice';
36
37let isPhysicalKeyboardExist = true;
38try {
39  // 1. Obtain the list of input devices and check whether a physical keyboard is connected.
40  inputDevice.getDeviceList().then(data => {
41    for (let i = 0; i < data.length; ++i) {
42      inputDevice.getKeyboardType(data[i]).then(type => {
43        if (type === inputDevice.KeyboardType.ALPHABETIC_KEYBOARD) {
44          // The physical keyboard is connected.
45          isPhysicalKeyboardExist = true;
46        }
47      });
48    }
49  });
50  // 2. Listen for device hot swap events.
51  inputDevice.on("change", (data) => {
52    console.log(`Device event info: ${JSON.stringify(data)}`);
53    inputDevice.getKeyboardType(data.deviceId).then((type) => {
54      console.log("The keyboard type is: " + type);
55      if (type === inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'add') {
56        // The physical keyboard is inserted.
57        isPhysicalKeyboardExist = true;
58      } else if (type == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'remove') {
59        // The physical keyboard is removed.
60        isPhysicalKeyboardExist = false;
61      }
62    });
63  });
64} catch (error) {
65  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
66}
67```
68