# @ohos.multimodalInput.inputDeviceCooperate (é”®é¼ ç©¿è¶Š)(系统接å£) é”®é¼ ç©¿è¶ŠåŠŸèƒ½æ¨¡å—,æ供两å°æˆ–多å°è®¾å¤‡ç»„网ååŒåŽé”®é¼ å…±äº«èƒ½åŠ›ï¼Œå®žçŽ°é”®é¼ è¾“å…¥è®¾å¤‡çš„è·¨è®¾å¤‡ååŒæ“作。 > **说明** > >- 从API Version 10开始,该接å£ä¸å†ç»´æŠ¤ï¼ŒæŽ¨è使用新接å£[@ohos.cooperate](../apis-distributedservice-kit/js-apis-devicestatus-cooperate-sys.md) (é”®é¼ ç©¿è¶Š)。 > >- 本模å—首批接å£ä»ŽAPI version 9开始支æŒã€‚åŽç»ç‰ˆæœ¬çš„新增接å£ï¼Œé‡‡ç”¨ä¸Šè§’æ ‡å•ç‹¬æ ‡è®°æŽ¥å£çš„起始版本。 > >- 本模å—接å£å‡ä¸ºç³»ç»ŸæŽ¥å£ã€‚ ## å¯¼å…¥æ¨¡å— ```ts import { inputDeviceCooperate } from '@kit.InputKit'; ``` ## inputDeviceCooperate.enable enable(enable: boolean, callback: AsyncCallback<void>): void å¼€å¯ã€å…³é—é”®é¼ ç©¿è¶Šï¼Œä½¿ç”¨AsyncCallback异æ¥æ–¹å¼è¿”回结果。 **系统能力**: SystemCapability.MultimodalInput.Input.Cooperator **å‚æ•°**: | å‚æ•°å | 类型 | å¿…å¡« | 说明 | | -------- | ------------------------- | ---- | --------------------------- | | enable | boolean | 是 | é”®é¼ ç©¿è¶Šä½¿èƒ½çŠ¶æ€ã€‚ | | callback | AsyncCallback<void> | 是 |回调函数,异æ¥è¿”å›žé”®é¼ ç©¿è¶Šå¼€å¯ã€å…³é—结果。 | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[ohos.devicestatus错误ç ](../apis-distributedservice-kit/errorcode-devicestatus.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | -------- | -----------------| | 401 | Parameter error. | **示例**: ```ts import { inputDeviceCooperate } from '@kit.InputKit'; import { BusinessError } from '@kit.BasicServicesKit'; 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对象,异æ¥è¿”å›žé”®é¼ ç©¿è¶Šå¼€å¯ã€å…³é—结果。 | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | -------- | -----------------| | 401 | Parameter error. | **示例**: ```ts 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.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> | 是 | 回调函数,异æ¥è¿”å›žé”®é¼ ç©¿è¶Šå¯åŠ¨ã€åœæ¢çŠ¶æ€ã€‚| **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)å’Œ[ohos.multimodalinput错误ç ](errorcode-multimodalinput.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | -------- | ---------------------------------------- | | 401 | Parameter error. | | 4400001 | Incorrect descriptor for the target device. | | 4400002 | Screen hop failed. | **示例**: ```ts 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.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错误ç ](errorcode-multimodalinput.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | -------- | ---------------------------------------- | | 401 | Parameter error. | | 4400001 | Incorrect descriptor for the target device. | | 4400002 | Screen hop failed. | **示例**: ```ts 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.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> | 是 | 回调函数,异æ¥è¿”回åœæ¢é”®é¼ 穿越结果。 | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | -------- | ----------------- | | 401 | Parameter error. | **示例**: ```ts import { inputDeviceCooperate } from '@kit.InputKit'; import { BusinessError } from '@kit.BasicServicesKit'; 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对象,异æ¥è¿”回åœæ¢é”®é¼ 穿越结果。 | **示例**: ```ts import { inputDeviceCooperate } from '@kit.InputKit'; import { BusinessError } from '@kit.BasicServicesKit'; 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 }> | 是 | 回调函数,异æ¥è¿”å›žé”®é¼ ç©¿è¶Šå¼€å…³çŠ¶æ€ã€‚ | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | -------- | ----------------- | | 401 | Parameter error. | **示例**: ```ts import { inputDeviceCooperate } from '@kit.InputKit'; import { BusinessError } from '@kit.BasicServicesKit'; let deviceDescriptor = "descriptor"; try { inputDeviceCooperate.getState(deviceDescriptor, (error: BusinessError, data: object) => { 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对象,异æ¥è¿”å›žé”®é¼ ç©¿è¶Šå¼€å…³çŠ¶æ€ã€‚ | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | -------- | ----------------- | | 401 | Parameter error. | **示例**: ```ts 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.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](#eventmsg) }> | 是 | 回调函数,异æ¥è¿”å›žé”®é¼ ç©¿è¶Šäº‹ä»¶ã€‚ | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | -------- | ----------------- | | 401 | Parameter error. | **示例**: ```ts import { inputDeviceCooperate } from '@kit.InputKit'; function callback(msg: object) { console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`); 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> | å¦ | 需è¦å–æ¶ˆæ³¨å†Œçš„å›žè°ƒå‡½æ•°ï¼Œè‹¥æ— æ¤å‚数,则å–消当å‰åº”用注册的所有回调函数。 | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | -------- | ----------------- | | 401 | Parameter error. | **示例**: ```ts import { inputDeviceCooperate } from '@kit.InputKit'; // å–消注册å•ä¸ªå›žè°ƒå‡½æ•° function callbackOn(msg: object) { console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`); 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`])}`); } ``` ```ts import { inputDeviceCooperate } from '@kit.InputKit'; // å–消注册所有回调函数 function 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.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 | é”®é¼ ç©¿è¶ŠçŠ¶æ€ï¼Œè¡¨ç¤ºé”®é¼ 穿越状æ€å…³é—。 |