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 "input_event_callback.h"
17 #include "inputmethod_sysevent.h"
18
19 namespace OHOS {
20 namespace MiscServices {
21 using namespace MMI;
GetInstance()22 KeyboardEvent &KeyboardEvent::GetInstance()
23 {
24 static KeyboardEvent keyboardEvent;
25 return keyboardEvent;
26 }
27
AddKeyEventMonitor(KeyHandle handle)28 int32_t KeyboardEvent::AddKeyEventMonitor(KeyHandle handle)
29 {
30 IMSA_HILOGI("KeyboardEvent::AddKeyEventMonitor start.");
31 std::shared_ptr<InputEventCallback> callback = std::make_shared<InputEventCallback>();
32 callback->SetKeyHandle(handle);
33 auto manager = InputManager::GetInstance();
34 if (manager == nullptr) {
35 return ErrorCode::ERROR_NULL_POINTER;
36 }
37 int32_t monitorId = manager->AddMonitor([callback](std::shared_ptr<MMI::KeyEvent> keyEvent) {
38 if (callback == nullptr) {
39 IMSA_HILOGE("callback is nullptr!");
40 return;
41 }
42 callback->OnInputEvent(keyEvent);
43 });
44 if (monitorId < 0) {
45 IMSA_HILOGE("add monitor failed, id: %{public}d!", monitorId);
46 return ErrorCode::ERROR_SUBSCRIBE_KEYBOARD_EVENT;
47 }
48 IMSA_HILOGD("add monitor success, id: %{public}d.", monitorId);
49
50 CombinationKeyCallBack combinationKeyCallBack = [callback](std::shared_ptr<MMI::KeyEvent> keyEvent) {
51 InputMethodSysEvent::GetInstance().ReportSystemShortCut("usual.event.WIN_SPACE");
52 if (callback == nullptr) {
53 IMSA_HILOGE("callback is nullptr!");
54 return;
55 }
56 callback->TriggerSwitch();
57 };
58 SubscribeCombinationKey(
59 MMI::KeyEvent::KEYCODE_META_LEFT, MMI::KeyEvent::KEYCODE_SPACE, combinationKeyCallBack, true);
60 SubscribeCombinationKey(
61 MMI::KeyEvent::KEYCODE_META_RIGHT, MMI::KeyEvent::KEYCODE_SPACE, combinationKeyCallBack, true);
62
63 CombinationKeyCallBack ctrlShiftCallBack = [callback](std::shared_ptr<MMI::KeyEvent> keyEvent) {
64 InputMethodSysEvent::GetInstance().ReportSystemShortCut("usual.event.CTRL_SHIFT");
65 if (callback == nullptr) {
66 IMSA_HILOGE("callback is nullptr!");
67 return;
68 }
69 callback->TriggerSwitch();
70 };
71
72 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_CTRL_LEFT, MMI::KeyEvent::KEYCODE_SHIFT_LEFT, ctrlShiftCallBack);
73 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_CTRL_LEFT, MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, ctrlShiftCallBack);
74 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_CTRL_RIGHT, MMI::KeyEvent::KEYCODE_SHIFT_LEFT, ctrlShiftCallBack);
75 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_CTRL_RIGHT, MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, ctrlShiftCallBack);
76 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_SHIFT_LEFT, MMI::KeyEvent::KEYCODE_CTRL_LEFT, ctrlShiftCallBack);
77 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, MMI::KeyEvent::KEYCODE_CTRL_LEFT, ctrlShiftCallBack);
78 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_SHIFT_LEFT, MMI::KeyEvent::KEYCODE_CTRL_RIGHT, ctrlShiftCallBack);
79 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, MMI::KeyEvent::KEYCODE_CTRL_RIGHT, ctrlShiftCallBack);
80 return ErrorCode::NO_ERROR;
81 }
82
SubscribeCombinationKey(int32_t preKey,int32_t finalKey,CombinationKeyCallBack callback,bool setFinalKeyDown)83 void KeyboardEvent::SubscribeCombinationKey(
84 int32_t preKey, int32_t finalKey, CombinationKeyCallBack callback, bool setFinalKeyDown)
85 {
86 std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
87 std::set<int32_t> preKeys;
88 preKeys.insert(preKey);
89 keyOption->SetPreKeys(preKeys);
90 keyOption->SetFinalKey(finalKey);
91 keyOption->SetFinalKeyDown(setFinalKeyDown);
92 // 0 means press delay 0 ms
93 keyOption->SetFinalKeyDownDuration(0);
94 auto manager = InputManager::GetInstance();
95 if (manager == nullptr) {
96 IMSA_HILOGE("manager is nullptr");
97 return;
98 }
99 int32_t subscribeId = manager->SubscribeKeyEvent(keyOption, callback);
100 if (subscribeId < 0) {
101 IMSA_HILOGE("failed to SubscribeKeyEvent, id: %{public}d preKey: %{public}d.", subscribeId, preKey);
102 }
103 }
104 } // namespace MiscServices
105 } // namespace OHOS