openharmony 鸿蒙 js-apis-cooperate-sys

2025-06-12 浏览 (1)

@ohos.multimodalInput.inputDeviceCooperate (Screen Hopping) (System API)

The inputDeviceCooperate module implements screen hopping for two or more networked devices to share the keyboard and mouse for collaborative operations.

NOTE

  • The APIs provided by this module are no longer maintained since API Version 10. You are advised to use @ohos.cooperate (Screen Hopping).

  • The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The APIs provided by this module are system APIs.

Modules to Import

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

inputDeviceCooperate.enable

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

Specifies whether to enable screen hopping. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MultimodalInput.Input.Cooperator

Parameters

NameTypeMandatoryDescription
enablebooleanYesWhether to enable screen hopping.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the following error codes, see Screen Hopping Error Codes.

IDError Message
401Parameter error.

Example

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

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

inputDeviceCooperate.enable

enable(enable: boolean): Promise<void>

Specifies whether to enable screen hopping. This API uses a promise to return the result.

System capability: SystemCapability.MultimodalInput.Input.Cooperator

Parameters

NameTypeMandatoryDescription
enablebooleanYesWhether to enable screen hopping.

Return value

ParametersDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.

Example

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

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

inputDeviceCooperate.start

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

Starts screen hopping. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MultimodalInput.Input.Cooperator

Parameters

NameTypeMandatoryDescription
sinkDeviceDescriptorstringYesDescriptor of the target device for screen hopping.
srcInputDeviceIdnumberYesID of the target device for screen hopping.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Screen Hopping Error Codes.

IDError Message
401Parameter error.
4400001Incorrect descriptor for the target device.
4400002Screen hop failed.

Example

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

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

inputDeviceCooperate.start

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

Starts screen hopping. This API uses a promise to return the result.

System capability: SystemCapability.MultimodalInput.Input.Cooperator

Parameters

NameTypeMandatoryDescription
sinkDeviceDescriptorstringYesDescriptor of the target device for screen hopping.
srcInputDeviceIdnumberYesID of the target device for screen hopping.

Return value

NameDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see Screen Hopping Error Codes.

IDError Message
401Parameter error.
4400001Incorrect descriptor for the target device.
4400002Screen hop failed.

Example

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

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

inputDeviceCooperate.stop

stop(callback: AsyncCallback<void>): void

Stops screen hopping. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MultimodalInput.Input.Cooperator

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.

Example

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

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

inputDeviceCooperate.stop

stop(): Promise<void>

Stops screen hopping. This API uses a promise to return the result.

System capability: SystemCapability.MultimodalInput.Input.Cooperator

Return value

NameDescription
Promise<void>Promise used to return the result.

Example

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

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

inputDeviceCooperate.getState

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

Checks whether screen hopping is enabled. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MultimodalInput.Input.Cooperator

Parameters

NameTypeMandatoryDescription
deviceDescriptorstringYesDescriptor of the target device for screen hopping.
callbackAsyncCallback<{ state: boolean }>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.

Example

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

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

inputDeviceCooperate.getState

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

Checks whether screen hopping is enabled. This API uses a promise to return the result.

System capability: SystemCapability.MultimodalInput.Input.Cooperator

Parameters

NameTypeMandatoryDescription
deviceDescriptorstringYesDescriptor of the target device for screen hopping.

Return value

ParametersDescription
Promise<{ state: boolean }>Promise used to return the result. The value true indicates that screen hopping is enabled, and the false indicates the opposite.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.

Example

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

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

on('cooperation')

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

Enables listening for screen hopping status change events.

System capability: SystemCapability.MultimodalInput.Input.Cooperator

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is cooperation.
callbackAsyncCallback<{ deviceDescriptor: string, eventMsg: EventMsg }>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.

Example

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

let callback = (msg: object) => {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`);
  return false;
}
try {
  inputDeviceCooperate.on('cooperation', callback);
} catch (error) {
  console.error(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

off('cooperation')

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

Disables listening for screen hopping status change events.

System capability: SystemCapability.MultimodalInput.Input.Cooperator

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is cooperation.
callbackAsyncCallback<void>NoCallback to be unregistered. If this parameter is not specified, all callbacks registered by the current application will be unregistered.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.

Example

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

// Unregister a single callback.
let callbackOn = (msg: object) => {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`);
  return false;
}
let callbackOff = () => {
  console.log(`Keyboard mouse crossing event`);
  return false;
}
try {
  inputDeviceCooperate.on('cooperation', callbackOn);
  inputDeviceCooperate.off("cooperation", callbackOff);
} catch (error) {
  console.error(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
import { inputDeviceCooperate } from '@kit.InputKit';

// Unregister all callbacks.
let callback = (msg: object) => {
  console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`);
  return false;
}
try {
  inputDeviceCooperate.on('cooperation', callback);
  inputDeviceCooperate.off("cooperation");
} catch (error) {
  console.error(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

EventMsg

Enumerates screen hopping event.

System capability: SystemCapability.MultimodalInput.Input.Cooperator

NameValueDescription
MSG_COOPERATE_INFO_START200Screen hopping starts.
MSG_COOPERATE_INFO_SUCCESS201Screen hopping succeeds.
MSG_COOPERATE_INFO_FAIL202Screen hopping fails.
MSG_COOPERATE_STATE_ON500Screen hopping is enabled.
MSG_COOPERATE_STATE_OFF501Screen hopping is disabled.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Input Kit

harmony 鸿蒙Input_DeviceListener

harmony 鸿蒙Input_InterceptorEventCallback

harmony 鸿蒙Input_AxisEvent

harmony 鸿蒙Input_DeviceInfo

harmony 鸿蒙Input_DeviceListener

harmony 鸿蒙Input_Hotkey

harmony 鸿蒙Input_InterceptorEventCallback

harmony 鸿蒙Input_InterceptorOptions

harmony 鸿蒙Input_KeyEvent

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/Ro1O9B