@ohos.multimodalInput.inputDevice (输入设备)

输入设备管理模块,用于监听输入设备连接和断开状态,查询输入设备相关信息。

说明

本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import { inputDevice } from '@kit.InputKit';

inputDevice.getDeviceList9+

getDeviceList(callback: AsyncCallback<Array<number>>): void

获取所有输入设备的id列表,使用AsyncCallback异步方式返回结果。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
callback AsyncCallback<Array<number>> 回调函数,异步返回所有输入设备的id列表。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

try {
  inputDevice.getDeviceList((error: Error, ids: Array<Number>) => {
    if (error) {
      console.log(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Device id list: ${JSON.stringify(ids)}`);
  });
} catch (error) {
  console.log(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDevice.getDeviceList9+

getDeviceList(): Promise<Array<number>>

获取所有输入设备的id列表,使用Promise异步方式返回结果。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

返回值

参数 说明
Promise<Array<number>> Promise对象,异步返回所有输入设备的id列表。

示例

try {
  inputDevice.getDeviceList().then((ids: Array<Number>) => {
    console.log(`Device id list: ${JSON.stringify(ids)}`);
  });
} catch (error) {
  console.log(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDevice.getDeviceInfo9+

getDeviceInfo(deviceId: number, callback: AsyncCallback<InputDeviceData>): void

获取指定输入设备的信息,使用AsyncCallback异步方式返回结果。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
deviceId number 输入设备id。
callback AsyncCallback<InputDeviceData> 回调函数,异步返回输入设备信息。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

// 获取输入设备id为1的设备信息。
try {
  inputDevice.getDeviceInfo(1, (error: Error, deviceData: inputDevice.InputDeviceData) => {
    if (error) {
      console.log(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Device info: ${JSON.stringify(deviceData)}`);
  });
} catch (error) {
  console.log(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDevice.getDeviceInfo9+

getDeviceInfo(deviceId: number): Promise<InputDeviceData>

获取指定输入设备的信息,使用Promise异步方式返回结果。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
deviceId number 输入设备id。

返回值

参数 说明
Promise<InputDeviceData> Promise对象,异步返回输入设备信息。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

// 获取输入设备id为1的设备信息。
try {
  inputDevice.getDeviceInfo(1).then((deviceData: inputDevice.InputDeviceData) => {
    console.log(`Device info: ${JSON.stringify(deviceData)}`);
  });
} catch (error) {
  console.log(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDevice.getDeviceInfoSync10+

getDeviceInfoSync(deviceId: number): InputDeviceData

获取指定输入设备的信息。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
deviceId number 输入设备id。

返回值

参数 说明
InputDeviceData 返回输入设备信息。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

// 获取输入设备id为1的设备信息。
try {
  let deviceData: inputDevice.InputDeviceData = inputDevice.getDeviceInfoSync(1)
  console.log(`Device info: ${JSON.stringify(deviceData)}`)
} catch (error) {
  console.log(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`)
}

inputDevice.on9+

on(type: "change", listener: Callback<DeviceListener>): void

监听输入设备的热插拔事件,使用时需连接鼠标键盘等外部设备。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
type string 输入设备的事件【鼠标、键盘、触摸屏等】类型。
listener Callback<DeviceListener> 回调函数,异步上报输入设备热插拔事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

let isPhysicalKeyboardExist = true;
try {
  inputDevice.on("change", (data: inputDevice.DeviceListener) => {
    console.log(`Device event info: ${JSON.stringify(data)}`);
    inputDevice.getKeyboardType(data.deviceId, (err: Error, type: inputDevice.KeyboardType) => {
      console.log("The keyboard type is: " + type);
      if (type == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'add') {
        // 监听物理键盘已连接。
        isPhysicalKeyboardExist = true;
      } else if (type == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'remove') {
        // 监听物理键盘已断开。
        isPhysicalKeyboardExist = false;
      }
    });
  });
  // 根据isPhysicalKeyboardExist的值决定软键盘是否弹出。
} catch (error) {
  console.log(`Get device info failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDevice.off9+

off(type: "change", listener?: Callback<DeviceListener>): void

取消监听输入设备的热插拔事件。在应用退出前调用,取消监听。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
type string 输入设备的事件【鼠标、键盘、触摸屏等】类型。
listener Callback<DeviceListener> 取消监听的回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

function callback(data: inputDevice.DeviceListener) {
  console.log(`Report device event info: ${JSON.stringify(data, [`type`, `deviceId`])}`);
};

try {
  inputDevice.on("change", callback);
} catch (error) {
  console.log(`Listen device event failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

// 取消指定的监听。
try {
  inputDevice.off("change", callback);
} catch (error) {
  console.log(`Cancel listening device event failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

// 取消所有监听。
try {
  inputDevice.off("change");
} catch (error) {
  console.log(`Cancel all listening device event failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDevice.getDeviceIds(deprecated)

getDeviceIds(callback: AsyncCallback<Array<number>>): void

获取所有输入设备的id列表,使用AsyncCallback异步方式返回结果。

从API version 9 开始不再维护,建议使用inputDevice.getDeviceList代替。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
callback AsyncCallback<Array<number>> 回调函数,异步返回所有输入设备的id列表。

示例

inputDevice.getDeviceIds((error: Error, ids: Array<Number>) => {
  if (error) {
    console.log(`Failed to get device id list, error: ${JSON.stringify(error, [`code`, `message`])}`);
    return;
  }
  console.log(`Device id list: ${JSON.stringify(ids)}`);
});

inputDevice.getDeviceIds(deprecated)

getDeviceIds(): Promise<Array<number>>

获取所有输入设备的id列表,使用Promise异步方式返回结果。

从API version 9 开始不再维护,建议使用inputDevice.getDeviceList代替。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

返回值

参数 说明
Promise<Array<number>> Promise对象,异步返回所有输入设备的id列表。

示例

inputDevice.getDeviceIds().then((ids: Array<Number>) => {
  console.log(`Device id list: ${JSON.stringify(ids)}`);
});

inputDevice.getDevice(deprecated)

getDevice(deviceId: number, callback: AsyncCallback<InputDeviceData>): void

获取指定输入设备的信息,使用AsyncCallback异步方式返回结果。

从API version 9 开始不再维护,建议使用inputDevice.getDeviceInfo代替。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
deviceId number 输入设备id。
callback AsyncCallback<InputDeviceData> 回调函数,异步返回输入设备信息。

示例

// 获取输入设备id为1的设备信息。
inputDevice.getDevice(1, (error: Error, deviceData: inputDevice.InputDeviceData) => {
  if (error) {
    console.log(`Failed to get device info, error: ${JSON.stringify(error, [`code`, `message`])}`);
    return;
  }
  console.log(`Device info: ${JSON.stringify(deviceData)}`);
});

inputDevice.getDevice(deprecated)

getDevice(deviceId: number): Promise<InputDeviceData>

获取指定输入设备的信息,使用Promise异步方式返回结果。

从API version 9 开始不再维护,建议使用inputDevice.getDeviceInfo代替。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
deviceId number 输入设备id。

返回值

参数 说明
Promise<InputDeviceData> Promise对象,异步返回输入设备信息。

示例

// 获取输入设备id为1的设备信息。
inputDevice.getDevice(1).then((deviceData: inputDevice.InputDeviceData) => {
  console.log(`Device info: ${JSON.stringify(deviceData)}`);
});

inputDevice.supportKeys9+

supportKeys(deviceId: number, keys: Array<KeyCode>, callback: AsyncCallback <Array<boolean>>): void

获取输入设备是否支持指定的键码值,使用AsyncCallback异步方式返回结果。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
deviceId number 输入设备id,同一个物理设备反复插拔,设备id会发生变化。
keys Array<KeyCode> 需要查询的键码值,最多支持5个按键查询。
callback AsyncCallback<Array<boolean>> 回调函数,异步返回查询结果。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

// 查询id为1的输入设备对于17、22和2055按键的支持情况。
try {
  inputDevice.supportKeys(1, [17, 22, 2055], (error: Error, supportResult: Array<Boolean>) => {
    console.log(`Query result: ${JSON.stringify(supportResult)}`);
  });
} catch (error) {
  console.log(`Query failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDevice.supportKeys9+

supportKeys(deviceId: number, keys: Array<KeyCode>): Promise<Array<boolean>>

获取输入设备是否支持指定的键码值,使用Promise异步方式返回结果。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
deviceId number 输入设备id,同一个物理设备反复插拔,设备id会发生变化。
keys Array<KeyCode> 需要查询的键码值,最多支持5个按键查询。

返回值

参数 说明
Promise<Array<boolean>> Promise对象,异步返回查询结果。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

// 查询id为1的输入设备对于17、22和2055按键的支持情况。
try {
  inputDevice.supportKeys(1, [17, 22, 2055]).then((supportResult: Array<Boolean>) => {
    console.log(`Query result: ${JSON.stringify(supportResult)}`);
  });
} catch (error) {
  console.log(`Query failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDevice.supportKeysSync10+

supportKeysSync(deviceId: number, keys: Array<KeyCode>): Array<boolean>

获取输入设备是否支持指定的键码值。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
deviceId number 输入设备id,同一个物理设备反复插拔,设备id会发生变化。
keys Array<KeyCode> 需要查询的键码值,最多支持5个按键查询。

返回值

参数 说明
Array<boolean> 返回查询结果。true表示支持,false表示不支持。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

// 查询id为1的输入设备对于17、22和2055按键的支持情况。
try {
  let supportResult: Array<Boolean> = inputDevice.supportKeysSync(1, [17, 22, 2055])
  console.log(`Query result: ${JSON.stringify(supportResult)}`)
} catch (error) {
  console.log(`Query failed, error: ${JSON.stringify(error, [`code`, `message`])}`)
}

inputDevice.getKeyboardType9+

getKeyboardType(deviceId: number, callback: AsyncCallback<KeyboardType>): void

获取输入设备的键盘类型,使用AsyncCallback异步方式返回结果。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
deviceId number 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。
callback AsyncCallback<KeyboardType> 回调函数,异步返回查询结果。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

// 查询id为1的输入设备的键盘类型。
try {
  inputDevice.getKeyboardType(1, (error: Error, type: Number) => {
    if (error) {
      console.log(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Keyboard type: ${JSON.stringify(type)}`);
  });
} catch (error) {
  console.log(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDevice.getKeyboardType9+

getKeyboardType(deviceId: number): Promise<KeyboardType>

获取输入设备的键盘类型,使用Promise异步方式返回结果。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
deviceId number 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。

返回值

参数 说明
Promise<KeyboardType> Promise对象,异步返回查询结果。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

// 示例查询设备id为1的设备键盘类型。
try {
  inputDevice.getKeyboardType(1).then((type: Number) => {
    console.log(`Keyboard type: ${JSON.stringify(type)}`);
  });
} catch (error) {
  console.log(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDevice.getKeyboardTypeSync10+

getKeyboardTypeSync(deviceId: number): KeyboardType

获取输入设备的键盘类型。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
deviceId number 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。

返回值

参数 说明
KeyboardType 返回查询结果。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例

// 示例查询设备id为1的设备键盘类型。
try {
  let type: number = inputDevice.getKeyboardTypeSync(1)
  console.log(`Keyboard type: ${JSON.stringify(type)}`)
} catch (error) {
  console.log(`Failed to get keyboard type, error: ${JSON.stringify(error, [`code`, `message`])}`)
}

inputDevice.isFunctionKeyEnabled15+

isFunctionKeyEnabled(functionKey: FunctionKey): Promise<boolean>

检查功能键是否使能。使用Promise异步回调。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
functionKey FunctionKey 需要设置的功能键类型。

返回值

参数 说明
Promise<boolean> Promise对象。返回查询结果,true表示功能键使能,false表示功能键未使能。

错误码

以下错误码的详细介绍请参见通用错误码输入设备错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.
3900002 There is currently no keyboard device connected.

示例

import { inputDevice } from '@kit.InputKit';

try {
  inputDevice.isFunctionKeyEnabled(inputDevice.FunctionKey.CAPS_LOCK).then((state: boolean) => {
    console.log(`capslock state: ${JSON.stringify(state)}`);
  });
} catch (error) {
  console.log(`Failed to get capslock state, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDevice.setFunctionKeyEnabled15+

setFunctionKeyEnabled(functionKey: FunctionKey, enabled: boolean): Promise<void>

设置功能键使能状态。使用Promise异步回调。

需要权限:ohos.permission.INPUT_KEYBOARD_CONTROLLER

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

参数

参数名 类型 必填 说明
functionKey FunctionKey 需要设置的功能键类型。
enabled boolean 功能键使能状态。取值为true表示使能功能键,取值为false表示不使能功能键。

错误码

以下错误码的详细介绍请参见通用错误码输入设备错误码

错误码ID 错误信息
201 Permission denied.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.
3900002 There is currently no keyboard device connected.
3900003 It is prohibited for non-input applications.

示例

import { inputDevice } from '@kit.InputKit';
import { BusinessError } from '@kit.BasicServicesKit';

try {
  inputDevice.setFunctionKeyEnabled(inputDevice.FunctionKey.CAPS_LOCK, true).then(() => {
    console.info(`Set capslock state success`);
  }).catch((error: BusinessError) => {
    console.info(`Set capslock state failed, error=${JSON.stringify(error)}`);
  });
} catch (error) {
    console.info(`Set capslock enable error`);
}

inputDevice.getIntervalSinceLastInput14+

getIntervalSinceLastInput(): Promise<number>

获取距离上次系统输入事件的时间间隔,使用Promise异步回调。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

返回值

参数 说明
Promise<number> Promise对象,异步返回获取的时间间隔,单位为微秒(μs)。

示例

  inputDevice.getIntervalSinceLastInput().then((timeInterval: number) => {
    console.log(`Interval since last input: ${JSON.stringify(timeInterval)}`);
  });

DeviceListener9+

输入设备热插拔的描述信息。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

名称 类型 可读 可写 说明
type ChangedType 输入设备插入或者移除。
deviceId number 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。

InputDeviceData

输入设备的描述信息。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

名称 类型 可读 可写 说明
id number 输入设备的唯一标识,同一个物理设备反复插拔,设备id会发生变化。
name string 输入设备的名字。
sources Array<SourceType> 输入设备支持的源类型。比如有的键盘上附带触摸板,则此设备有keyboard和touchpad两种输入源。
axisRanges Array<AxisRange> 输入设备的轴信息。
bus9+ number 输入设备的总线类型。
product9+ number 输入设备的产品信息。
vendor9+ number 输入设备的厂商信息。
version9+ number 输入设备的版本信息。
phys9+ string 输入设备的物理地址。
uniq9+ string 输入设备的唯一标识。

AxisType9+

type AxisType = 'touchmajor' | 'touchminor' | 'orientation' | 'x' | 'y' | 'pressure' | 'toolminor' | 'toolmajor' | 'null'

输入设备的轴类型。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

类型 说明
'touchmajor' 椭圆触摸区域长轴。
'touchminor' 椭圆触摸区域短轴。
'toolminor' 工具区域短轴。
'toolmajor' 工具区域长轴。
'orientation' 方向轴。
'pressure' 压力轴。
'x' 横坐标轴。
'y' 纵坐标轴。
'null' 无。

AxisRange

输入设备的轴信息。

系统能力: SystemCapability.MultimodalInput.Input.InputDevice

名称 类型 可读 可写 说明
source SourceType 轴的输入源类型。
axis AxisType 轴的类型。
max number 轴的最大值。
min number 轴的最小值。
fuzz9+ number 轴的模糊值。
flat9+ number 轴的基准值。
resolution9+ number 轴的分辨率。

SourceType9+

type SourceType = 'keyboard' | 'mouse' | 'touchpad' | 'touchscreen' | 'joystick' | 'trackball'

轴的输入源类型。比如鼠标设备可上报x轴事件,则x轴的输入源就是鼠标。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

类型 说明
'keyboard' 表示输入设备是键盘。
'touchscreen' 表示输入设备是触摸屏。
'mouse' 表示输入设备是鼠标。
'trackball' 表示输入设备是轨迹球。
'touchpad' 表示输入设备是触摸板。
'joystick' 表示输入设备是操纵杆。

ChangedType9+

type ChangedType = 'add' | 'remove'

定义监听设备热插拔事件。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

类型 说明
'add' 表示输入设备插入。
'remove' 表示输入设备移除。

KeyboardType9+

定义键盘输入设备的类型。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

名称 说明
NONE 0 表示无按键设备。
UNKNOWN 1 表示未知按键设备。
ALPHABETIC_KEYBOARD 2 表示全键盘设备。
DIGITAL_KEYBOARD 3 表示小键盘设备。
HANDWRITING_PEN 4 表示手写笔设备。
REMOTE_CONTROL 5 表示遥控器设备。

FunctionKey15+

定义功能键的类型。

系统能力:SystemCapability.MultimodalInput.Input.InputDevice

名称 说明
CAPS_LOCK 1 CapsLock键,仅支持对输入键盘扩展的CapsLock键设置使能。