• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimodalInput.inputDeviceCooperate (键鼠穿越)
2
3键鼠穿越功能模块,提供两台或多台设备组网协同后键鼠共享能力,实现键鼠输入设备的跨设备协同操作。
4
5> **说明**
6>
7>- 从API Version 10开始,该接口不再维护,推荐使用新接口[@ohos.cooperate (键鼠穿越)](js-apis-devicestatus-cooperate.md)。
8>
9>- 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10>
11>- 本模块接口均为系统接口。
12
13## 导入模块
14
15```ts
16import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
17```
18
19## inputDeviceCooperate.enable
20
21enable(enable: boolean, callback: AsyncCallback<void>): void
22
23开启、关闭键鼠穿越,使用AsyncCallback异步方式返回结果。
24
25**系统能力**: SystemCapability.MultimodalInput.Input.Cooperator
26
27**参数**:
28
29| 参数名    | 类型      | 必填  | 说明    |
30| -------- | ------------------------- | ---- | --------------------------- |
31| enable   | boolean                   | 是   | 键鼠穿越使能状态。 |
32| callback | AsyncCallback<void>  | 是  |回调函数,异步返回键鼠穿越开启、关闭结果。   |
33
34
35
36**示例**:
37
38```ts
39import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
40import { BusinessError } from '@ohos.base'
41
42try {
43  inputDeviceCooperate.enable(true, (error: BusinessError) => {
44    if (error) {
45      console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
46      return;
47    }
48    console.log(`Keyboard mouse crossing enable success.`);
49  });
50} catch (error) {
51  console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
52}
53```
54
55## inputDeviceCooperate.enable
56
57enable(enable: boolean): Promise<void>
58
59开启、关闭键鼠穿越,使用Promise异步方式返回结果。
60
61
62**系统能力**: SystemCapability.MultimodalInput.Input.Cooperator
63
64**参数**:
65
66| 参数名     | 类型     | 必填  | 说明                                                                                 |
67| --------- | ------- | ---- | -------------------------------------------------------------------                 |
68| enable    | boolean | 是   | 键鼠穿越使能状态。                   |
69
70
71
72**返回值**:
73
74| 参数                 | 说明                     |
75| ------------------- | ------------------------------- |
76| Promise<void>      | Promise对象,异步返回键鼠穿越开启、关闭结果。        |
77
78
79
80**示例**:
81
82```ts
83import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
84import { BusinessError } from '@ohos.base'
85
86try {
87  inputDeviceCooperate.enable(true).then(() => {
88    console.log(`Keyboard mouse crossing enable success.`);
89  }, (error: BusinessError) => {
90    console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
91  });
92} catch (error) {
93  console.log(`Keyboard mouse crossing enable failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
94}
95```
96
97## inputDeviceCooperate.start
98
99start(sinkDeviceDescriptor: string, srcInputDeviceId: number, callback: AsyncCallback\<void>): void
100
101启动键鼠穿越,使用AsyncCallback异步方式返回结果。
102
103**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator
104
105**参数**:
106
107| 参数名                | 类型                          | 必填  | 说明                            |
108| --------             | ---------------------------- | ----  | ----------------------------   |
109| sinkDeviceDescriptor | string                       |  是   | 键鼠穿越目标设备描述符。             |
110| srcInputDeviceId     | number                       |  是   | 键鼠穿越待穿越外设标识符。           |
111| callback             | AsyncCallback\<void>         |  是    | 回调函数,异步返回键鼠穿越启动、停止状态。|
112
113**错误码:**
114
115以下错误码的详细介绍请参见[ohos.multimodalinput错误码](../errorcodes/errorcode-multimodalinput.md)。
116
117| 错误码ID | 错误信息 |
118| -------- | ---------------------------------------- |
119| 4400001  | Incorrect descriptor for the target device.     |
120| 4400002  | Screen hop failed.                |
121
122**示例**:
123
124```ts
125import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
126import { BusinessError } from '@ohos.base'
127
128let sinkDeviceDescriptor = "descriptor";
129let srcInputDeviceId = 0;
130try {
131  inputDeviceCooperate.start(sinkDeviceDescriptor, srcInputDeviceId, (error: BusinessError) => {
132    if (error) {
133      console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
134      return;
135    }
136    console.log(`Start Keyboard mouse crossing success.`);
137  });
138} catch (error) {
139  console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
140}
141```
142
143## inputDeviceCooperate.start
144
145start(sinkDeviceDescriptor: string, srcInputDeviceId: number): Promise\<void>
146
147启动键鼠穿越,使用Promise异步方式返回结果。
148
149**系统能力**: SystemCapability.MultimodalInput.Input.Cooperator
150
151**参数**:
152
153| 参数名                | 类型                          | 必填  | 说明                            |
154| --------             | ---------------------------- | ----  | ----------------------------   |
155| sinkDeviceDescriptor | string                       |  是   | 键鼠穿越目标设备描述符。             |
156| srcInputDeviceId     | number                       |  是   | 键鼠穿越待穿越外设标识符。           |
157
158
159
160**返回值**:
161
162| 参数名                  | 说明                             |
163| ---------------------- | ------------------------------- |
164| Promise\<void>         | Promise对象,异步返回键鼠穿越启动、关闭结果。       |
165
166**错误码:**
167
168以下错误码的详细介绍请参见[ohos.multimodalinput错误码](../errorcodes/errorcode-multimodalinput.md)。
169
170| 错误码ID | 错误信息 |
171| -------- | ---------------------------------------- |
172| 4400001  | Incorrect descriptor for the target device.                 |
173| 4400002  | Screen hop failed.            |
174
175**示例**:
176
177```ts
178import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
179import { BusinessError } from '@ohos.base'
180
181let sinkDeviceDescriptor = "descriptor";
182let srcInputDeviceId = 0;
183try {
184  inputDeviceCooperate.start(sinkDeviceDescriptor, srcInputDeviceId).then(() => {
185    console.log(`Start Keyboard mouse crossing success.`);
186  }, (error: BusinessError) => {
187    console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
188  });
189} catch (error) {
190  console.log(`Start Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
191}
192```
193
194## inputDeviceCooperate.stop
195
196stop(callback: AsyncCallback\<void>): void
197
198停止键鼠穿越,使用AsyncCallback异步方式返回结果。
199
200**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator
201
202**参数**:
203
204| 参数名                | 类型                          | 必填  | 说明                            |
205| --------             | ---------------------------- | ----  | ----------------------------   |
206| callback             | AsyncCallback\<void>         |  是   | 回调函数,异步返回停止键鼠穿越结果。        |
207
208
209
210**示例**:
211
212```ts
213import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
214import { BusinessError } from '@ohos.base'
215
216try {
217  inputDeviceCooperate.stop((error: BusinessError) => {
218    if (error) {
219      console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
220      return;
221    }
222    console.log(`Stop Keyboard mouse crossing success.`);
223  });
224} catch (error) {
225  console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
226}
227```
228
229## inputDeviceCooperate.stop
230
231stop(): Promise\<void>
232
233停止键鼠穿越,使用Promise异步方式返回结果。
234
235**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator
236
237**返回值**:
238
239| 参数名                | 说明                            |
240| --------             | ----------------------------   |
241| Promise\<void>       |  Promise对象,异步返回停止键鼠穿越结果。      |
242
243**示例**:
244
245```ts
246import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
247import { BusinessError } from '@ohos.base'
248
249try {
250  inputDeviceCooperate.stop().then(() => {
251    console.log(`Stop Keyboard mouse crossing success.`);
252  }, (error: BusinessError) => {
253    console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
254  });
255} catch (error) {
256  console.log(`Stop Keyboard mouse crossing failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
257}
258```
259
260## inputDeviceCooperate.getState
261
262getState(deviceDescriptor: string, callback: AsyncCallback<{ state: boolean }>): void
263
264获取键鼠穿越开关的状态,使用AsyncCallback异步方式返回结果。
265
266**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator
267
268**参数**:
269
270| 参数名                | 类型                          | 必填   | 说明                            |
271| --------             | ---------                    | ----  | ----------------------------    |
272| deviceDescriptor     | string                       |  是    | 键鼠穿越目标设备描述符。             |
273| callback             | AsyncCallback<{ state: boolean }> |  是    | 回调函数,异步返回键鼠穿越开关状态。        |
274
275**示例**:
276
277```ts
278import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
279import { BusinessError } from '@ohos.base'
280
281let deviceDescriptor = "descriptor";
282try {
283  inputDeviceCooperate.getState(deviceDescriptor, (error: BusinessError, data: boolean) => {
284    if (error) {
285      console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
286      return;
287    }
288    console.log(`Get the status success, data: ${JSON.stringify(data)}`);
289  });
290} catch (error) {
291  console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
292}
293```
294
295## inputDeviceCooperate.getState
296
297getState(deviceDescriptor: string): Promise<{ state: boolean }>
298
299获取键鼠穿越开关的状态,使用Promise异步方式返回结果。
300
301**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator
302
303**参数**:
304
305| 参数名                | 类型                          | 必填   | 说明                            |
306| --------             | ---------                    | ----  | ----------------------------    |
307| deviceDescriptor     | string                       |  是    | 键鼠穿越目标设备描述符。            |
308
309
310
311**返回值**:
312
313| 参数                        | 说明                     |
314| -------------------        | ------------------------------- |
315| Promise<{ state: boolean }>| Promise对象,异步返回键鼠穿越开关状态。        |
316
317
318
319**示例**:
320
321```ts
322import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
323import { BusinessError } from '@ohos.base'
324
325let deviceDescriptor = "descriptor";
326try {
327  inputDeviceCooperate.getState(deviceDescriptor).then((data: boolean) => {
328    console.log(`Get the status success, data: ${JSON.stringify(data)}`);
329  }, (error: BusinessError) => {
330    console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
331  });
332} catch (error) {
333  console.log(`Get the status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
334}
335```
336
337## on('cooperation')
338
339on(type: 'cooperation', callback: AsyncCallback<{ deviceDescriptor: string, eventMsg: EventMsg }>): void
340
341注册监听键鼠穿越状态。
342
343**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator
344
345**参数**:
346
347| 参数名                | 类型                                                             | 必填 | 说明                            |
348| --------             | ----------------------------                                    | ---- | ----------------------------   |
349| type                 | string                                                          |  是  | 注册类型,取值”cooperation“。         |
350| callback             | AsyncCallback<{ deviceDescriptor: string, eventMsg: [EventMsg](#eventmsg) }> |  是  | 回调函数,异步返回键鼠穿越事件。    |
351
352
353
354**示例**:
355
356```ts
357import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
358
359function callback(deviceDescriptor: string, eventMsg: inputDeviceCooperate.EventMsg) {
360  console.log(`Keyboard mouse crossing event: ${JSON.stringify(deviceDescriptor)}`);
361  return false;
362}
363try {
364  inputDeviceCooperate.on('cooperation', callback);
365} catch (error) {
366  console.log(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
367}
368```
369
370## off('cooperation')
371
372off(type: 'cooperation', callback?: AsyncCallback\<void>): void
373
374关闭监听键鼠穿越状态。
375
376**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator
377
378**参数**:
379
380| 参数名                | 类型                                                              | 必填    | 说明                           |
381| --------             | ----------------------------                                     | ----   | ----------------------------   |
382| type                 | string                                                           |  是    | 注册类型,取值“cooperation”。         |
383| callback             | AsyncCallback\<void> |  否  | 需要取消注册的回调函数,若无此参数,则取消当前应用注册的所有回调函数。 |
384
385
386
387**示例**:
388
389```ts
390import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
391
392// 取消注册单个回调函数
393function callbackOn(deviceDescriptor: string, eventMsg: inputDeviceCooperate.EventMsg) {
394  console.log(`Keyboard mouse crossing event: ${JSON.stringify(deviceDescriptor)}`);
395  return false;
396}
397function callbackOff() {
398  console.log(`Keyboard mouse crossing event`);
399  return false;
400}
401try {
402  inputDeviceCooperate.on('cooperation', callbackOn);
403  inputDeviceCooperate.off("cooperation", callbackOff);
404} catch (error) {
405  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
406}
407```
408```ts
409import inputDeviceCooperate from '@ohos.multimodalInput.inputDeviceCooperate'
410
411// 取消注册所有回调函数
412function callback(deviceDescriptor: string, eventMsg: inputDeviceCooperate.EventMsg) {
413  console.log(`Keyboard mouse crossing event: ${JSON.stringify(deviceDescriptor)}`);
414  return false;
415}
416try {
417  inputDeviceCooperate.on('cooperation', callback);
418  inputDeviceCooperate.off("cooperation");
419} catch (error) {
420  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
421}
422```
423
424## EventMsg
425
426键鼠穿越事件。
427
428**系统能力**:SystemCapability.MultimodalInput.Input.Cooperator
429
430| 名称                       | 值        | 说明                              |
431| --------                     | --------- |  -----------------               |
432| MSG_COOPERATE_INFO_START     | 200       |  键鼠穿越消息,表示键鼠穿越开始。       |
433| MSG_COOPERATE_INFO_SUCCESS   | 201       |  键鼠穿越消息,表示键鼠穿越成功。      |
434| MSG_COOPERATE_INFO_FAIL      | 202       |  键鼠穿越消息,表示键鼠穿越失败。      |
435| MSG_COOPERATE_STATE_ON       | 500       |  键鼠穿越状态,表示键鼠穿越状态开启。   |
436| MSG_COOPERATE_STATE_OFF      | 501       |  键鼠穿越状态,表示键鼠穿越状态关闭。   |
437