• 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** API to obtain the list of connected input devices. Call the **getKeyboardType** 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** 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.
323. When a user enters text, check whether a physical keyboard is connected. If a physical keyboard is not connected, launch the virtual keyboard.
33
34
35```js
36import inputDevice from '@ohos.multimodalInput.inputDevice';
37
38let isPhysicalKeyboardExist = true;
39try {
40  // 1. Obtain the list of input devices and check whether a physical keyboard is connected.
41  inputDevice.getDeviceList().then(data => {
42    for (let i = 0; i < data.length; ++i) {
43      inputDevice.getKeyboardType(data[i]).then(type => {
44        if (type === inputDevice.KeyboardType.ALPHABETIC_KEYBOARD) {
45          // The physical keyboard is connected.
46          isPhysicalKeyboardExist = true;
47        }
48      });
49    }
50  });
51  // 2. Listen for device hot swap events.
52  inputDevice.on("change", (data) => {
53    console.log(`Device event info: ${JSON.stringify(data)}`);
54    inputDevice.getKeyboardType(data.deviceId).then((type) => {
55      console.log("The keyboard type is: " + type);
56      if (type === inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'add') {
57        // The physical keyboard is inserted.
58        isPhysicalKeyboardExist = true;
59      } else if (type == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'remove') {
60        // The physical keyboard is removed.
61        isPhysicalKeyboardExist = false;
62      }
63    });
64  });
65} catch (error) {
66  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
67}
68  // 3. Determine whether to launch the virtual keyboard based on the value of isPhysicalKeyboardExist.
69  // TODO
70```
71