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