@ohos.cooperate (键鼠穿越)(系统接口)

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

说明

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

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

导入模块

import cooperate from '@ohos.cooperate';

cooperate.prepareCooperate11+

prepareCooperate(callback: AsyncCallback<void>): void;

准备键鼠穿越,使用Callback异步回调。

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数

参数名 类型 必填 说明
callback AsyncCallback<void> 回调函数,准备键鼠穿越成功时,err为undefined,否则为错误对象。

示例:

import { BusinessError } from '@ohos.base';
try {
  cooperate.prepareCooperate((error: BusinessError) => {
    if (error) {
      console.log(`Keyboard mouse crossing prepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Keyboard mouse crossing prepareCooperate success.`);
  });
} catch (error) {
  console.log(`Keyboard mouse crossing prepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

cooperate.prepareCooperate11+

prepareCooperate(): Promise<void>;

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

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

返回值:

参数 说明
Promise<void> 无返回结果的Promise对象。

示例

import { BusinessError } from '@ohos.base';
try {
  cooperate.prepareCooperate().then(() => {
    console.log(`Keyboard mouse crossing prepareCooperate success.`);
  }, (error: BusinessError) => {
    console.log(`Keyboard mouse crossing prepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
  });
} catch (error) {
  console.log(`Keyboard mouse crossing prepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

cooperate.unprepareCooperate11+

unprepareCooperate(callback: AsyncCallback<void>): void;

取消键鼠穿越准备,使用Callback异步回调。

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力: SystemCapability.Msdp.DeviceStatus.Cooperate

参数名 类型 必填 说明
callback AsyncCallback<void> 回调函数,取消键鼠穿越准备成功时,err为undefined,否则为错误对象。

示例:

import { BusinessError } from '@ohos.base';
try {
  cooperate.unprepareCooperate((error: BusinessError) => {
    if (error) {
      console.log(`Keyboard mouse crossing unprepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Keyboard mouse crossing unprepareCooperate success.`);
  });
} catch (error) {
  console.log(`Keyboard mouse crossing unprepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

cooperate.unprepareCooperate11+

unprepareCooperate(): Promise<void>;

取消键鼠穿越准备,使用Promise异步回调。

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力: SystemCapability.Msdp.DeviceStatus.Cooperate

返回值:

参数 说明
Promise<void> 无返回结果的Promise对象。

示例:

import { BusinessError } from '@ohos.base';
try {
  cooperate.unprepareCooperate().then(() => {
    console.log(`Keyboard mouse crossing unprepareCooperate success.`);
  }, (error: BusinessError) => {
    console.log(`Keyboard mouse crossing unprepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
  });
} catch (error) {
  console.log(`Keyboard mouse crossing unprepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

cooperate.activateCooperate11+

activateCooperate(targetNetworkId: string, inputDeviceId: number, callback: AsyncCallback<void>): void;

启动键鼠穿越,使用Callback异步回调。

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数:

参数名 类型 必填 说明
targetNetworkId string 键鼠穿越目标设备描述符。
inputDeviceId number 待穿越输入设备标识符。
callback AsyncCallback<void> 回调函数,键鼠穿越启动成功时,err为undefined,否则为错误对象。

错误码:

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

错误码ID 错误信息
20900001 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
let targetNetworkId = "networkId";
let inputDeviceId = 0;
try {
  cooperate.activateCooperate(targetNetworkId, inputDeviceId, (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`])}`);
}

cooperate.activateCooperate11+

activateCooperate(targetNetworkId: string, inputDeviceId: number): Promise<void>;

启动键鼠穿越,使用Promise异步回调。

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力: SystemCapability.Msdp.DeviceStatus.Cooperate

参数:

参数名 类型 必填 说明
targetNetworkId string 键鼠穿越目标设备描述符。
inputDeviceId number 待穿越输入设备标识符。

返回值:

参数名 说明
Promise<void> 无返回结果的Promise对象。

错误码:

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

错误码ID 错误信息
20900001 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
let targetNetworkId = "networkId";
let inputDeviceId = 0;
try {
 cooperate.activateCooperate(targetNetworkId, inputDeviceId).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`])}`);
}

cooperate.deactivateCooperate11+

deactivateCooperate(isUnchained: boolean, callback: AsyncCallback<void>): void;

停止键鼠穿越,使用Callback异步回调。

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数:

参数名 类型 必填 说明
isUnchained boolean 是否关闭跨设备链路。 ture表示关闭跨设备链路,false表示不关闭。
callback AsyncCallback<void> 回调函数,键鼠穿越停止成功时,err为undefined,否则为错误对象。

示例

import { BusinessError } from '@ohos.base';
try {
  cooperate.deactivateCooperate(false, (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`])}`);
}

cooperate.deactivateCooperate11+

deactivateCooperate(isUnchained: boolean): Promise<void>;

停止键鼠穿越,使用Promise异步回调。

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数:

参数名 类型 必填 说明
isUnchained boolean 是否关闭跨设备链路。 ture表示关闭跨设备链路,false表示不关闭。

返回值:

参数名 说明
Promise<void> 无返回结果的Promise对象。

示例

import { BusinessError } from '@ohos.base';
try {
  cooperate.deactivateCooperate(false).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`])}`);
}

cooperate.getCooperateSwitchState11+

getCooperateSwitchState(networkId: string, callback: AsyncCallback<boolean>): void;

获取目标设备键鼠穿越开关的状态,使用Callback异步回调。

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数:

参数名 类型 必填 说明
networkId string 键鼠穿越目标设备描述符。
callback AsyncCallback<boolean> 回调函数,返回ture表示目标设备键鼠穿越的开关开启,返回false表示开关未开启。

示例:

import { BusinessError } from '@ohos.base';
let deviceDescriptor = "networkId";
try {
  cooperate.getCooperateSwitchState(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`])}`);
}

cooperate.getCooperateSwitchState11+

getCooperateSwitchState(networkId: string): Promise<boolean>;

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

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数

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

返回值

参数 说明
Promise<boolean> Promise对象,返回ture表示目标设备键鼠穿越的开关开启,返回false表示开关未开启。

示例

import { BusinessError } from '@ohos.base';
let deviceDescriptor = "networkId";
try {
  cooperate.getCooperateSwitchState(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('cooperateMessage')11+

on(type: 'cooperateMessage', callback: Callback<CooperateMessage>): void;

注册监听键鼠穿越状态。

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数

参数名 类型 必填 说明
type string 监听类型,取值为'cooperateMessage'
callback Callback<CooperateMessage> 回调函数,异步返回键鼠穿越状态消息。

示例

function callback(msg: cooperate.CooperateMessage) {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`);
  return false;
}
try {
  cooperate.on('cooperateMessage', callback);
} catch (error) {
  console.log(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

off('cooperateMessage')11+

off(type: 'cooperateMessage', callback?: Callback<CooperateMessage>): void;

取消监听键鼠穿越状态。

需要权限:ohos.permission.COOPERATE_MANAGER

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数:

参数名 类型 必填 说明
type string 监听类型,取值为'cooperate'。
callback Callback<CooperateMessage> 需要取消注册的回调函数,若无此参数,则取消当前应用注册的所有回调函数。

示例

// 取消注册单个回调函数
function callbackOn(msgOn: cooperate.CooperateMessage) {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(msgOn)}`);
  return false;
}
function callbackOff(msgOff: cooperate.CooperateMessage) {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(msgOff)}`);
  return false;
}
try {
  cooperate.on('cooperateMessage', callbackOn);
  cooperate.off('cooperateMessage', callbackOff);
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消注册所有回调函数
function callbackOn(msg: cooperate.CooperateMessage) {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`);
  return false;
}
try {
  cooperate.on('cooperateMessage', callbackOn);
  cooperate.off('cooperateMessage');
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

CooperateMessage11+

键鼠穿越的消息。

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

名称 类型 可读 可写 说明
networkId string 键鼠穿越目标设备描述符。
state CooperateState 键鼠穿越的状态。

CooperateState10+

键鼠穿越的状态。

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

名称 类型 可读 可写 说明
COOPERATE_PREPARE number 表示准备键鼠穿越。
COOPERATE_UNPREPARE number 表示取消键鼠穿越准备。
COOPERATE_ACTIVATE number 表示启动键鼠穿越。
COOPERATE_ACTIVATE_SUCCESS number 表示键鼠穿越启动成功。
COOPERATE_ACTIVATE_FAILURE number 表示键鼠穿越启动失败。
COOPERATE_DEACTIVATE_SUCCESS number 表示键鼠穿越停止成功。
COOPERATE_DEACTIVATE_FAILURE number 表示键鼠穿越停止失败。
COOPERATE_SESSION_DISCONNECTED number 表示键鼠穿越会话断开。

cooperate.prepare(deprecated)

prepare(callback: AsyncCallback<void>): void;

准备键鼠穿越,使用Callback异步回调。

说明:

从API version 10开始不在维护。建议使用cooperate.prepareCooperate替代

系统能力: SystemCapability.Msdp.DeviceStatus.Cooperate

参数

参数名 类型 必填 说明
callback AsyncCallback<void> 回调函数,准备键鼠穿越成功时,err为undefined,否则为错误对象。

示例

import { BusinessError } from '@ohos.base';
try {
  cooperate.prepare((error: BusinessError) => {
    if (error) {
      console.log(`Keyboard mouse crossing prepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Keyboard mouse crossing prepare success.`);
  });
} catch (error) {
  console.log(`Keyboard mouse crossing prepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

cooperate.prepare(deprecated)

prepare(): Promise<void>;

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

说明:

从API version 10开始不在维护。建议使用cooperate.prepareCooperate替代

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

返回值

参数 说明
Promise<void> 无返回结果的Promise对象。

示例

import { BusinessError } from '@ohos.base';
try {
  cooperate.prepare().then(() => {
    console.log(`Keyboard mouse crossing prepare success.`);
  }, (error: BusinessError) => {
    console.log(`Keyboard mouse crossing prepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
  });
} catch (error) {
  console.log(`Keyboard mouse crossing prepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

cooperate.unprepare(deprecated)

unprepare(callback: AsyncCallback<void>): void;

取消键鼠穿越准备,使用Callback异步回调。

说明:

从API version 10开始不在维护。建议使用cooperate.unprepareCooperate替代

系统能力: SystemCapability.Msdp.DeviceStatus.Cooperate

参数名 类型 必填 说明
callback AsyncCallback<void> 回调函数,取消键鼠穿越准备成功时,err为undefined,否则为错误对象。

示例

import { BusinessError } from '@ohos.base';
try {
  cooperate.unprepare((error: BusinessError) => {
    if (error) {
      console.log(`Keyboard mouse crossing unprepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
      return;
    }
    console.log(`Keyboard mouse crossing unprepare success.`);
  });
} catch (error) {
  console.log(`Keyboard mouse crossing unprepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

cooperate.unprepare(deprecated)

unprepare(): Promise<void>;

取消键鼠穿越准备,使用Promise异步回调。

说明:

从API version 10开始不在维护。建议使用cooperate.unprepareCooperate替代

系统能力: SystemCapability.Msdp.DeviceStatus.Cooperate

返回值

参数 说明
Promise<void> 无返回结果的Promise对象。

示例:

import { BusinessError } from '@ohos.base';
try {
  cooperate.unprepare().then(() => {
    console.log(`Keyboard mouse crossing unprepare success.`);
  }, (error: BusinessError) => {
    console.log(`Keyboard mouse crossing unprepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
  });
} catch (error) {
  console.log(`Keyboard mouse crossing unprepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

cooperate.activate(deprecated)

activate(targetNetworkId: string, inputDeviceId: number, callback: AsyncCallback<void>): void;

启动键鼠穿越,使用Callback异步回调。

说明:

从API version 10开始不在维护。建议使用cooperate.activateCooperate替代

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数

参数名 类型 必填 说明
targetNetworkId string 键鼠穿越目标设备描述符。
inputDeviceId number 待穿越输入设备标识符。
callback AsyncCallback<void> 回调函数,键鼠穿越启动成功时,err为undefined,否则为错误对象。

错误码:

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

错误码ID 错误信息
20900001 Operation failed.

示例

import { BusinessError } from '@ohos.base';
let targetNetworkId = "networkId";
let inputDeviceId = 0;
try {
  cooperate.activate(targetNetworkId, inputDeviceId, (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`])}`);
}

cooperate.activate(deprecated)

activate(targetNetworkId: string, inputDeviceId: number): Promise<void>;

启动键鼠穿越,使用Promise异步回调。

说明:

从API version 10开始不在维护。建议使用cooperate.activateCooperate替代

系统能力: SystemCapability.Msdp.DeviceStatus.Cooperate

参数

参数名 类型 必填 说明
targetNetworkId string 键鼠穿越目标设备描述符。
inputDeviceId number 待穿越输入设备标识符。

返回值

参数名 说明
Promise<void> 无返回结果的Promise对象。

错误码:

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

错误码ID 错误信息
20900001 Operation failed.

示例

import { BusinessError } from '@ohos.base';
let targetNetworkId = "networkId";
let inputDeviceId = 0;
try {
 cooperate.activate(targetNetworkId, inputDeviceId).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`])}`);
}

cooperate.deactivate(deprecated)

deactivate(isUnchained: boolean, callback: AsyncCallback<void>): void;

停止键鼠穿越,使用Callback异步回调。

说明:

从API version 10开始不在维护。建议使用cooperate.deactivateCooperate替代

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数

参数名 类型 必填 说明
isUnchained boolean 是否关闭跨设备链路。
ture表示关闭跨设备链路,false表示不关闭。
callback AsyncCallback<void> 回调函数,键鼠穿越停止成功时,err为undefined,否则为错误对象。

示例

import { BusinessError } from '@ohos.base';
try {
  cooperate.deactivate(false, (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`])}`);
}

cooperate.deactivate(deprecated)

deactivate(isUnchained: boolean): Promise<void>;

停止键鼠穿越,使用Promise异步回调。

说明:

从API version 10开始不在维护。建议使用cooperate.deactivateCooperate替代

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数

参数名 类型 必填 说明
isUnchained boolean 是否关闭跨设备链路。
ture表示关闭跨设备链路,false表示不关闭。

返回值

参数名 说明
Promise<void> 无返回结果的Promise对象。

示例

import { BusinessError } from '@ohos.base';
try {
  cooperate.deactivate(false).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`])}`);
}

cooperate.getCrossingSwitchState(deprecated)

getCrossingSwitchState(networkId: string, callback: AsyncCallback<boolean>): void;

获取目标设备键鼠穿越开关的状态,使用Callback异步回调。

说明:

从API version 10开始不在维护。建议使用cooperate.getCooperateSwitchState替代

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数

参数名 类型 必填 说明
networkId string 键鼠穿越目标设备描述符。
callback AsyncCallback<boolean> 回调函数,返回ture表示目标设备键鼠穿越的开关开启,返回false表示开关未开启。

示例

import { BusinessError } from '@ohos.base';
let deviceDescriptor = "networkId";
try {
  cooperate.getCrossingSwitchState(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`])}`);
}

cooperate.getCrossingSwitchState(deprecated)

getCrossingSwitchState(networkId: string): Promise<boolean>;

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

说明:

从API version 10开始不在维护。建议使用cooperate.getCooperateSwitchState替代

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数

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

返回值

参数 说明
Promise<boolean> Promise对象,返回ture表示目标设备键鼠穿越的开关开启,返回false表示开关未开启。

示例

import { BusinessError } from '@ohos.base';
let deviceDescriptor = "networkId";
try {
  cooperate.getCrossingSwitchState(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('cooperate')(deprecated)

on(type: 'cooperate', callback: Callback<{ networkId: string, msg: CooperateMsg }>): void;

注册监听键鼠穿越状态。

说明:

从API version 10开始不在维护。建议使用on('cooperateMessage')替代

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数

参数名 类型 必填 说明
type string 监听类型,取值为'cooperate'
callback Callback<{ networkId: string, msg: CooperateMsg }> 回调函数,异步返回键鼠穿越状态消息。

示例

function callback(networkId: string, msg: cooperate.CooperateMsg) {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(networkId)}`);
  return false;
}
try {
  cooperate.on('cooperate', callback);
} catch (error) {
  console.log(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

off('cooperate')(deprecated)

off(type: 'cooperate', callback?: Callback<void>): void;

取消监听键鼠穿越状态。

说明:

从API version 10开始不在维护。建议使用off('cooperateMessage')替代

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

参数

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

示例

// 取消注册单个回调函数
function callbackOn(networkId: string, msg: cooperate.CooperateMsg) {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(networkId)}`);
  return false;
}
function callbackOff() {
  console.log(`Keyboard mouse crossing event`);
  return false;
}
try {
  cooperate.on('cooperate', callbackOn);
  cooperate.off('cooperate', callbackOff);
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消注册所有回调函数
function callbackOn(networkId: string, msg: cooperate.CooperateMsg) {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(networkId)}`);
  return false;
}
try {
  cooperate.on('cooperate', callbackOn);
  cooperate.off('cooperate');
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

CooperateMsg(deprecated)

键鼠穿越的消息通知。

说明:

从API version 10开始不在维护。建议使用CooperateMessage替代

系统能力:SystemCapability.Msdp.DeviceStatus.Cooperate

名称 说明
COOPERATE_PREPARE 0 表示准备键鼠穿越。
COOPERATE_UNPREPARE 1 表示取消键鼠穿越准备。
COOPERATE_ACTIVATE 2 表示启动键鼠穿越。
COOPERATE_ACTIVATE_SUCCESS 3 表示键鼠穿越启动成功。
COOPERATE_ACTIVATE_FAIL 4 表示键鼠穿越启动失败。
COOPERATE_DEACTIVATE_SUCCESS 5 表示键鼠穿越停止成功。
COOPERATE_DEACTIVATE_FAIL 6 表示键鼠穿越停止失败。
COOPERATE_SESSION_DISCONNECTED 7 表示键鼠穿越会话断开。