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  */
15 
16 #include "keyboard_event.h"
17 
18 #include <global.h>
19 #include <memory>
20 
21 #include "global.h"
22 #include "input_event_callback.h"
23 #include "key_event.h"
24 
25 namespace OHOS {
26 namespace MiscServices {
27 using namespace MMI;
GetInstance()28 KeyboardEvent &KeyboardEvent::GetInstance()
29 {
30     static KeyboardEvent keyboardEvent;
31     return keyboardEvent;
32 }
33 
AddKeyEventMonitor(KeyHandle handle)34 int32_t KeyboardEvent::AddKeyEventMonitor(KeyHandle handle)
35 {
36     IMSA_HILOGI("KeyboardEvent::AddKeyEventMonitor start.");
37     std::shared_ptr<InputEventCallback> callback = std::make_shared<InputEventCallback>();
38     callback->SetKeyHandle(handle);
39     int32_t monitorId = InputManager::GetInstance()->AddMonitor([callback](std::shared_ptr<MMI::KeyEvent> keyEvent) {
40         if (callback == nullptr) {
41             IMSA_HILOGE("callback is nullptr!");
42             return;
43         }
44         callback->OnInputEvent(keyEvent);
45     });
46     if (monitorId < 0) {
47         IMSA_HILOGE("add monitor failed, id: %{public}d!", monitorId);
48         return ErrorCode::ERROR_SUBSCRIBE_KEYBOARD_EVENT;
49     }
50     IMSA_HILOGD("add monitor success, id: %{public}d.", monitorId);
51 
52     CombinationKeyCallBack combinationKeyCallBack = [callback](std::shared_ptr<MMI::KeyEvent> keyEvent) {
53         if (callback == nullptr) {
54             IMSA_HILOGE("callback is nullptr!");
55             return;
56         }
57         callback->TriggerSwitch();
58     };
59     SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_META_LEFT, MMI::KeyEvent::KEYCODE_SPACE, combinationKeyCallBack);
60     SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_META_RIGHT, MMI::KeyEvent::KEYCODE_SPACE, combinationKeyCallBack);
61     return ErrorCode::NO_ERROR;
62 }
63 
SubscribeCombinationKey(int32_t preKey, int32_t finalKey, CombinationKeyCallBack callback)64 void KeyboardEvent::SubscribeCombinationKey(int32_t preKey, int32_t finalKey, CombinationKeyCallBack callback)
65 {
66     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
67     std::set<int32_t> preKeys;
68     preKeys.insert(preKey);
69     keyOption->SetPreKeys(preKeys);
70     keyOption->SetFinalKey(finalKey);
71     keyOption->SetFinalKeyDown(true);
72     // 0 means press delay 0 ms
73     keyOption->SetFinalKeyDownDuration(0);
74     int32_t subscribeId = InputManager::GetInstance()->SubscribeKeyEvent(keyOption, callback);
75     if (subscribeId < 0) {
76         IMSA_HILOGE("failed to SubscribeKeyEvent, id: %{public}d preKey: %{public}d.", subscribeId, preKey);
77     }
78 }
79 } // namespace MiscServices
80 } // namespace OHOS