鸿蒙开发设备管理:【@ohos.multimodalInput.inputDevice (输入设备)】

输入设备

输入设备管理模块,用于监听输入设备连接、断开和变化,并查看输入设备相关信息。比如监听鼠标插拔,并获取鼠标的id、name和指针移动速度等信息。

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

导入模块

import inputDevice from '@ohos.multimodalInput.inputDevice';

inputDevice.on9+

on(type: “change”, listener: Callback): void

监听输入设备的热插拔事件。

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

参数

参数类型必填说明
typestring输入设备的事件类型。
listenerCallback<[DeviceListener]>可上报的输入设备事件。

示例

let isPhysicalKeyboardExist = true;
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') {
            // 监听物理键盘已连接。
            isPhysicalKeyboardExist = true;
        } else if (ret == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD && data.type == 'remove') {
            // 监听物理键盘已断开。
            isPhysicalKeyboardExist = false;
        }
    });
});
// 根据isPhysicalKeyboardExist的值决定软键盘是否弹出。

inputDevice.off9+

off(type: “change”, listener?: Callback): void

取消监听输入设备的热插拔事件。

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

参数

参数类型必填说明
typestring输入设备的事件类型。
listenerCallback<[DeviceListener]>可上报的输入设备事件。

示例

function listener(data) {
    console.log("type: " + data.type + ", deviceId: " + data.deviceId);
}

// 单独取消listener的监听。
inputDevice.off("change", listener);

// 取消所有监听。
inputDevice.off("change");
// 取消监听后,软键盘默认都弹出。

inputDevice.getDeviceIds

getDeviceIds(callback: AsyncCallback<Array>): void

获取所有输入设备的id列表,使用callback方式作为异步方法。

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

参数

参数类型必填说明
callbackAsyncCallback<Array>回调函数。

示例

inputDevice.getDeviceIds((ids)=>{
    console.log("The device ID list is: " + ids);
});

inputDevice.getDeviceIds

getDeviceIds(): Promise<Array>

获取所有输入设备的id列表,使用Promise方式作为异步方法。

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

返回值

参数说明
Promise<Array>Promise实例,用于异步获取结果。

示例

inputDevice.getDeviceIds().then((ids)=>{
    console.log("The device ID list is: " + ids);
});

inputDevice.getDevice

getDevice(deviceId: number, callback: AsyncCallback): void

获取输入设备的描述信息,使用callback方式作为异步方法。

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

参数

参数类型必填说明
deviceIdnumber需要获取信息的设备id。
callbackAsyncCallback<[InputDeviceData]>回调函数,异步返回InputDeviceData对象。

示例

// 示例获取设备id为1的设备name信息。
inputDevice.getDevice(1, (inputDevice)=>{
    console.log("The device name is: " + inputDevice.name);
});

inputDevice.getDevice

getDevice(deviceId: number): Promise

获取输入设备的描述信息,使用Promise方式作为异步方法。

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

参数

参数类型必填说明
deviceIdnumber需要获取信息的设备id。

返回值

参数说明
Promise<[InputDeviceData]>Promise实例,用于异步获取结果。

示例

// 示例获取设备id为1的设备name信息。
inputDevice.getDevice(1).then((inputDevice)=>{
    console.log("The device name is: " + inputDevice.name);
});

inputDevice.supportKeys9+

supportKeys(deviceId: number, keys: Array, callback: Callback<Array>): void

获取输入设备支持的键码值,使用callback方式作为异步方法。

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

参数

参数类型必填说明
deviceIdnumber输入设备的唯一标识,同一个物理设备反复插拔,其设备id会发生变化。
keysArray需要查询的键码值,最多支持5个按键查询。
callbackCallback<Array>回调函数,异步返回查询结果。

示例

// 示例查询id为1的设备对于17、22和2055按键的支持情况。
inputDevice.supportKeys(1, [17, 22, 2055], (ret)=>{
    console.log("The query result is as follows: " + ret);
});

inputDevice.supportKeys9+

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

获取输入设备支持的键码值,使用Promise方式作为异步方法。

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

参数

参数类型必填说明
deviceIdnumber输入设备的唯一标识,同一个物理设备反复插拔,其设备id会发生变化。
keysArray需要查询的键码值,最多支持5个按键查询。

返回值

参数说明
Promise<Array>Promise实例,用于异步获取结果。

示例

// 示例查询id为1的设备对于17、22和2055按键的支持情况。
inputDevice.supportKeys(1, [17, 22, 2055]).then((ret)=>{
    console.log("The query result is as follows: " + ret);
})

inputDevice.getKeyboardType9+

getKeyboardType(deviceId: number, callback: AsyncCallback): void

查询输入设备的键盘类型,使用callback方式作为异步方法。

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

参数

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

示例

// 示例查询设备id为1的设备键盘类型。
inputDevice.getKeyboardType(1, (ret)=>{
    console.log("The keyboard type of the device is: " + ret);
});

inputDevice.getKeyboardType9+

getKeyboardType(deviceId: number): Promise

查询输入设备的键盘类型,使用Promise方式作为异步方法。

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

返回值

参数说明
Promise<[KeyboardType]>Promise实例,用于异步获取结果。

示例

// 示例查询设备id为1的设备键盘类型。
inputDevice.getKeyboardType(1).then((ret)=>{
    console.log("The keyboard type of the device is: " + ret);
})

DeviceListener9+

输入设备的描述信息。

系统能力:以下各项对应的系统能力均为SystemCapability.MultimodalInput.Input.InputDevice

名称参数类型说明
type[ChangedType]表示输入设备插入或者移除。
deviceIdnumber输入设备的唯一标识,同一个物理设备反复插拔,其设备id会发生变化。

InputDeviceData

输入设备的描述信息。

系统能力:以下各项对应的系统能力均为SystemCapability.MultimodalInput.Input.InputDevice

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

AxisType9+

输入设备的轴类型。

系统能力:以下各项对应的系统能力均为SystemCapability.MultimodalInput.Input.InputDevice

名称参数类型说明
touchMajorstring表示touchMajor轴。
touchMinorstring表示touchMinor轴。
toolMinorstring表示toolMinor轴。
toolMajorstring表示toolMajor轴。
orientationstring表示orientation轴。
pressurestring表示pressure轴。
xstring表示x轴。
ystring表示y轴。
NULLstring无。

AxisRange

输入设备的轴信息。

系统能力: 以下各项对应的系统能力均为SystemCapability.MultimodalInput.Input.InputDevice

名称参数类型说明
source[SourceType]轴的输入源类型。
axis[AxisType]轴的类型。
maxnumber轴的最大值。
minnumber轴的最小值。
fuzz9+number轴的模糊值。
flat9+number轴的基准值。
resolution9+number轴的分辨率。

SourceType

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

系统能力:以下各项对应的系统能力均为SystemCapability.MultimodalInput.Input.InputDevice

名称参数类型说明
keyboardstring表示输入设备是键盘。
touchscreenstring表示输入设备是触摸屏。
mousestring表示输入设备是鼠标。
trackballstring表示输入设备是轨迹球。
touchpadstring表示输入设备是触摸板。
joystickstring表示输入设备是操纵杆。

ChangedType

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

系统能力:以下各项对应的系统能力均为SystemCapability.MultimodalInput.Input.InputDevice

名称参数类型说明
addstring表示输入设备插入。
removestring表示输入设备移除。

KeyboardType9+

定义键盘输入设备的类型。
在这里插入图片描述

系统能力:以下各项对应的系统能力均为SystemCapability.MultimodalInput.Input.InputDevice

名称参数类型说明
NONEnumber0表示无按键设备。
UNKNOWNnumber1表示未知按键设备。
ALPHABETIC_KEYBOARDnumber2表示全键盘设备。
DIGITAL_KEYBOARDnumber3表示小键盘设备。
HANDWRITING_PENnumber4表示手写笔设备。
REMOTE_CONTROLnumber5表示遥控器设备。
  • 13
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值