1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import inputConsumer from "@ohos.multimodalInput.inputConsumer";
16import Log from "./Log";
17import createOrGet from "./SingleInstanceHelper";
18
19export type MultiCallback = (keyOptions: inputConsumer.KeyOptions) => void;
20export enum MultiKeyCode {
21  WIN = 2076,
22  N = 2030,
23  I = 2025,
24}
25
26const TAG = "MultimodalInputManager";
27
28class MultimodalInputManager {
29  //win + N
30  notificationKeyOptions: any = {
31    preKeys: [2076],
32    finalKey: 2030,
33    isFinalKeyDown: true,
34    finalKeyDownDuration: 0,
35  };
36  //win + I
37  controlKeyOptions: any = {
38    preKeys: [2076],
39    finalKey: 2025,
40    isFinalKeyDown: true,
41    finalKeyDownDuration: 0,
42  };
43
44  subscribeCombinationKey(keys: MultiKeyCode[], cb: MultiCallback): () => void {
45    if (keys.length <= 1) {
46      Log.showError(TAG, `Invalid keys, can't subscribe.`);
47      return () => {};
48    }
49    let keyOptions = {
50      preKeys: keys.slice(0, keys.length - 1),
51      finalKey: keys[keys.length - 1],
52      isFinalKeyDown: true,
53      finalKeyDownDuration: 0,
54    };
55    inputConsumer.on("key", keyOptions, (options) => {
56      Log.showInfo(TAG, `on CombinationKey, options:${JSON.stringify(options)}`);
57      cb(options);
58    });
59    Log.showInfo(TAG, `subscribe CombinationKey, keys:${JSON.stringify(keys)}`);
60    return () => {
61      inputConsumer.off("key", keyOptions, (data) => {});
62    };
63  }
64
65  registerControlListener(callback) {
66    Log.showDebug(TAG, `registerListener control`);
67    inputConsumer.on("key", this.controlKeyOptions, (data) => {
68      Log.showInfo(TAG, `controlRegisterCallBack data: ${JSON.stringify(data)}`);
69      callback.onControlShowOrHide(data);
70    });
71    Log.showDebug(TAG, `registerListener end`);
72  }
73
74  registerNotificationListener(callback) {
75    Log.showDebug(TAG, `registerListener notification`);
76    inputConsumer.on("key", this.notificationKeyOptions, ( data) => {
77      Log.showInfo(TAG, `notificationRegisterCallBack data: ${JSON.stringify(data)}`);
78      callback.onNotificationShowOrHide(data);
79    });
80    Log.showDebug(TAG, `registerListener end`);
81  }
82
83  unregisterListener() {
84    Log.showDebug(TAG, `unregisterListener start`);
85    inputConsumer.off("key", this.notificationKeyOptions, (data) => {
86      Log.showInfo(TAG, `notificationUnregisterCallBack data: ${JSON.stringify(data)}`);
87    });
88    inputConsumer.off("key", this.controlKeyOptions, (data) => {
89      Log.showInfo(TAG, `controlUnregisterCallBack data: ${JSON.stringify(data)}`);
90    });
91    Log.showDebug(TAG, `unregisterListener end`);
92  }
93}
94let sMultimodalInputManager = createOrGet(MultimodalInputManager, TAG);
95
96export default sMultimodalInputManager as MultimodalInputManager;
97