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