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