• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 '../../utils/Log';
17
18const TAG: string = 'common_MultimodalInputManager';
19
20export class MultimodalInputManager {
21  public static readonly KEY_CODE_KEYBOARD_LEFT = 2014;
22  public static readonly KEY_CODE_KEYBOARD_RIGHT = 2015;
23  public static readonly KEY_CODE_KEYBOARD_ESC = 2070;
24  public static readonly KEY_CODE_KEYBOARD_ENTER = 2054;
25
26  //win + N
27  leftKeyOptions: any = {
28    'preKeys': [],
29    'finalKey': 2014,
30    'isFinalKeyDown': true,
31    'finalKeyDownDuration': 0
32  };
33
34  //win + I
35  rightKeyOptions: any = {
36    'preKeys': [],
37    'finalKey': 2015,
38    'isFinalKeyDown': true,
39    'finalKeyDownDuration': 0
40  };
41  escKeyOptions: any = {
42    'preKeys': [],
43    'finalKey': 2070,
44    'isFinalKeyDown': true,
45    'finalKeyDownDuration': 0
46  };
47
48  leftKeyCallback;
49  rightKeyCallback;
50  escKeyCallback;
51
52  async registerListener(callback) {
53    Log.debug(TAG, `registerListener start`);
54    inputConsumer.on('key', this.leftKeyOptions, this.leftKeyCallback = (data) => {
55      Log.debug(TAG, `notificationRegister data: ${JSON.stringify(data)}`);
56      callback(0);
57    });
58    inputConsumer.on('key', this.rightKeyOptions, this.rightKeyCallback = (data) => {
59      Log.debug(TAG, `controlRegister data: ${JSON.stringify(data)}`);
60      callback(1);
61    });
62    inputConsumer.on('key', this.escKeyOptions, this.escKeyCallback = (data) => {
63      Log.debug(TAG, `escRegister data: ${JSON.stringify(data)}`);
64      callback(2);
65    });
66    Log.debug(TAG, `registerListener end`);
67  }
68
69  async unregisterListener() {
70    Log.debug(TAG, `unregisterListener start`);
71    inputConsumer.off('key', this.leftKeyOptions, this.leftKeyCallback);
72    this.leftKeyCallback = undefined;
73    inputConsumer.off('key', this.rightKeyOptions, this.rightKeyCallback);
74    this.rightKeyCallback = undefined;
75    inputConsumer.off('key', this.escKeyOptions, this.escKeyCallback);
76    this.escKeyCallback = undefined;
77    Log.debug(TAG, `unregisterListener end`);
78  }
79}
80
81export const mMultimodalInputManager = new MultimodalInputManager();