1# @ohos.cooperate (键鼠穿越)(系统接口) 2<!--Kit: Distributed Service Kit--> 3<!--Subsystem: Msdp--> 4<!--Owner: @wuliangdong--> 5<!--Designer: @butterls--> 6<!--Tester: @zhaodengqi--> 7<!--Adviser: @hu-zhiqiong--> 8 9键鼠穿越功能模块,提供两台或多台设备组网协同后键鼠共享能力,实现键鼠输入设备的跨设备协同操作。 10 11> **说明** 12> 13> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15> - 本模块接口均为系统接口。 16 17## 导入模块 18 19```ts 20import { cooperate } from '@kit.DistributedServiceKit'; 21``` 22 23## cooperate.prepareCooperate<sup>11+</sup> 24 25prepareCooperate(callback: AsyncCallback<void>): void; 26 27准备键鼠穿越,使用Callback异步回调。 28 29**需要权限**:ohos.permission.COOPERATE_MANAGER 30 31**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 32 33**参数**: 34 35| 参数名 | 类型 | 必填 | 说明 | 36| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 37| callback | AsyncCallback<void> | 是 | 回调函数,准备键鼠穿越成功时,err为undefined,否则为错误对象。 | 38 39**错误码:** 40 41以下错误码的详细介绍请参见[ohos.devicestatus错误码](errorcode-devicestatus.md)。 42 43| 错误码ID | 错误信息 | 44| -------- | ----------------- | 45| 201 | Permission denied. | 46| 202 | Permission verification failed. A non-system application calls a system API. | 47| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 48 49示例: 50 51```ts 52import { BusinessError } from '@kit.BasicServicesKit'; 53try { 54 cooperate.prepareCooperate((error: BusinessError) => { 55 if (error) { 56 console.error(`Keyboard mouse crossing prepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 57 return; 58 } 59 console.info(`Keyboard mouse crossing prepareCooperate success.`); 60 }); 61} catch (error) { 62 console.error(`Keyboard mouse crossing prepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 63} 64``` 65 66## cooperate.prepareCooperate<sup>11+</sup> 67 68prepareCooperate(): Promise<void>; 69 70准备键鼠穿越,使用Promise异步方式返回结果。 71 72**需要权限**:ohos.permission.COOPERATE_MANAGER 73 74**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 75 76**返回值:** 77 78| 参数 | 说明 | 79| ------------------- | ------------------------- | 80| Promise<void> | 无返回结果的Promise对象。 | 81 82**错误码:** 83 84以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 85 86| 错误码ID | 错误信息 | 87| -------- | ----------------- | 88| 201 | Permission denied. | 89| 202 | Permission verification failed. A non-system application calls a system API. | 90| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 91 92**示例**: 93 94```ts 95 96import { BusinessError } from '@kit.BasicServicesKit'; 97try { 98 cooperate.prepareCooperate().then(() => { 99 console.info(`Keyboard mouse crossing prepareCooperate success.`); 100 }, (error: BusinessError) => { 101 console.error(`Keyboard mouse crossing prepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 102 }); 103} catch (error) { 104 console.error(`Keyboard mouse crossing prepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 105} 106``` 107 108 109 110## cooperate.unprepareCooperate<sup>11+</sup> 111 112unprepareCooperate(callback: AsyncCallback<void>): void; 113 114取消键鼠穿越准备,使用Callback异步回调。 115 116**需要权限**:ohos.permission.COOPERATE_MANAGER 117 118**系统能力**: SystemCapability.Msdp.DeviceStatus.Cooperate 119 120| 参数名 | 类型 | 必填 | 说明 | 121| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 122| callback | AsyncCallback<void> | 是 | 回调函数,取消键鼠穿越准备成功时,err为undefined,否则为错误对象。 | 123 124**错误码:** 125 126以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 127 128| 错误码ID | 错误信息 | 129| -------- | ----------------- | 130| 201 | Permission denied. | 131| 202 | Permission verification failed. A non-system application calls a system API. | 132| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 133 134**示例:** 135 136```ts 137import { BusinessError } from '@kit.BasicServicesKit'; 138try { 139 cooperate.unprepareCooperate((error: BusinessError) => { 140 if (error) { 141 console.error(`Keyboard mouse crossing unprepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 142 return; 143 } 144 console.info(`Keyboard mouse crossing unprepareCooperate success.`); 145 }); 146} catch (error) { 147 console.error(`Keyboard mouse crossing unprepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 148} 149``` 150 151## cooperate.unprepareCooperate<sup>11+</sup> 152 153unprepareCooperate(): Promise<void>; 154 155取消键鼠穿越准备,使用Promise异步回调。 156 157**需要权限**:ohos.permission.COOPERATE_MANAGER 158 159**系统能力**: SystemCapability.Msdp.DeviceStatus.Cooperate 160 161**返回值:** 162 163| 参数 | 说明 | 164| ------------------- | ------------------------- | 165| Promise<void> | 无返回结果的Promise对象。 | 166 167**错误码:** 168 169以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 170 171| 错误码ID | 错误信息 | 172| -------- | ----------------- | 173| 201 | Permission denied. | 174| 202 | Permission verification failed. A non-system application calls a system API. | 175 176**示例:** 177 178```ts 179import { BusinessError } from '@kit.BasicServicesKit'; 180try { 181 cooperate.unprepareCooperate().then(() => { 182 console.info(`Keyboard mouse crossing unprepareCooperate success.`); 183 }, (error: BusinessError) => { 184 console.error(`Keyboard mouse crossing unprepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 185 }); 186} catch (error) { 187 console.error(`Keyboard mouse crossing unprepareCooperate failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 188} 189``` 190 191 192 193## cooperate.activateCooperate<sup>11+</sup> 194 195activateCooperate(targetNetworkId: string, inputDeviceId: number, callback: AsyncCallback<void>): void; 196 197启动键鼠穿越,使用Callback异步回调。 198 199**需要权限**:ohos.permission.COOPERATE_MANAGER 200 201**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 202 203**参数:** 204 205| 参数名 | 类型 | 必填 | 说明 | 206| --------------- | ------------------------- | ---- | ------------------------------------------------------------ | 207| targetNetworkId | string | 是 | 键鼠穿越目标设备描述符。 | 208| inputDeviceId | number | 是 | 待穿越输入设备标识符。 | 209| callback | AsyncCallback<void> | 是 | 回调函数,键鼠穿越启动成功时,err为undefined,否则为错误对象。 | 210 211**错误码:** 212 213以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[键鼠穿越管理错误码](errorcode-devicestatus.md)。 214 215| 错误码ID | 错误信息 | 216| -------- | ----------------- | 217| 201 | Permission denied. | 218| 202 | Permission verification failed. A non-system application calls a system API. | 219| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 220| 20900001 | Service exception. Possible causes: 1. A system error, such as null pointer, container-related exception, or IPC exception. 2. N-API invocation exception or invalid N-API status. | 221 222**示例:** 223 224```ts 225import { BusinessError } from '@kit.BasicServicesKit'; 226let targetNetworkId = "networkId"; 227let inputDeviceId = 0; 228try { 229 cooperate.activateCooperate(targetNetworkId, inputDeviceId, (error: BusinessError) => { 230 if (error) { 231 console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 232 return; 233 } 234 console.info(`Start Keyboard mouse crossing success.`); 235 }); 236} catch (error) { 237 console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 238} 239``` 240 241## cooperate.activateCooperate<sup>11+</sup> 242 243activateCooperate(targetNetworkId: string, inputDeviceId: number): Promise<void>; 244 245启动键鼠穿越,使用Promise异步回调。 246 247**需要权限**:ohos.permission.COOPERATE_MANAGER 248 249**系统能力**: SystemCapability.Msdp.DeviceStatus.Cooperate 250 251**参数:** 252 253| 参数名 | 类型 | 必填 | 说明 | 254| --------------- | ------ | ---- | ------------------------ | 255| targetNetworkId | string | 是 | 键鼠穿越目标设备描述符。 | 256| inputDeviceId | number | 是 | 待穿越输入设备标识符。 | 257 258**返回值:** 259 260| 参数名 | 说明 | 261| ------------------- | ------------------------- | 262| Promise<void> | 无返回结果的Promise对象。 | 263 264**错误码:** 265 266以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[键鼠穿越管理错误码](errorcode-devicestatus.md)。 267 268| 错误码ID | 错误信息 | 269| -------- | ----------------- | 270| 201 | Permission denied. | 271| 202 | Permission verification failed. A non-system application calls a system API.| 272| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 273| 20900001 | Service exception. Possible causes: 1. A system error, such as null pointer, container-related exception, or IPC exception. 2. N-API invocation exception or invalid N-API status. | 274 275**示例:** 276 277```ts 278import { BusinessError } from '@kit.BasicServicesKit'; 279let targetNetworkId = "networkId"; 280let inputDeviceId = 0; 281try { 282 cooperate.activateCooperate(targetNetworkId, inputDeviceId).then(() => { 283 console.info(`Start Keyboard mouse crossing success.`); 284 }, (error: BusinessError) => { 285 console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 286 }); 287} catch (error) { 288 console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 289} 290``` 291 292 293 294## cooperate.deactivateCooperate<sup>11+</sup> 295 296deactivateCooperate(isUnchained: boolean, callback: AsyncCallback<void>): void; 297 298停止键鼠穿越,使用Callback异步回调。 299 300**需要权限**:ohos.permission.COOPERATE_MANAGER 301 302**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 303 304**参数:** 305 306| 参数名 | 类型 | 必填 | 说明 | 307| ----------- | ------------------------- | ---- | ------------------------------------------------------------ | 308| isUnchained | boolean | 是 | 是否关闭跨设备链路。 true表示关闭跨设备链路,false表示不关闭。 | 309| callback | AsyncCallback<void> | 是 | 回调函数,键鼠穿越停止成功时,err为undefined,否则为错误对象。 | 310 311**错误码:** 312 313以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 314 315| 错误码ID | 错误信息 | 316| -------- | ----------------- | 317| 201 | Permission denied. | 318| 202 | Permission verification failed. A non-system application calls a system API. | 319| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 320 321**示例**: 322 323```ts 324import { BusinessError } from '@kit.BasicServicesKit'; 325try { 326 cooperate.deactivateCooperate(false, (error: BusinessError) => { 327 if (error) { 328 console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 329 return; 330 } 331 console.info(`Stop Keyboard mouse crossing success.`); 332 }); 333} catch (error) { 334 console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 335} 336``` 337 338## cooperate.deactivateCooperate<sup>11+</sup> 339 340deactivateCooperate(isUnchained: boolean): Promise<void>; 341 342停止键鼠穿越,使用Promise异步回调。 343 344**需要权限**:ohos.permission.COOPERATE_MANAGER 345 346**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 347 348**参数:** 349 350| 参数名 | 类型 | 必填 | 说明 | 351| ----------- | ------- | ---- | ------------------------------------------------------------ | 352| isUnchained | boolean | 是 | 是否关闭跨设备链路。 true表示关闭跨设备链路,false表示不关闭。 | 353 354**返回值:** 355 356| 参数名 | 说明 | 357| ------------------- | ------------------------- | 358| Promise<void> | 无返回结果的Promise对象。 | 359 360**错误码:** 361 362以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 363 364| 错误码ID | 错误信息 | 365| -------- | ----------------- | 366| 201 | Permission denied. | 367| 202 | Permission verification failed. A non-system application calls a system API. | 368 369**示例**: 370 371```ts 372import { BusinessError } from '@kit.BasicServicesKit'; 373try { 374 cooperate.deactivateCooperate(false).then(() => { 375 console.info(`Stop Keyboard mouse crossing success.`); 376 }, (error: BusinessError) => { 377 console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 378 }); 379} catch (error) { 380 console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 381} 382``` 383 384 385 386## cooperate.getCooperateSwitchState<sup>11+</sup> 387 388getCooperateSwitchState(networkId: string, callback: AsyncCallback<boolean>): void; 389 390获取目标设备键鼠穿越开关的状态,使用Callback异步回调。 391 392**需要权限**:ohos.permission.COOPERATE_MANAGER 393 394**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 395 396**参数:** 397 398| 参数名 | 类型 | 必填 | 说明 | 399| --------- | ---------------------------- | ---- | ------------------------------------------------------------ | 400| networkId | string | 是 | 键鼠穿越目标设备描述符。 | 401| callback | AsyncCallback<boolean> | 是 | 回调函数,返回true表示目标设备键鼠穿越的开关开启,返回false表示开关未开启。 | 402 403**错误码:** 404 405以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 406 407| 错误码ID | 错误信息 | 408| -------- | ----------------- | 409| 201 | Permission denied. | 410| 202 | Permission verification failed. A non-system application calls a system API. | 411| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 412 413**示例:** 414 415```ts 416import { BusinessError } from '@kit.BasicServicesKit'; 417let deviceDescriptor = "networkId"; 418try { 419 cooperate.getCooperateSwitchState(deviceDescriptor, (error: BusinessError, data: boolean) => { 420 if (error) { 421 console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 422 return; 423 } 424 console.info(`Get the status success, data: ${JSON.stringify(data)}`); 425 }); 426} catch (error) { 427 console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 428} 429``` 430 431## cooperate.getCooperateSwitchState<sup>11+</sup> 432 433getCooperateSwitchState(networkId: string): Promise<boolean>; 434 435获取目标设备键鼠穿越开关的状态,使用Promise异步方式返回结果。 436 437**需要权限**:ohos.permission.COOPERATE_MANAGER 438 439**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 440 441**参数**: 442 443| 参数名 | 类型 | 必填 | 说明 | 444| --------- | ------ | ---- | ------------------------ | 445| networkId | string | 是 | 键鼠穿越目标设备描述符。 | 446 447**返回值**: 448 449| 参数 | 说明 | 450| ---------------------- | ------------------------------------------------------------ | 451| Promise<boolean> | Promise对象,返回true表示目标设备键鼠穿越的开关开启,返回false表示开关未开启。 | 452 453**错误码:** 454 455以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 456 457| 错误码ID | 错误信息 | 458| -------- | ----------------- | 459| 201 | Permission denied. | 460| 202 | Permission verification failed. A non-system application calls a system API. | 461| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 462 463**示例**: 464 465```ts 466import { BusinessError } from '@kit.BasicServicesKit'; 467let deviceDescriptor = "networkId"; 468try { 469 cooperate.getCooperateSwitchState(deviceDescriptor).then((data: boolean) => { 470 console.info(`Get the status success, data: ${JSON.stringify(data)}`); 471 }, (error: BusinessError) => { 472 console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 473 }); 474} catch (error) { 475 console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 476} 477``` 478 479 480 481## on('cooperateMessage')<sup>11+</sup> 482 483on(type: 'cooperateMessage', callback: Callback<CooperateMessage>): void; 484 485注册监听键鼠穿越状态。 486 487**需要权限**:ohos.permission.COOPERATE_MANAGER 488 489**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 490 491**参数**: 492 493| 参数名 | 类型 | 必填 | 说明 | 494| -------- | ----------------------------------------------------- | ---- | ------------------------------------ | 495| type | string | 是 | 监听类型,取值为'cooperateMessage' | 496| callback | Callback<[CooperateMessage](#cooperatemessage11)> | 是 | 回调函数,异步返回键鼠穿越状态消息。 | 497 498**错误码:** 499 500以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 501 502| 错误码ID | 错误信息 | 503| -------- | ----------------- | 504| 201 | Permission denied. | 505| 202 | Permission verification failed. A non-system application calls a system API. | 506| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 507 508**示例**: 509 510```ts 511function callback(msg: cooperate.CooperateMessage) { 512 console.info(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`); 513 return false; 514} 515try { 516 cooperate.on('cooperateMessage', callback); 517} catch (error) { 518 console.error(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 519} 520``` 521 522 523 524## off('cooperateMessage')<sup>11+</sup> 525 526off(type: 'cooperateMessage', callback?: Callback<CooperateMessage>): void; 527 528取消监听键鼠穿越状态。 529 530**需要权限**:ohos.permission.COOPERATE_MANAGER 531 532**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 533 534**参数:** 535 536| 参数名 | 类型 | 必填 | 说明 | 537| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 538| type | string | 是 | 监听类型,取值为'cooperate'。 | 539| callback | Callback<[CooperateMessage](#cooperatemessage11)> | 否 | 需要取消注册的回调函数,若无此参数,则取消当前应用注册的所有回调函数。 | 540 541**错误码:** 542 543以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 544 545| 错误码ID | 错误信息 | 546| -------- | ----------------- | 547| 201 | Permission denied. | 548| 202 | Permission verification failed. A non-system application calls a system API. | 549| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 550 551**示例**: 552 553```ts 554// 取消注册单个回调函数 555function callbackOn(msgOn: cooperate.CooperateMessage) { 556 console.info(`Keyboard mouse crossing event: ${JSON.stringify(msgOn)}`); 557 return false; 558} 559function callbackOff(msgOff: cooperate.CooperateMessage) { 560 console.info(`Keyboard mouse crossing event: ${JSON.stringify(msgOff)}`); 561 return false; 562} 563try { 564 cooperate.on('cooperateMessage', callbackOn); 565 cooperate.off('cooperateMessage', callbackOff); 566} catch (error) { 567 console.error(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 568} 569``` 570 571```ts 572// 取消注册所有回调函数 573import { cooperate } from '@kit.DistributedServiceKit'; 574function callbackOn(msg: cooperate.CooperateMessage) { 575 console.info(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`); 576 return false; 577} 578try { 579 cooperate.on('cooperateMessage', callbackOn); 580 cooperate.off('cooperateMessage'); 581} catch (error) { 582 console.error(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 583} 584``` 585 586 587## on('cooperateMouse')<sup>12+</sup> 588 589on(type: 'cooperateMouse', networkId: string, callback: Callback<MouseLocation>): void; 590 591注册监听指定设备鼠标光标位置。 592 593**需要权限**:ohos.permission.COOPERATE_MANAGER 594 595**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 596 597**参数**: 598 599| 参数名 | 类型 | 必填 | 说明 | 600| -------- | ----------------------------------------------------- | ---- | ------------------------------------ | 601| type | string | 是 | 监听类型,取值为'cooperateMouse' | 602| networkId| string | 是 | 目标设备描述符 | 603| callback | Callback<[MouseLocation](#mouselocation12)> | 是 | 回调函数,异步返回指定监听设备鼠标光标位置信息。 | 604 605**错误码:** 606 607以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 608 609| 错误码ID | 错误信息 | 610| -------- | ----------------- | 611| 201 | Permission denied. | 612| 202 | Permission verification failed. A non-system application calls a system API. | 613| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 614 615**示例**: 616 617```ts 618function callback(data: cooperate.MouseLocation) { 619 console.info('displayX:' + data.displayX + 'displayY:' + data.displayX + 'displayWidth:' + 620 data.displayWidth + 'displayHeight:' + data.displayHeight ); 621} 622try { 623 let networkId: string = 'Default'; 624 cooperate.on('cooperateMouse', networkId, callback); 625} catch (error) { 626 console.error(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 627} 628``` 629 630 631 632## off('cooperateMouse')<sup>12+</sup> 633 634off(type: 'cooperateMouse', networkId: string, callback?: Callback<MouseLocation>): void; 635 636取消监听指定设备鼠标光标位置。 637 638**需要权限**:ohos.permission.COOPERATE_MANAGER 639 640**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 641 642**参数:** 643 644| 参数名 | 类型 | 必填 | 说明 | 645| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 646| type | string | 是 | 监听类型,取值为'cooperateMouse'。 | 647| networkId| string | 是 | 目标设备描述符 | 648| callback | Callback<[MouseLocation](#mouselocation12)> | 否 | 需要取消注册的回调函数,若无此参数,则取消当前应用注册的所有回调函数。 | 649 650**错误码:** 651 652以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 653 654| 错误码ID | 错误信息 | 655| -------- | ----------------- | 656| 201 | Permission denied. | 657| 202 | Permission verification failed. A non-system application calls a system API. | 658| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 659 660**示例**: 661 662```ts 663// 取消注册单个回调函数 664function callbackOn(data: cooperate.MouseLocation) { 665 console.info('Register mouse location listener'); 666 return false; 667} 668function callbackOff(data: cooperate.MouseLocation) { 669 console.info('Unregister mouse location listener'); 670 return false; 671} 672try { 673 let networkId: string = 'Default'; 674 cooperate.on('cooperateMouse', networkId, callbackOn); 675 cooperate.off('cooperateMouse', networkId, callbackOff); 676} catch (error) { 677 console.error(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 678} 679``` 680 681```ts 682// 取消注册所有回调函数 683function callbackOn(data: cooperate.MouseLocation) { 684 console.info('Register mouse location listener'); 685} 686try { 687 let networkId: string = 'Default'; 688 cooperate.on('cooperateMouse', networkId, callbackOn); 689 cooperate.off('cooperateMouse', networkId); 690} catch (error) { 691 console.error(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 692} 693``` 694 695## cooperate.activateCooperateWithOptions<sup>20+</sup> 696 697activateCooperateWithOptions(targetNetworkId: string, inputDeviceId: number, 698 cooperateOptions?: CooperateOptions ): Promise<void> 699 700启动键鼠穿越,使用选项开始屏幕跳转。 701 702**需要权限**:ohos.permission.COOPERATE_MANAGER 703 704**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 705 706**参数**: 707 708| 参数名 | 类型 | 必填 | 说明 | 709| --------- | ------ | ---- | ------------------------ | 710| targetNetworkId | string | 是 | 键鼠穿越目标设备描述符。 | 711| inputDeviceId | number | 是 | 发起穿越操作的输入设备ID。 | 712|cooperateOptions | [CooperateOptions](#cooperateoptions20) | 否 | 穿越可选控制参数,用于控制穿出点具体位置等。不设置此参数时,本接口能力与[cooperate.activateCooperate](#cooperateactivatecooperate11-1)相同。 | 713 714**返回值:** 715 716| 参数 | 说明 | 717| ------------------- | ------------------------- | 718| Promise<void> | 无返回结果的Promise对象。 | 719 720**错误码:** 721 722以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[键鼠穿越管理错误码](errorcode-devicestatus.md)。 723 724| 错误码ID | 错误信息 | 725| -------- | ----------------- | 726| 201 | Permission denied. | 727| 202 | Permission verification failed. A non-system application calls a system API. | 728| 20900001 | Operation failed. | 729 730**示例**: 731 732```ts 733import { BusinessError } from '@kit.BasicServicesKit'; 734let targetNetworkId = "networkId"; 735let inputDeviceId = 0; 736try { 737 cooperate.activateCooperateWithOptions(targetNetworkId, inputDeviceId).then(() => { 738 console.info(`activateCooperateWithOptions success.`); 739 }, (error: BusinessError) => { 740 console.error(`activateCooperateWithOptions, error: ${JSON.stringify(error, [`code`, `message`])}`); 741 }); 742} catch (error) { 743 console.error(`activateCooperateWithOptions, error: ${JSON.stringify(error, [`code`, `message`])}`); 744} 745``` 746 747 748## CooperateMessage<sup>11+</sup> 749 750键鼠穿越的消息。 751 752**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 753 754| 名称 | 类型 |只读 | 可选 | 说明 | 755| --------- | -------------- | ---- | ---- | ------------------------ | 756| networkId | string | 是 | 否 | 键鼠穿越目标设备描述符。 | 757| state | CooperateState | 是 | 否 | 键鼠穿越的状态。 | 758 759 760## MouseLocation<sup>12+</sup> 761 762键鼠穿越的位置。 763 764**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 765 766| 名称 | 类型 | 只读 | 可选 | 说明 | 767| --------- | -------------- | ---- | ---- | ------------------------ | 768| displayX | number | 是 | 否 | 鼠标指针位于屏幕的X坐标上的位置。 | 769| displayY | number | 是 | 否 | 鼠标指针位于屏幕的Y坐标上的位置。 | 770| displayWidth | number | 是 | 否 | 屏幕宽度,单位:px。 | 771| displayHeight | number | 是 | 否 | 屏幕高度,单位:px。 | 772 773## CooperateState<sup>11+</sup> 774 775键鼠穿越状态的枚举。 776 777**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 778 779| 名称 | 值 | 说明 | 780| ------------------------------ | ---- | ---------------------- | 781| COOPERATE_PREPARE | 0 | 表示准备键鼠穿越。 | 782| COOPERATE_UNPREPARE | 1 | 表示取消键鼠穿越准备。 | 783| COOPERATE_ACTIVATE | 2 | 表示启动键鼠穿越。 | 784| COOPERATE_ACTIVATE_SUCCESS | 3 | 表示键鼠穿越启动成功。 | 785| COOPERATE_ACTIVATE_FAILURE | 4 | 表示键鼠穿越无法启动。 | 786| COOPERATE_DEACTIVATE_SUCCESS | 5 | 表示键鼠穿越停止成功。 | 787| COOPERATE_DEACTIVATE_FAILURE | 6 | 表示键鼠穿越无法停止。 | 788| COOPERATE_SESSION_DISCONNECTED | 7 | 表示键鼠穿越会话断开。 | 789 790## CooperateOptions<sup>20+</sup> 791 792 键鼠穿越可选控制参数,控制穿出点位置。 793 794**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 795 796| 名称 | 类型 | 只读 | 可选 | 说明 | 797| --------- | -------------- | ---- | ---- | ------------------------ | 798| displayX | number | 否 | 否 | 鼠标X坐标位置。 | 799| displayY | number | 否 | 否 | 鼠标Y坐标位置。 | 800| displayId | number | 否 | 否 | 对端设备屏幕标识。 | 801 802## cooperate.prepare<sup>(deprecated)</sup> 803 804prepare(callback: AsyncCallback<void>): void; 805 806准备键鼠穿越,使用Callback异步回调。 807 808> **说明:** 809> 810> 从API version 10开始不再维护。建议使用[cooperate.prepareCooperate](#cooperatepreparecooperate11)替代 811 812**系统能力**: SystemCapability.Msdp.DeviceStatus.Cooperate 813 814**参数**: 815 816| 参数名 | 类型 | 必填 | 说明 | 817| -------- | ------------------------- | ---- | --------------------------- | 818| callback | AsyncCallback<void> | 是 |回调函数,准备键鼠穿越成功时,err为undefined,否则为错误对象。 | 819 820**错误码:** 821 822以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 823 824| 错误码ID | 错误信息 | 825| -------- | ----------------- | 826| 202 | Permission verification failed. A non-system application calls a system API. | 827| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 828 829**示例**: 830 831```ts 832import { BusinessError } from '@kit.BasicServicesKit'; 833try { 834 cooperate.prepare((error: BusinessError) => { 835 if (error) { 836 console.error(`Keyboard mouse crossing prepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 837 return; 838 } 839 console.info(`Keyboard mouse crossing prepare success.`); 840 }); 841} catch (error) { 842 console.error(`Keyboard mouse crossing prepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 843} 844``` 845 846## cooperate.prepare<sup>(deprecated)</sup> 847 848prepare(): Promise<void>; 849 850准备键鼠穿越,使用Promise异步方式返回结果。 851 852> **说明:** 853> 854> 从API version 10开始不再维护。建议使用[cooperate.prepareCooperate](#cooperatepreparecooperate11-1)替代 855 856**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 857 858**返回值**: 859 860| 参数 | 说明 | 861| ------------------- | ------------------------------- | 862| Promise<void> | 无返回结果的Promise对象。 | 863 864**错误码:** 865 866以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 867 868| 错误码ID | 错误信息 | 869| -------- | ----------------- | 870| 202 | Permission verification failed. A non-system application calls a system API.| 871| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 872 873**示例**: 874 875```ts 876import { BusinessError } from '@kit.BasicServicesKit'; 877try { 878 cooperate.prepare().then(() => { 879 console.info(`Keyboard mouse crossing prepare success.`); 880 }, (error: BusinessError) => { 881 console.error(`Keyboard mouse crossing prepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 882 }); 883} catch (error) { 884 console.error(`Keyboard mouse crossing prepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 885} 886``` 887 888 889 890## cooperate.unprepare<sup>(deprecated)</sup> 891 892unprepare(callback: AsyncCallback<void>): void; 893 894取消键鼠穿越准备,使用Callback异步回调。 895 896> **说明:** 897> 898> 从API version 10开始不再维护。建议使用[cooperate.unprepareCooperate](#cooperateunpreparecooperate11)替代 899 900**系统能力**: SystemCapability.Msdp.DeviceStatus.Cooperate 901 902| 参数名 | 类型 | 必填 | 说明 | 903| -------- | ------------------------- | ---- | ------------------------------------------ | 904| callback | AsyncCallback<void> | 是 | 回调函数,取消键鼠穿越准备成功时,err为undefined,否则为错误对象。 | 905 906**错误码:** 907 908以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 909 910| 错误码ID | 错误信息 | 911| -------- | ----------------- | 912| 202 | Permission verification failed. A non-system application calls a system API.| 913| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 914 915**示例**: 916 917```ts 918import { BusinessError } from '@kit.BasicServicesKit'; 919try { 920 cooperate.unprepare((error: BusinessError) => { 921 if (error) { 922 console.error(`Keyboard mouse crossing unprepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 923 return; 924 } 925 console.info(`Keyboard mouse crossing unprepare success.`); 926 }); 927} catch (error) { 928 console.error(`Keyboard mouse crossing unprepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 929} 930``` 931 932## cooperate.unprepare<sup>(deprecated)</sup> 933 934unprepare(): Promise<void>; 935 936取消键鼠穿越准备,使用Promise异步回调。 937 938> **说明:** 939> 940> 从API version 10开始不再维护。建议使用[cooperate.unprepareCooperate](#cooperateunpreparecooperate11-1)替代 941 942**系统能力**: SystemCapability.Msdp.DeviceStatus.Cooperate 943 944**返回值**: 945 946| 参数 | 说明 | 947| ------------------- | --------------------------------------------- | 948| Promise<void> | 无返回结果的Promise对象。 | 949 950**错误码:** 951 952以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 953 954| 错误码ID | 错误信息 | 955| -------- | ----------------- | 956| 202 | Permission verification failed. A non-system application calls a system API. | 957| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 958 959**示例:** 960 961```ts 962import { BusinessError } from '@kit.BasicServicesKit'; 963try { 964 cooperate.unprepare().then(() => { 965 console.info(`Keyboard mouse crossing unprepare success.`); 966 }, (error: BusinessError) => { 967 console.error(`Keyboard mouse crossing unprepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 968 }); 969} catch (error) { 970 console.error(`Keyboard mouse crossing unprepare failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 971} 972``` 973 974 975 976## cooperate.activate<sup>(deprecated)</sup> 977 978activate(targetNetworkId: string, inputDeviceId: number, callback: AsyncCallback<void>): void; 979 980启动键鼠穿越,使用Callback异步回调。 981 982> **说明:** 983> 984> 从API version 10开始不再维护。建议使用[cooperate.activateCooperate](#cooperateactivatecooperate11)替代 985 986**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 987 988**参数**: 989 990| 参数名 | 类型 | 必填 | 说明 | 991| -------- | ---------------------------- | ---- | ---------------------------- | 992| targetNetworkId | string | 是 | 键鼠穿越目标设备描述符。 | 993| inputDeviceId | number | 是 | 待穿越输入设备标识符。 | 994| callback | AsyncCallback<void> | 是 | 回调函数,键鼠穿越启动成功时,err为undefined,否则为错误对象。 | 995 996**错误码:** 997 998以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[键鼠穿越管理错误码](errorcode-devicestatus.md)。 999 1000| 错误码ID | 错误信息 | 1001| -------- | ---------------------------------------- | 1002| 20900001 | Service exception. Possible causes: 1. A system error, such as null pointer, container-related exception, or IPC exception. 2. N-API invocation exception or invalid N-API status. | 1003| 202 | Permission verification failed. A non-system application calls a system API.| 1004| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 1005 1006**示例**: 1007 1008```ts 1009import { BusinessError } from '@kit.BasicServicesKit'; 1010let targetNetworkId = "networkId"; 1011let inputDeviceId = 0; 1012try { 1013 cooperate.activate(targetNetworkId, inputDeviceId, (error: BusinessError) => { 1014 if (error) { 1015 console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1016 return; 1017 } 1018 console.info(`Start Keyboard mouse crossing success.`); 1019 }); 1020} catch (error) { 1021 console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1022} 1023``` 1024 1025## cooperate.activate<sup>(deprecated)</sup> 1026 1027activate(targetNetworkId: string, inputDeviceId: number): Promise<void>; 1028 1029启动键鼠穿越,使用Promise异步回调。 1030 1031> **说明:** 1032> 1033> 从API version 10开始不再维护。建议使用[cooperate.activateCooperate](#cooperateactivatecooperate11-1)替代 1034 1035**系统能力**: SystemCapability.Msdp.DeviceStatus.Cooperate 1036 1037**参数**: 1038 1039| 参数名 | 类型 | 必填 | 说明 | 1040| -------- | ---------------------------- | ---- | ---------------------------- | 1041| targetNetworkId | string | 是 | 键鼠穿越目标设备描述符。 | 1042| inputDeviceId | number | 是 | 待穿越输入设备标识符。 | 1043 1044 1045 1046**返回值**: 1047 1048| 参数名 | 说明 | 1049| ---------------------- | ------------------------------- | 1050| Promise<void> | 无返回结果的Promise对象。 | 1051 1052**错误码:** 1053 1054以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[键鼠穿越管理错误码](errorcode-devicestatus.md)。 1055 1056| 错误码ID | 错误信息 | 1057| -------- | ---------------------------------------- | 1058| 20900001 | Service exception. Possible causes: 1. A system error, such as null pointer, container-related exception, or IPC exception. 2. N-API invocation exception or invalid N-API status.| 1059| 202 | Permission verification failed. A non-system application calls a system API. | 1060| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 1061 1062**示例**: 1063 1064```ts 1065import { BusinessError } from '@kit.BasicServicesKit'; 1066let targetNetworkId = "networkId"; 1067let inputDeviceId = 0; 1068try { 1069 cooperate.activate(targetNetworkId, inputDeviceId).then(() => { 1070 console.info(`Start Keyboard mouse crossing success.`); 1071 }, (error: BusinessError) => { 1072 console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1073 }); 1074} catch (error) { 1075 console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1076} 1077``` 1078 1079 1080 1081## cooperate.deactivate<sup>(deprecated)</sup> 1082 1083deactivate(isUnchained: boolean, callback: AsyncCallback<void>): void; 1084 1085停止键鼠穿越,使用Callback异步回调。 1086 1087> **说明:** 1088> 1089> 从API version 10开始不再维护。建议使用[cooperate.deactivateCooperate](#cooperatedeactivatecooperate11)替代 1090 1091**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 1092 1093**参数**: 1094 1095| 参数名 | 类型 | 必填 | 说明 | 1096| -------- | ---------------------------- | ---- | ---------------------------- | 1097| isUnchained | boolean | 是 | 是否关闭跨设备链路。<br> true表示关闭跨设备链路,false表示不关闭。 | 1098| callback | AsyncCallback<void> | 是 | 回调函数,键鼠穿越停止成功时,err为undefined,否则为错误对象。| 1099 1100**错误码:** 1101 1102以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 1103 1104| 错误码ID | 错误信息 | 1105| -------- | ----------------- | 1106| 202 |Permission verification failed. A non-system application calls a system API. | 1107| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 1108 1109**示例**: 1110 1111```ts 1112import { BusinessError } from '@kit.BasicServicesKit'; 1113try { 1114 cooperate.deactivate(false, (error: BusinessError) => { 1115 if (error) { 1116 console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1117 return; 1118 } 1119 console.info(`Stop Keyboard mouse crossing success.`); 1120 }); 1121} catch (error) { 1122 console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1123} 1124``` 1125 1126## cooperate.deactivate<sup>(deprecated)</sup> 1127 1128deactivate(isUnchained: boolean): Promise<void>; 1129 1130停止键鼠穿越,使用Promise异步回调。 1131 1132> **说明:** 1133> 1134> 从API version 10开始不再维护。建议使用[cooperate.deactivateCooperate](#cooperatedeactivatecooperate11-1)替代 1135 1136**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 1137 1138**参数**: 1139 1140| 参数名 | 类型 | 必填 | 说明 | 1141| ----------- | ------- | ---- | ------------------ | 1142| isUnchained | boolean | 是 | 是否关闭跨设备链路。<br> true表示关闭跨设备链路,false表示不关闭。 | 1143 1144 1145 1146**返回值**: 1147 1148| 参数名 | 说明 | 1149| -------- | ---------------------------- | 1150| Promise<void> | 无返回结果的Promise对象。| 1151 1152**错误码:** 1153 1154以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 1155 1156| 错误码ID | 错误信息 | 1157| -------- | ----------------- | 1158| 202 | Permission verification failed. A non-system application calls a system API. | 1159 1160**示例**: 1161 1162```ts 1163import { BusinessError } from '@kit.BasicServicesKit'; 1164try { 1165 cooperate.deactivate(false).then(() => { 1166 console.info(`Stop Keyboard mouse crossing success.`); 1167 }, (error: BusinessError) => { 1168 console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1169 }); 1170} catch (error) { 1171 console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1172} 1173``` 1174 1175 1176 1177## cooperate.getCrossingSwitchState<sup>(deprecated)</sup> 1178 1179getCrossingSwitchState(networkId: string, callback: AsyncCallback<boolean>): void; 1180 1181获取目标设备键鼠穿越开关的状态,使用Callback异步回调。 1182 1183> **说明:** 1184> 1185> 从API version 10开始不再维护。建议使用[cooperate.getCooperateSwitchState](#cooperategetcooperateswitchstate11)替代 1186 1187**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 1188 1189**参数**: 1190 1191| 参数名 | 类型 | 必填 | 说明 | 1192| -------- | --------- | ---- | ---------------------------- | 1193| networkId | string | 是 | 键鼠穿越目标设备描述符。 | 1194| callback | AsyncCallback<boolean> | 是 | 回调函数,返回true表示目标设备键鼠穿越的开关开启,返回false表示开关未开启。 | 1195 1196**错误码:** 1197 1198以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 1199 1200| 错误码ID | 错误信息 | 1201| -------- | ----------------- | 1202| 202 | Permission verification failed. A non-system application calls a system API. | 1203| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 1204 1205**示例**: 1206 1207```ts 1208import { BusinessError } from '@kit.BasicServicesKit'; 1209let deviceDescriptor = "networkId"; 1210try { 1211 cooperate.getCrossingSwitchState(deviceDescriptor, (error: BusinessError, data: boolean) => { 1212 if (error) { 1213 console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1214 return; 1215 } 1216 console.info(`Get the status success, data: ${JSON.stringify(data)}`); 1217 }); 1218} catch (error) { 1219 console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1220} 1221``` 1222 1223## cooperate.getCrossingSwitchState<sup>(deprecated)</sup> 1224 1225getCrossingSwitchState(networkId: string): Promise<boolean>; 1226 1227获取目标设备键鼠穿越开关的状态,使用Promise异步方式返回结果。 1228 1229> **说明:** 1230> 1231> 从API version 10开始不再维护。建议使用[cooperate.getCooperateSwitchState](#cooperategetcooperateswitchstate11-1)替代 1232 1233**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 1234 1235**参数**: 1236 1237| 参数名 | 类型 | 必填 | 说明 | 1238| -------- | --------- | ---- | ---------------------------- | 1239| networkId | string | 是 | 键鼠穿越目标设备描述符。 | 1240 1241**错误码:** 1242 1243以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 1244 1245| 错误码ID | 错误信息 | 1246| -------- | ----------------- | 1247| 202 | Permission verification failed. A non-system application calls a system API. | 1248| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 1249 1250**返回值**: 1251 1252| 参数 | 说明 | 1253| ------------------- | ------------------------------- | 1254| Promise<boolean> | Promise对象,返回true表示目标设备键鼠穿越的开关开启,返回false表示开关未开启。 | 1255 1256 1257 1258**示例**: 1259 1260```ts 1261import { BusinessError } from '@kit.BasicServicesKit'; 1262let deviceDescriptor = "networkId"; 1263try { 1264 cooperate.getCrossingSwitchState(deviceDescriptor).then((data: boolean) => { 1265 console.info(`Get the status success, data: ${JSON.stringify(data)}`); 1266 }, (error: BusinessError) => { 1267 console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1268 }); 1269} catch (error) { 1270 console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1271} 1272``` 1273 1274 1275 1276## on('cooperate')<sup>(deprecated)</sup> 1277 1278on(type: 'cooperate', callback: Callback<{ networkId: string, msg: CooperateMsg }>): void; 1279 1280注册监听键鼠穿越状态。 1281 1282> **说明:** 1283> 1284> 从API version 10开始不再维护。建议使用[on('cooperateMessage')](#oncooperatemessage11)替代 1285 1286**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 1287 1288**参数**: 1289 1290| 参数名 | 类型 | 必填 | 说明 | 1291| -------- | ---------------------------- | ---- | ---------------------------- | 1292| type | string | 是 | 监听类型,取值为'cooperate' | 1293| callback | Callback<{ networkId: string, msg: [CooperateMsg](#cooperatemsgdeprecated) }> | 是 | 回调函数,异步返回键鼠穿越状态消息。 | 1294 1295**错误码:** 1296 1297以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 1298 1299| 错误码ID | 错误信息 | 1300| -------- | ----------------- | 1301| 202 | Permission verification failed. A non-system application calls a system API. | 1302| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 1303 1304**示例**: 1305 1306```ts 1307import { cooperate } from '@kit.DistributedServiceKit'; 1308class Data { 1309 networkId: string = "networkId"; 1310 msg: cooperate.CooperateMsg = 0; 1311} 1312try { 1313 cooperate.on('cooperate', (data: Data)=>{ 1314 console.info(`Keyboard mouse crossing event: ${JSON.stringify(data)}`); 1315 }); 1316} catch (error) { 1317 console.error(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1318} 1319``` 1320 1321 1322 1323## off('cooperate')<sup>(deprecated)</sup> 1324 1325off(type: 'cooperate', callback?: Callback<void>): void; 1326 1327取消监听键鼠穿越状态。 1328 1329> **说明:** 1330> 1331> 从API version 10开始不再维护。建议使用[off('cooperateMessage')](#offcooperatemessage11)替代 1332 1333**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 1334 1335**参数**: 1336 1337| 参数名 | 类型 | 必填 | 说明 | 1338| -------- | ---------------------------- | ---- | ---------------------------- | 1339| type | string | 是 | 监听类型,取值为'cooperate'。 | 1340| callback | AsyncCallback<void> | 否 | 需要取消注册的回调函数,若无此参数,则取消当前应用注册的所有回调函数。 | 1341 1342**错误码:** 1343 1344以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 1345 1346| 错误码ID | 错误信息 | 1347| -------- | ----------------- | 1348| 202 | Permission verification failed. A non-system application calls a system API. | 1349| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.2.Incorrect parameter types.3.Parameter verification failed. | 1350 1351**示例**: 1352 1353```ts 1354// 取消注册单个回调函数 1355class Data { 1356 networkId: string = "networkId"; 1357 msg: cooperate.CooperateMsg = 0; 1358} 1359function callbackOff() { 1360 console.info(`Keyboard mouse crossing event`); 1361 return false; 1362} 1363try { 1364 cooperate.on('cooperate', (data: Data)=>{ 1365 console.info(`Keyboard mouse crossing event: ${JSON.stringify(data)}`); 1366 }); 1367 cooperate.off('cooperate', callbackOff); 1368} catch (error) { 1369 console.error(`Register failed, error: ${JSON.stringify(error)}`); 1370} 1371``` 1372```ts 1373// 取消注册所有回调函数 1374class Data { 1375 networkId: string = "networkId"; 1376 msg: cooperate.CooperateMsg = 0; 1377} 1378try { 1379 cooperate.on('cooperate', (data: Data)=>{ 1380 console.info(`Keyboard mouse crossing event: ${JSON.stringify(data)}`); 1381 }); 1382 cooperate.off('cooperate'); 1383} catch (error) { 1384 console.error(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1385} 1386``` 1387 1388 1389 1390## CooperateMsg<sup>(deprecated)</sup> 1391 1392键鼠穿越的消息通知。 1393 1394> **说明:** 1395> 1396> 从API version 10开始不再维护。建议使用[CooperateMessage](#cooperatemessage11)替代 1397 1398**系统能力**:SystemCapability.Msdp.DeviceStatus.Cooperate 1399 1400| 名称 | 值 | 说明 | 1401| -------- | ----------------- | ----------------- | 1402| COOPERATE_PREPARE | 0 | 表示准备键鼠穿越。 | 1403| COOPERATE_UNPREPARE | 1 | 表示取消键鼠穿越准备。 | 1404| COOPERATE_ACTIVATE | 2 | 表示启动键鼠穿越。 | 1405| COOPERATE_ACTIVATE_SUCCESS | 3 | 表示键鼠穿越启动成功。 | 1406| COOPERATE_ACTIVATE_FAIL | 4 | 表示键鼠穿越启动失败。 | 1407| COOPERATE_DEACTIVATE_SUCCESS | 5 | 表示键鼠穿越停止成功。 | 1408| COOPERATE_DEACTIVATE_FAIL | 6 | 表示键鼠穿越停止失败。 | 1409| COOPERATE_SESSION_DISCONNECTED | 7 | 表示键鼠穿越会话断开。 |