1# @ohos.multimodalInput.inputDeviceCooperate (键鼠穿越)(系统接口) 2 3键鼠穿越功能模块,提供两台或多台设备组网协同后键鼠共享能力,实现键鼠输入设备的跨设备协同操作。 4 5> **说明** 6> 7>- 从API Version 10开始,该接口不再维护,推荐使用新接口[@ohos.cooperate](../apis-distributedservice-kit/js-apis-devicestatus-cooperate-sys.md) (键鼠穿越)。 8> 9>- 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10> 11>- 本模块接口均为系统接口。 12 13## 导入模块 14 15```ts 16import { inputDeviceCooperate } from '@kit.InputKit'; 17``` 18 19## inputDeviceCooperate.enable 20 21enable(enable: boolean, callback: AsyncCallback<void>): void 22 23开启、关闭键鼠穿越,使用AsyncCallback异步方式返回结果。 24 25**系统能力**: SystemCapability.MultimodalInput.Input.Cooperator 26 27**参数**: 28 29| 参数名 | 类型 | 必填 | 说明 | 30| -------- | ------------------------- | ---- | --------------------------- | 31| enable | boolean | 是 | 键鼠穿越使能状态。 | 32| callback | AsyncCallback<void> | 是 |回调函数,异步返回键鼠穿越开启、关闭结果。 | 33 34**错误码**: 35 36以下错误码的详细介绍请参见[ohos.devicestatus错误码](../apis-distributedservice-kit/errorcode-devicestatus.md)。 37 38| 错误码ID | 错误信息 | 39| -------- | -----------------| 40| 401 | Parameter error. | 41 42 43**示例**: 44 45```ts 46import { inputDeviceCooperate } from '@kit.InputKit'; 47import { BusinessError } from '@kit.BasicServicesKit'; 48 49try { 50 inputDeviceCooperate.enable(true, (error: BusinessError) => { 51 if (error) { 52 console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 53 return; 54 } 55 console.log(`Keyboard mouse crossing enable success.`); 56 }); 57} catch (error) { 58 console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 59} 60``` 61 62## inputDeviceCooperate.enable 63 64enable(enable: boolean): Promise<void> 65 66开启、关闭键鼠穿越,使用Promise异步方式返回结果。 67 68 69**系统能力**: SystemCapability.MultimodalInput.Input.Cooperator 70 71**参数**: 72 73| 参数名 | 类型 | 必填 | 说明 | 74| --------- | ------- | ---- | ------------------------------------------------------------------- | 75| enable | boolean | 是 | 键鼠穿越使能状态。 | 76 77**返回值**: 78 79| 参数 | 说明 | 80| ------------------- | ------------------------------- | 81| Promise<void> | Promise对象,异步返回键鼠穿越开启、关闭结果。 | 82 83**错误码**: 84 85以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 86 87| 错误码ID | 错误信息 | 88| -------- | -----------------| 89| 401 | Parameter error. | 90 91**示例**: 92 93```ts 94import { inputDeviceCooperate } from '@kit.InputKit'; 95import { BusinessError } from '@kit.BasicServicesKit'; 96 97try { 98 inputDeviceCooperate.enable(true).then(() => { 99 console.log(`Keyboard mouse crossing enable success.`); 100 }, (error: BusinessError) => { 101 console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 102 }); 103} catch (error) { 104 console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 105} 106``` 107 108## inputDeviceCooperate.start 109 110start(sinkDeviceDescriptor: string, srcInputDeviceId: number, callback: AsyncCallback\<void>): void 111 112启动键鼠穿越,使用AsyncCallback异步方式返回结果。 113 114**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator 115 116**参数**: 117 118| 参数名 | 类型 | 必填 | 说明 | 119| -------- | ---------------------------- | ---- | ---------------------------- | 120| sinkDeviceDescriptor | string | 是 | 键鼠穿越目标设备描述符。 | 121| srcInputDeviceId | number | 是 | 键鼠穿越待穿越外设标识符。 | 122| callback | AsyncCallback\<void> | 是 | 回调函数,异步返回键鼠穿越启动、停止状态。| 123 124**错误码**: 125 126以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.multimodalinput错误码](errorcode-multimodalinput.md)。 127 128| 错误码ID | 错误信息 | 129| -------- | ---------------------------------------- | 130| 401 | Parameter error. | 131| 4400001 | Incorrect descriptor for the target device. | 132| 4400002 | Screen hop failed. | 133 134**示例**: 135 136```ts 137import { inputDeviceCooperate } from '@kit.InputKit'; 138import { BusinessError } from '@kit.BasicServicesKit'; 139 140let sinkDeviceDescriptor = "descriptor"; 141let srcInputDeviceId = 0; 142try { 143 inputDeviceCooperate.start(sinkDeviceDescriptor, srcInputDeviceId, (error: BusinessError) => { 144 if (error) { 145 console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 146 return; 147 } 148 console.log(`Start Keyboard mouse crossing success.`); 149 }); 150} catch (error) { 151 console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 152} 153``` 154 155## inputDeviceCooperate.start 156 157start(sinkDeviceDescriptor: string, srcInputDeviceId: number): Promise\<void> 158 159启动键鼠穿越,使用Promise异步方式返回结果。 160 161**系统能力**: SystemCapability.MultimodalInput.Input.Cooperator 162 163**参数**: 164 165| 参数名 | 类型 | 必填 | 说明 | 166| -------- | ---------------------------- | ---- | ---------------------------- | 167| sinkDeviceDescriptor | string | 是 | 键鼠穿越目标设备描述符。 | 168| srcInputDeviceId | number | 是 | 键鼠穿越待穿越外设标识符。 | 169 170 171 172**返回值**: 173 174| 参数名 | 说明 | 175| ---------------------- | ------------------------------- | 176| Promise\<void> | Promise对象,异步返回键鼠穿越启动、关闭结果。 | 177 178**错误码**: 179 180以下错误码的详细介绍请参见[ohos.multimodalinput错误码](errorcode-multimodalinput.md)。 181 182| 错误码ID | 错误信息 | 183| -------- | ---------------------------------------- | 184| 401 | Parameter error. | 185| 4400001 | Incorrect descriptor for the target device. | 186| 4400002 | Screen hop failed. | 187 188**示例**: 189 190```ts 191import { inputDeviceCooperate } from '@kit.InputKit'; 192import { BusinessError } from '@kit.BasicServicesKit'; 193 194let sinkDeviceDescriptor = "descriptor"; 195let srcInputDeviceId = 0; 196try { 197 inputDeviceCooperate.start(sinkDeviceDescriptor, srcInputDeviceId).then(() => { 198 console.log(`Start Keyboard mouse crossing success.`); 199 }, (error: BusinessError) => { 200 console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 201 }); 202} catch (error) { 203 console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 204} 205``` 206 207## inputDeviceCooperate.stop 208 209stop(callback: AsyncCallback\<void>): void 210 211停止键鼠穿越,使用AsyncCallback异步方式返回结果。 212 213**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator 214 215**参数**: 216 217| 参数名 | 类型 | 必填 | 说明 | 218| -------- | ---------------------------- | ---- | ---------------------------- | 219| callback | AsyncCallback\<void> | 是 | 回调函数,异步返回停止键鼠穿越结果。 | 220 221**错误码**: 222 223以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 224 225| 错误码ID | 错误信息 | 226| -------- | ----------------- | 227| 401 | Parameter error. | 228 229**示例**: 230 231```ts 232import { inputDeviceCooperate } from '@kit.InputKit'; 233import { BusinessError } from '@kit.BasicServicesKit'; 234 235try { 236 inputDeviceCooperate.stop((error: BusinessError) => { 237 if (error) { 238 console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 239 return; 240 } 241 console.log(`Stop Keyboard mouse crossing success.`); 242 }); 243} catch (error) { 244 console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 245} 246``` 247 248## inputDeviceCooperate.stop 249 250stop(): Promise\<void> 251 252停止键鼠穿越,使用Promise异步方式返回结果。 253 254**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator 255 256**返回值**: 257 258| 参数名 | 说明 | 259| -------- | ---------------------------- | 260| Promise\<void> | Promise对象,异步返回停止键鼠穿越结果。 | 261 262**示例**: 263 264```ts 265import { inputDeviceCooperate } from '@kit.InputKit'; 266import { BusinessError } from '@kit.BasicServicesKit'; 267 268try { 269 inputDeviceCooperate.stop().then(() => { 270 console.log(`Stop Keyboard mouse crossing success.`); 271 }, (error: BusinessError) => { 272 console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 273 }); 274} catch (error) { 275 console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 276} 277``` 278 279## inputDeviceCooperate.getState 280 281getState(deviceDescriptor: string, callback: AsyncCallback<{ state: boolean }>): void 282 283获取键鼠穿越开关的状态,使用AsyncCallback异步方式返回结果。 284 285**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator 286 287**参数**: 288 289| 参数名 | 类型 | 必填 | 说明 | 290| -------- | --------- | ---- | ---------------------------- | 291| deviceDescriptor | string | 是 | 键鼠穿越目标设备描述符。 | 292| callback | AsyncCallback<{ state: boolean }> | 是 | 回调函数,异步返回键鼠穿越开关状态。 | 293 294**错误码**: 295 296以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 297 298| 错误码ID | 错误信息 | 299| -------- | ----------------- | 300| 401 | Parameter error. | 301 302 303**示例**: 304 305```ts 306import { inputDeviceCooperate } from '@kit.InputKit'; 307import { BusinessError } from '@kit.BasicServicesKit'; 308 309let deviceDescriptor = "descriptor"; 310try { 311 inputDeviceCooperate.getState(deviceDescriptor, (error: BusinessError, data: object) => { 312 if (error) { 313 console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 314 return; 315 } 316 console.log(`Get the status success, data: ${JSON.stringify(data)}`); 317 }); 318} catch (error) { 319 console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 320} 321``` 322 323## inputDeviceCooperate.getState 324 325getState(deviceDescriptor: string): Promise<{ state: boolean }> 326 327获取键鼠穿越开关的状态,使用Promise异步方式返回结果。 328 329**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator 330 331**参数**: 332 333| 参数名 | 类型 | 必填 | 说明 | 334| -------- | --------- | ---- | ---------------------------- | 335| deviceDescriptor | string | 是 | 键鼠穿越目标设备描述符。 | 336 337**返回值**: 338 339| 参数 | 说明 | 340| ------------------- | ------------------------------- | 341| Promise<{ state: boolean }>| Promise对象,异步返回键鼠穿越开关状态。 | 342 343**错误码**: 344 345以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 346 347| 错误码ID | 错误信息 | 348| -------- | ----------------- | 349| 401 | Parameter error. | 350 351 352**示例**: 353 354```ts 355import { inputDeviceCooperate } from '@kit.InputKit'; 356import { BusinessError } from '@kit.BasicServicesKit'; 357 358let deviceDescriptor = "descriptor"; 359try { 360 inputDeviceCooperate.getState(deviceDescriptor).then((data: object) => { 361 console.log(`Get the status success, data: ${JSON.stringify(data)}`); 362 }, (error: BusinessError) => { 363 console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 364 }); 365} catch (error) { 366 console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 367} 368``` 369 370## on('cooperation') 371 372on(type: 'cooperation', callback: AsyncCallback<{ deviceDescriptor: string, eventMsg: EventMsg }>): void 373 374注册监听键鼠穿越状态。 375 376**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator 377 378**参数**: 379 380| 参数名 | 类型 | 必填 | 说明 | 381| -------- | ---------------------------- | ---- | ---------------------------- | 382| type | string | 是 | 注册类型,取值”cooperation“。 | 383| callback | AsyncCallback<{ deviceDescriptor: string, eventMsg: [EventMsg](#eventmsg) }> | 是 | 回调函数,异步返回键鼠穿越事件。 | 384 385**错误码**: 386 387以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 388 389| 错误码ID | 错误信息 | 390| -------- | ----------------- | 391| 401 | Parameter error. | 392 393 394**示例**: 395 396```ts 397import { inputDeviceCooperate } from '@kit.InputKit'; 398 399function callback(msg: object) { 400 console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`); 401 return false; 402} 403try { 404 inputDeviceCooperate.on('cooperation', callback); 405} catch (error) { 406 console.log(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 407} 408``` 409 410## off('cooperation') 411 412off(type: 'cooperation', callback?: AsyncCallback\<void>): void 413 414关闭监听键鼠穿越状态。 415 416**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator 417 418**参数**: 419 420| 参数名 | 类型 | 必填 | 说明 | 421| -------- | ---------------------------- | ---- | ---------------------------- | 422| type | string | 是 | 注册类型,取值“cooperation”。 | 423| callback | AsyncCallback\<void> | 否 | 需要取消注册的回调函数,若无此参数,则取消当前应用注册的所有回调函数。 | 424 425**错误码**: 426 427以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 428 429| 错误码ID | 错误信息 | 430| -------- | ----------------- | 431| 401 | Parameter error. | 432 433 434**示例**: 435 436```ts 437import { inputDeviceCooperate } from '@kit.InputKit'; 438 439// 取消注册单个回调函数 440function callbackOn(msg: object) { 441 console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`); 442 return false; 443} 444function callbackOff() { 445 console.log(`Keyboard mouse crossing event`); 446 return false; 447} 448try { 449 inputDeviceCooperate.on('cooperation', callbackOn); 450 inputDeviceCooperate.off("cooperation", callbackOff); 451} catch (error) { 452 console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 453} 454``` 455```ts 456import { inputDeviceCooperate } from '@kit.InputKit'; 457 458// 取消注册所有回调函数 459function callback(msg: object) { 460 console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`); 461 return false; 462} 463try { 464 inputDeviceCooperate.on('cooperation', callback); 465 inputDeviceCooperate.off("cooperation"); 466} catch (error) { 467 console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 468} 469``` 470 471## EventMsg 472 473键鼠穿越事件。 474 475**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator 476 477| 名称 | 值 | 说明 | 478| -------- | --------- | ----------------- | 479| MSG_COOPERATE_INFO_START | 200 | 键鼠穿越消息,表示键鼠穿越开始。 | 480| MSG_COOPERATE_INFO_SUCCESS | 201 | 键鼠穿越消息,表示键鼠穿越成功。 | 481| MSG_COOPERATE_INFO_FAIL | 202 | 键鼠穿越消息,表示键鼠穿越失败。 | 482| MSG_COOPERATE_STATE_ON | 500 | 键鼠穿越状态,表示键鼠穿越状态开启。 | 483| MSG_COOPERATE_STATE_OFF | 501 | 键鼠穿越状态,表示键鼠穿越状态关闭。 | 484