harmony 鸿蒙@ohos.multimodalInput.inputDeviceCooperate (键鼠穿越)

  • 2022-10-28
  • 浏览 (789)

@ohos.multimodalInput.inputDeviceCooperate (键鼠穿越)

键鼠穿越功能模块,提供两台或多台设备组网协同后键鼠共享能力,实现键鼠输入设备的跨设备协同操作。

说明

  • 从API Version 10开始,该接口不再维护,推荐使用新接口@ohos.cooperate (键鼠穿越)

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

  • 本模块接口均为系统接口。

导入模块

import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'

inputDeviceCooperate.enable

enable(enable: boolean, callback: AsyncCallback<void>): void

开启、关闭键鼠穿越,使用AsyncCallback异步方式返回结果。

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

参数

参数名 类型 必填 说明
enable boolean 键鼠穿越使能状态。
callback AsyncCallback<void> 回调函数,异步返回键鼠穿越开启、关闭结果。

示例

import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
import { BusinessError } from '@ohos.base'

try {
  inputDeviceCooperate.enable(true, (error: BusinessError) => {
    if (error) {
      console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Keyboard mouse crossing enable success.`);
  });
} catch (error) {
  console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDeviceCooperate.enable

enable(enable: boolean): Promise<void>

开启、关闭键鼠穿越,使用Promise异步方式返回结果。

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

参数

参数名 类型 必填 说明
enable boolean 键鼠穿越使能状态。

返回值

参数 说明
Promise<void> Promise对象,异步返回键鼠穿越开启、关闭结果。

示例

import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
import { BusinessError } from '@ohos.base'

try {
  inputDeviceCooperate.enable(true).then(() => {
    console.log(`Keyboard mouse crossing enable success.`);
  }, (error: BusinessError) => {
    console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
  });
} catch (error) {
  console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDeviceCooperate.start

start(sinkDeviceDescriptor: string, srcInputDeviceId: number, callback: AsyncCallback<void>): void

启动键鼠穿越,使用AsyncCallback异步方式返回结果。

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

参数

参数名 类型 必填 说明
sinkDeviceDescriptor string 键鼠穿越目标设备描述符。
srcInputDeviceId number 键鼠穿越待穿越外设标识符。
callback AsyncCallback<void> 回调函数,异步返回键鼠穿越启动、停止状态。

错误码:

以下错误码的详细介绍请参见ohos.multimodalinput错误码

错误码ID 错误信息
4400001 Incorrect descriptor for the target device.
4400002 Screen hop failed.

示例

import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
import { BusinessError } from '@ohos.base'

let sinkDeviceDescriptor = "descriptor";
let srcInputDeviceId = 0;
try {
  inputDeviceCooperate.start(sinkDeviceDescriptor, srcInputDeviceId, (error: BusinessError) => {
    if (error) {
      console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Start Keyboard mouse crossing success.`);
  });
} catch (error) {
  console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDeviceCooperate.start

start(sinkDeviceDescriptor: string, srcInputDeviceId: number): Promise<void>

启动键鼠穿越,使用Promise异步方式返回结果。

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

参数

参数名 类型 必填 说明
sinkDeviceDescriptor string 键鼠穿越目标设备描述符。
srcInputDeviceId number 键鼠穿越待穿越外设标识符。

返回值

参数名 说明
Promise<void> Promise对象,异步返回键鼠穿越启动、关闭结果。

错误码:

以下错误码的详细介绍请参见ohos.multimodalinput错误码

错误码ID 错误信息
4400001 Incorrect descriptor for the target device.
4400002 Screen hop failed.

示例

import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
import { BusinessError } from '@ohos.base'

let sinkDeviceDescriptor = "descriptor";
let srcInputDeviceId = 0;
try {
  inputDeviceCooperate.start(sinkDeviceDescriptor, srcInputDeviceId).then(() => {
    console.log(`Start Keyboard mouse crossing success.`);
  }, (error: BusinessError) => {
    console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
  });
} catch (error) {
  console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDeviceCooperate.stop

stop(callback: AsyncCallback<void>): void

停止键鼠穿越,使用AsyncCallback异步方式返回结果。

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

参数

参数名 类型 必填 说明
callback AsyncCallback<void> 回调函数,异步返回停止键鼠穿越结果。

示例

import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
import { BusinessError } from '@ohos.base'

try {
  inputDeviceCooperate.stop((error: BusinessError) => {
    if (error) {
      console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Stop Keyboard mouse crossing success.`);
  });
} catch (error) {
  console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDeviceCooperate.stop

stop(): Promise<void>

停止键鼠穿越,使用Promise异步方式返回结果。

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

返回值

参数名 说明
Promise<void> Promise对象,异步返回停止键鼠穿越结果。

示例

import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
import { BusinessError } from '@ohos.base'

try {
  inputDeviceCooperate.stop().then(() => {
    console.log(`Stop Keyboard mouse crossing success.`);
  }, (error: BusinessError) => {
    console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
  });
} catch (error) {
  console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDeviceCooperate.getState

getState(deviceDescriptor: string, callback: AsyncCallback<{ state: boolean }>): void

获取键鼠穿越开关的状态,使用AsyncCallback异步方式返回结果。

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

参数

参数名 类型 必填 说明
deviceDescriptor string 键鼠穿越目标设备描述符。
callback AsyncCallback<{ state: boolean }> 回调函数,异步返回键鼠穿越开关状态。

示例

import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
import { BusinessError } from '@ohos.base'

let deviceDescriptor = "descriptor";
try {
  inputDeviceCooperate.getState(deviceDescriptor, (error: BusinessError, data: boolean) => {
    if (error) {
      console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Get the status success, data: ${JSON.stringify(data)}`);
  });
} catch (error) {
  console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputDeviceCooperate.getState

getState(deviceDescriptor: string): Promise<{ state: boolean }>

获取键鼠穿越开关的状态,使用Promise异步方式返回结果。

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

参数

参数名 类型 必填 说明
deviceDescriptor string 键鼠穿越目标设备描述符。

返回值

参数 说明
Promise<{ state: boolean }> Promise对象,异步返回键鼠穿越开关状态。

示例

import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
import { BusinessError } from '@ohos.base'

let deviceDescriptor = "descriptor";
try {
  inputDeviceCooperate.getState(deviceDescriptor).then((data: boolean) => {
    console.log(`Get the status success, data: ${JSON.stringify(data)}`);
  }, (error: BusinessError) => {
    console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
  });
} catch (error) {
  console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

on(‘cooperation’)

on(type: ‘cooperation’, callback: AsyncCallback<{ deviceDescriptor: string, eventMsg: EventMsg }>): void

注册监听键鼠穿越状态。

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

参数

参数名 类型 必填 说明
type string 注册类型,取值”cooperation“。
callback AsyncCallback<{ deviceDescriptor: string, eventMsg: EventMsg }> 回调函数,异步返回键鼠穿越事件。

示例

import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'

function callback(deviceDescriptor: string, eventMsg: inputDeviceCooperate.EventMsg) {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(deviceDescriptor)}`);
  return false;
}
try {
  inputDeviceCooperate.on('cooperation', callback);
} catch (error) {
  console.log(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

off(‘cooperation’)

off(type: ‘cooperation’, callback?: AsyncCallback<void>): void

关闭监听键鼠穿越状态。

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

参数

参数名 类型 必填 说明
type string 注册类型,取值“cooperation”。
callback AsyncCallback<void> 需要取消注册的回调函数,若无此参数,则取消当前应用注册的所有回调函数。

示例

import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'

// 取消注册单个回调函数
function callbackOn(deviceDescriptor: string, eventMsg: inputDeviceCooperate.EventMsg) {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(deviceDescriptor)}`);
  return false;
}
function callbackOff() {
  console.log(`Keyboard mouse crossing event`);
  return false;
}
try {
  inputDeviceCooperate.on('cooperation', callbackOn);
  inputDeviceCooperate.off("cooperation", callbackOff);
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'

// 取消注册所有回调函数
function callback(deviceDescriptor: string, eventMsg: inputDeviceCooperate.EventMsg) {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(deviceDescriptor)}`);
  return false;
}
try {
  inputDeviceCooperate.on('cooperation', callback);
  inputDeviceCooperate.off("cooperation");
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

EventMsg

键鼠穿越事件。

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

名称 说明
MSG_COOPERATE_INFO_START 200 键鼠穿越消息,表示键鼠穿越开始。
MSG_COOPERATE_INFO_SUCCESS 201 键鼠穿越消息,表示键鼠穿越成功。
MSG_COOPERATE_INFO_FAIL 202 键鼠穿越消息,表示键鼠穿越失败。
MSG_COOPERATE_STATE_ON 500 键鼠穿越状态,表示键鼠穿越状态开启。
MSG_COOPERATE_STATE_OFF 501 键鼠穿越状态,表示键鼠穿越状态关闭。

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

0  赞