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.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
66Specifies whether to enable screen hopping. This API uses a promise to return the result.
67
68
69**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
70
71**Parameters**
72
73| Name    | Type    | Mandatory | Description                                                                                |
74| --------- | ------- | ---- | -------------------------------------------------------------------                 |
75| enable    | boolean | Yes  | Whether to enable screen hopping.                  |
76
77**Return value**
78
79| Parameters                | Description                    |
80| ------------------- | ------------------------------- |
81| Promise<void>      | Promise used to return the result.       |
82
83**Error codes**
84
85For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
86
87| ID| Error Message         |
88| -------- | -----------------|
89| 401 | Parameter error.      |
90
91**Example**
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
112Starts screen hopping. This API uses an asynchronous callback to return the result.
113
114**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
115
116**Parameters**
117
118| Name               | Type                         | Mandatory | Description                           |
119| --------             | ---------------------------- | ----  | ----------------------------   |
120| sinkDeviceDescriptor | string                       |  Yes  | Descriptor of the target device for screen hopping.            |
121| srcInputDeviceId     | number                       |  Yes  | ID of the target device for screen hopping.          |
122| callback             | AsyncCallback\<void>         |  Yes   | Callback used to return the result.|
123
124**Error codes**
125
126For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Screen Hopping Error Codes](errorcode-multimodalinput.md).
127
128| ID| Error Message|
129| -------- | ---------------------------------------- |
130| 401      | Parameter error.    |
131| 4400001  | Incorrect descriptor for the target device.                |
132| 4400002  | Screen hop failed.    |
133
134**Example**
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
159Starts screen hopping. This API uses a promise to return the result.
160
161**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
162
163**Parameters**
164
165| Name               | Type                         | Mandatory | Description                           |
166| --------             | ---------------------------- | ----  | ----------------------------   |
167| sinkDeviceDescriptor | string                       |  Yes  | Descriptor of the target device for screen hopping.            |
168| srcInputDeviceId     | number                       |  Yes  | ID of the target device for screen hopping.          |
169
170
171
172**Return value**
173
174| Name                 | Description                            |
175| ---------------------- | ------------------------------- |
176| Promise\<void>         | Promise used to return the result.      |
177
178**Error codes**
179
180For details about the error codes, see [Screen Hopping Error Codes](errorcode-multimodalinput.md).
181
182| ID| Error Message|
183| -------- | ---------------------------------------- |
184| 401      | Parameter error.    |
185| 4400001  | Incorrect descriptor for the target device.          |
186| 4400002  | Screen hop failed.               |
187
188**Example**
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
211Stops screen hopping. This API uses an asynchronous callback to return the result.
212
213**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
214
215**Parameters**
216
217| Name               | Type                         | Mandatory | Description                           |
218| --------             | ---------------------------- | ----  | ----------------------------   |
219| callback             | AsyncCallback\<void>         |  Yes  | Callback used to return the result.       |
220
221**Error codes**
222
223For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
224
225| ID| Error Message          |
226| -------- | ----------------- |
227| 401      | Parameter error.  |
228
229**Example**
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
252Stops screen hopping. This API uses a promise to return the result.
253
254**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
255
256**Return value**
257
258| Name               | Description                           |
259| --------             | ----------------------------   |
260| Promise\<void>       |  Promise used to return the result.     |
261
262**Example**
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
283Checks whether screen hopping is enabled. This API uses an asynchronous callback to return the result.
284
285**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
286
287**Parameters**
288
289| Name               | Type                         | Mandatory  | Description                           |
290| --------             | ---------                    | ----  | ----------------------------    |
291| deviceDescriptor     | string                       |  Yes   | Descriptor of the target device for screen hopping.            |
292| callback             | AsyncCallback<{ state: boolean }> |  Yes   | Callback used to return the result.       |
293
294**Error codes**
295
296For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
297
298| ID| Error Message         |
299| -------- | ----------------- |
300| 401      | Parameter error.  |
301
302
303**Example**
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
327Checks whether screen hopping is enabled. This API uses a promise to return the result.
328
329**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
330
331**Parameters**
332
333| Name               | Type                         | Mandatory  | Description                           |
334| --------             | ---------                    | ----  | ----------------------------    |
335| deviceDescriptor     | string                       |  Yes   | Descriptor of the target device for screen hopping.           |
336
337**Return value**
338
339| Parameters                       | Description                    |
340| -------------------        | ------------------------------- |
341| Promise<{ state: boolean }>| Promise used to return the result.       |
342
343**Error codes**
344
345For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
346
347| ID| Error Message         |
348| -------- | ----------------- |
349| 401      | Parameter error.  |
350
351
352**Example**
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
374Enables listening for screen hopping status change events.
375
376**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
377
378**Parameters**
379
380| Name               | Type                                                            | Mandatory| Description                           |
381| --------             | ----------------------------                                    | ---- | ----------------------------   |
382| type                 | string                                                          |  Yes | Event type. The value is **cooperation**.        |
383| callback             | AsyncCallback<{ deviceDescriptor: string, eventMsg: [EventMsg](#eventmsg) }> |  Yes | Callback used to return the result.   |
384
385**Error codes**
386
387For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
388
389| ID| Error Message         |
390| -------- | ----------------- |
391| 401      | Parameter error.  |
392
393
394**Example**
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
414Disables listening for screen hopping status change events.
415
416**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
417
418**Parameters**
419
420| Name               | Type                                                             | Mandatory   | Description                          |
421| --------             | ----------------------------                                     | ----   | ----------------------------   |
422| type                 | string                                                           |  Yes   | Event type. The value is **cooperation**.        |
423| callback             | AsyncCallback\<void> |  No | Callback to be unregistered. If this parameter is not specified, all callbacks registered by the current application will be unregistered.|
424
425**Error codes**
426
427For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
428
429| ID| Error Message         |
430| -------- | ----------------- |
431| 401      | Parameter error.  |
432
433
434**Example**
435
436```ts
437import { inputDeviceCooperate } from '@kit.InputKit';
438
439// Unregister a single callback.
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// Unregister all callbacks.
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
473Enumerates screen hopping event.
474
475**System capability**: SystemCapability.MultimodalInput.Input.Cooperator
476
477| Name                      | Value       | Description                             |
478| --------                     | --------- |  -----------------               |
479| MSG_COOPERATE_INFO_START     | 200       |  Screen hopping starts.      |
480| MSG_COOPERATE_INFO_SUCCESS   | 201       |  Screen hopping succeeds.     |
481| MSG_COOPERATE_INFO_FAIL      | 202       |  Screen hopping fails.     |
482| MSG_COOPERATE_STATE_ON       | 500       |  Screen hopping is enabled.  |
483| MSG_COOPERATE_STATE_OFF      | 501       |  Screen hopping is disabled.  |
484