• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimodalInput.inputDeviceCooperate (Screen Hopping) (System API)
2
3The **inputDeviceCooperate** module implements screen hopping for two or more networked devices to share the keyboard and mouse for collaborative operations.
4
5> **NOTE**
6>
7>- The APIs provided by this module are no longer maintained since API Version 10. You are advised to use [@ohos.cooperate (Screen Hopping)](../apis-distributedservice-kit/js-apis-devicestatus-cooperate-sys.md).
8>
9>- The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
10>
11>- The APIs provided by this module are system APIs.
12
13## Modules to Import
14
15```ts
16import { inputDeviceCooperate } from '@kit.InputKit';
17```
18
19## inputDeviceCooperate.enable
20
21enable(enable: boolean, callback: AsyncCallback<void>): void
22
23Specifies whether to enable screen hopping. This API uses an asynchronous callback to return the result.
24
25**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
26
27**Parameters**
28
29| Name   | Type     | Mandatory | Description   |
30| -------- | ------------------------- | ---- | --------------------------- |
31| enable   | boolean                   | Yes  | Whether to enable screen hopping.|
32| callback | AsyncCallback<void>  | Yes |Callback used to return the result.  |
33
34**Error codes**
35
36For details about the following error codes, see [Screen Hopping Error Codes](../apis-distributedservice-kit/errorcode-devicestatus.md).
37
38| ID| Error Message         |
39| -------- | -----------------|
40| 401 | Parameter error.      |
41
42
43**Example**
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.error(`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.error(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
59}
60```
61
62## inputDeviceCooperate.enable
63
64enable(enable: boolean): Promise<void>
65
66Specifies whether to enable screen hopping. This API uses a promise to return the result.
67
68**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
69
70**Parameters**
71
72| Name    | Type    | Mandatory | Description                                                                                |
73| --------- | ------- | ---- | -------------------------------------------------------------------                 |
74| enable    | boolean | Yes  | Whether to enable screen hopping.                  |
75
76**Return value**
77
78| Parameters                | Description                    |
79| ------------------- | ------------------------------- |
80| Promise<void>      | Promise used to return the result.       |
81
82**Error codes**
83
84For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
85
86| ID| Error Message         |
87| -------- | -----------------|
88| 401 | Parameter error.      |
89
90**Example**
91
92```ts
93import { inputDeviceCooperate } from '@kit.InputKit';
94import { BusinessError } from '@kit.BasicServicesKit';
95
96try {
97  inputDeviceCooperate.enable(true).then(() => {
98    console.log(`Keyboard mouse crossing enable success.`);
99  }, (error: BusinessError) => {
100    console.error(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
101  });
102} catch (error) {
103  console.error(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
104}
105```
106
107## inputDeviceCooperate.start
108
109start(sinkDeviceDescriptor: string, srcInputDeviceId: number, callback: AsyncCallback\<void>): void
110
111Starts screen hopping. This API uses an asynchronous callback to return the result.
112
113**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
114
115**Parameters**
116
117| Name               | Type                         | Mandatory | Description                           |
118| --------             | ---------------------------- | ----  | ----------------------------   |
119| sinkDeviceDescriptor | string                       |  Yes  | Descriptor of the target device for screen hopping.            |
120| srcInputDeviceId     | number                       |  Yes  | ID of the target device for screen hopping.          |
121| callback             | AsyncCallback\<void>         |  Yes   | Callback used to return the result.|
122
123**Error codes**
124
125For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Screen Hopping Error Codes](errorcode-cooperator.md).
126
127| ID| Error Message|
128| -------- | ---------------------------------------- |
129| 401      | Parameter error.    |
130| 4400001  | Incorrect descriptor for the target device.                |
131| 4400002  | Screen hop failed.   |
132
133**Example**
134
135```ts
136import { inputDeviceCooperate } from '@kit.InputKit';
137import { BusinessError } from '@kit.BasicServicesKit';
138
139let sinkDeviceDescriptor = "descriptor";
140let srcInputDeviceId = 0;
141try {
142  inputDeviceCooperate.start(sinkDeviceDescriptor, srcInputDeviceId, (error: BusinessError) => {
143    if (error) {
144      console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
145      return;
146    }
147    console.log(`Start Keyboard mouse crossing success.`);
148  });
149} catch (error) {
150  console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
151}
152```
153
154## inputDeviceCooperate.start
155
156start(sinkDeviceDescriptor: string, srcInputDeviceId: number): Promise\<void>
157
158Starts screen hopping. This API uses a promise to return the result.
159
160**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
161
162**Parameters**
163
164| Name               | Type                         | Mandatory | Description                           |
165| --------             | ---------------------------- | ----  | ----------------------------   |
166| sinkDeviceDescriptor | string                       |  Yes  | Descriptor of the target device for screen hopping.            |
167| srcInputDeviceId     | number                       |  Yes  | ID of the target device for screen hopping.          |
168
169
170
171**Return value**
172
173| Name                 | Description                            |
174| ---------------------- | ------------------------------- |
175| Promise\<void>         | Promise used to return the result.      |
176
177**Error codes**
178
179For details about the error codes, see [Screen Hopping Error Codes](errorcode-cooperator.md).
180
181| ID| Error Message|
182| -------- | ---------------------------------------- |
183| 401      | Parameter error.    |
184| 4400001  | Incorrect descriptor for the target device.          |
185| 4400002  | Screen hop failed.              |
186
187**Example**
188
189```ts
190import { inputDeviceCooperate } from '@kit.InputKit';
191import { BusinessError } from '@kit.BasicServicesKit';
192
193let sinkDeviceDescriptor = "descriptor";
194let srcInputDeviceId = 0;
195try {
196  inputDeviceCooperate.start(sinkDeviceDescriptor, srcInputDeviceId).then(() => {
197    console.log(`Start Keyboard mouse crossing success.`);
198  }, (error: BusinessError) => {
199    console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
200  });
201} catch (error) {
202  console.error(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
203}
204```
205
206## inputDeviceCooperate.stop
207
208stop(callback: AsyncCallback\<void>): void
209
210Stops screen hopping. This API uses an asynchronous callback to return the result.
211
212**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
213
214**Parameters**
215
216| Name               | Type                         | Mandatory | Description                           |
217| --------             | ---------------------------- | ----  | ----------------------------   |
218| callback             | AsyncCallback\<void>         |  Yes  | Callback used to return the result.       |
219
220**Error codes**
221
222For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
223
224| ID| Error Message          |
225| -------- | ----------------- |
226| 401      | Parameter error.  |
227
228**Example**
229
230```ts
231import { inputDeviceCooperate } from '@kit.InputKit';
232import { BusinessError } from '@kit.BasicServicesKit';
233
234try {
235  inputDeviceCooperate.stop((error: BusinessError) => {
236    if (error) {
237      console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
238      return;
239    }
240    console.log(`Stop Keyboard mouse crossing success.`);
241  });
242} catch (error) {
243  console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
244}
245```
246
247## inputDeviceCooperate.stop
248
249stop(): Promise\<void>
250
251Stops screen hopping. This API uses a promise to return the result.
252
253**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
254
255**Return value**
256
257| Name               | Description                           |
258| --------             | ----------------------------   |
259| Promise\<void>       |  Promise used to return the result.     |
260
261**Example**
262
263```ts
264import { inputDeviceCooperate } from '@kit.InputKit';
265import { BusinessError } from '@kit.BasicServicesKit';
266
267try {
268  inputDeviceCooperate.stop().then(() => {
269    console.log(`Stop Keyboard mouse crossing success.`);
270  }, (error: BusinessError) => {
271    console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
272  });
273} catch (error) {
274  console.error(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
275}
276```
277
278## inputDeviceCooperate.getState
279
280getState(deviceDescriptor: string, callback: AsyncCallback<{ state: boolean }>): void
281
282Checks whether screen hopping is enabled. This API uses an asynchronous callback to return the result.
283
284**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
285
286**Parameters**
287
288| Name               | Type                         | Mandatory  | Description                           |
289| --------             | ---------                    | ----  | ----------------------------    |
290| deviceDescriptor     | string                       |  Yes   | Descriptor of the target device for screen hopping.            |
291| callback             | AsyncCallback<{ state: boolean }> |  Yes   | Callback used to return the result.       |
292
293**Error codes**
294
295For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
296
297| ID| Error Message         |
298| -------- | ----------------- |
299| 401      | Parameter error.  |
300
301
302**Example**
303
304```ts
305import { inputDeviceCooperate } from '@kit.InputKit';
306import { BusinessError } from '@kit.BasicServicesKit';
307
308let deviceDescriptor = "descriptor";
309try {
310  inputDeviceCooperate.getState(deviceDescriptor, (error: BusinessError, data: object) => {
311    if (error) {
312      console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
313      return;
314    }
315    console.log(`Get the status success, data: ${JSON.stringify(data)}`);
316  });
317} catch (error) {
318  console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
319}
320```
321
322## inputDeviceCooperate.getState
323
324getState(deviceDescriptor: string): Promise<{ state: boolean }>
325
326Checks whether screen hopping is enabled. This API uses a promise to return the result.
327
328**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
329
330**Parameters**
331
332| Name               | Type                         | Mandatory  | Description                           |
333| --------             | ---------                    | ----  | ----------------------------    |
334| deviceDescriptor     | string                       |  Yes   | Descriptor of the target device for screen hopping.           |
335
336**Return value**
337
338| Parameters                       | Description                    |
339| -------------------        | ------------------------------- |
340| Promise<{ state: boolean }>| Promise used to return the result. The value **true** indicates that screen hopping is enabled, and the **false** indicates the opposite.      |
341
342**Error codes**
343
344For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
345
346| ID| Error Message         |
347| -------- | ----------------- |
348| 401      | Parameter error.  |
349
350
351**Example**
352
353```ts
354import { inputDeviceCooperate } from '@kit.InputKit';
355import { BusinessError } from '@kit.BasicServicesKit';
356
357let deviceDescriptor = "descriptor";
358try {
359  inputDeviceCooperate.getState(deviceDescriptor).then((data: object) => {
360    console.log(`Get the status success, data: ${JSON.stringify(data)}`);
361  }, (error: BusinessError) => {
362    console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
363  });
364} catch (error) {
365  console.error(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
366}
367```
368
369## on('cooperation')
370
371on(type: 'cooperation', callback: AsyncCallback<{ deviceDescriptor: string, eventMsg: EventMsg }>): void
372
373Enables listening for screen hopping status change events.
374
375**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
376
377**Parameters**
378
379| Name               | Type                                                            | Mandatory| Description                           |
380| --------             | ----------------------------                                    | ---- | ----------------------------   |
381| type                 | string                                                          |  Yes | Event type. The value is **cooperation**.        |
382| callback             | AsyncCallback<{ deviceDescriptor: string, eventMsg: [EventMsg](#eventmsg) }> |  Yes | Callback used to return the result.   |
383
384**Error codes**
385
386For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
387
388| ID| Error Message         |
389| -------- | ----------------- |
390| 401      | Parameter error.  |
391
392
393**Example**
394
395```ts
396import { inputDeviceCooperate } from '@kit.InputKit';
397
398let callback = (msg: object) => {
399  console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`);
400  return false;
401}
402try {
403  inputDeviceCooperate.on('cooperation', callback);
404} catch (error) {
405  console.error(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
406}
407```
408
409## off('cooperation')
410
411off(type: 'cooperation', callback?: AsyncCallback\<void>): void
412
413Disables listening for screen hopping status change events.
414
415**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
416
417**Parameters**
418
419| Name               | Type                                                             | Mandatory   | Description                          |
420| --------             | ----------------------------                                     | ----   | ----------------------------   |
421| type                 | string                                                           |  Yes   | Event type. The value is **cooperation**.        |
422| callback             | AsyncCallback\<void> |  No | Callback to be unregistered. If this parameter is not specified, all callbacks registered by the current application will be unregistered.|
423
424**Error codes**
425
426For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
427
428| ID| Error Message         |
429| -------- | ----------------- |
430| 401      | Parameter error.  |
431
432
433**Example**
434
435```ts
436import { inputDeviceCooperate } from '@kit.InputKit';
437
438// Unregister a single callback.
439let callbackOn = (msg: object) => {
440  console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`);
441  return false;
442}
443let callbackOff = () => {
444  console.log(`Keyboard mouse crossing event`);
445  return false;
446}
447try {
448  inputDeviceCooperate.on('cooperation', callbackOn);
449  inputDeviceCooperate.off("cooperation", callbackOff);
450} catch (error) {
451  console.error(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
452}
453```
454```ts
455import { inputDeviceCooperate } from '@kit.InputKit';
456
457// Unregister all callbacks.
458let callback = (msg: object) => {
459  console.log(`Keyboard mouse crossing event: ${JSON.stringify(msg)}`);
460  return false;
461}
462try {
463  inputDeviceCooperate.on('cooperation', callback);
464  inputDeviceCooperate.off("cooperation");
465} catch (error) {
466  console.error(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
467}
468```
469
470## EventMsg
471
472Enumerates screen hopping event.
473
474**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
475
476| Name                      | Value       | Description                             |
477| --------                     | --------- |  -----------------               |
478| MSG_COOPERATE_INFO_START     | 200       |  Screen hopping starts.      |
479| MSG_COOPERATE_INFO_SUCCESS   | 201       |  Screen hopping succeeds.     |
480| MSG_COOPERATE_INFO_FAIL      | 202       |  Screen hopping fails.     |
481| MSG_COOPERATE_STATE_ON       | 500       |  Screen hopping is enabled.  |
482| MSG_COOPERATE_STATE_OFF      | 501       |  Screen hopping is disabled.  |
483