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 int32_t monitorId = InputManager::GetInstance()->AddMonitor([callback](std::shared_ptr<MMI::KeyEvent> keyEvent) {
34 if (callback == nullptr) {
35 IMSA_HILOGE("callback is nullptr!");
36 return;
37 }
38 callback->OnInputEvent(keyEvent);
39 });
40 if (monitorId < 0) {
41 IMSA_HILOGE("add monitor failed, id: %{public}d!", monitorId);
42 return ErrorCode::ERROR_SUBSCRIBE_KEYBOARD_EVENT;
43 }
44 IMSA_HILOGD("add monitor success, id: %{public}d.", monitorId);
45
46 CombinationKeyCallBack combinationKeyCallBack = [callback](std::shared_ptr<MMI::KeyEvent> keyEvent) {
47 InputMethodSysEvent::GetInstance().ReportSystemShortCut("usual.event.WIN_SPACE");
48 if (callback == nullptr) {
49 IMSA_HILOGE("callback is nullptr!");
50 return;
51 }
52 callback->TriggerSwitch();
53 };
54 SubscribeCombinationKey(
55 MMI::KeyEvent::KEYCODE_META_LEFT, MMI::KeyEvent::KEYCODE_SPACE, combinationKeyCallBack, true);
56 SubscribeCombinationKey(
57 MMI::KeyEvent::KEYCODE_META_RIGHT, MMI::KeyEvent::KEYCODE_SPACE, combinationKeyCallBack, true);
58
59 CombinationKeyCallBack ctrlShiftCallBack = [callback](std::shared_ptr<MMI::KeyEvent> keyEvent) {
60 InputMethodSysEvent::GetInstance().ReportSystemShortCut("usual.event.CTRL_SHIFT");
61 if (callback == nullptr) {
62 IMSA_HILOGE("callback is nullptr!");
63 return;
64 }
65 callback->TriggerSwitch();
66 };
67
68 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_CTRL_LEFT, MMI::KeyEvent::KEYCODE_SHIFT_LEFT, ctrlShiftCallBack);
69 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_CTRL_LEFT, MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, ctrlShiftCallBack);
70 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_CTRL_RIGHT, MMI::KeyEvent::KEYCODE_SHIFT_LEFT, ctrlShiftCallBack);
71 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_CTRL_RIGHT, MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, ctrlShiftCallBack);
72 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_SHIFT_LEFT, MMI::KeyEvent::KEYCODE_CTRL_LEFT, ctrlShiftCallBack);
73 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, MMI::KeyEvent::KEYCODE_CTRL_LEFT, ctrlShiftCallBack);
74 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_SHIFT_LEFT, MMI::KeyEvent::KEYCODE_CTRL_RIGHT, ctrlShiftCallBack);
75 SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, MMI::KeyEvent::KEYCODE_CTRL_RIGHT, ctrlShiftCallBack);
76 return ErrorCode::NO_ERROR;
77 }
78
SubscribeCombinationKey(int32_t preKey,int32_t finalKey,CombinationKeyCallBack callback,bool setFinalKeyDown)79 void KeyboardEvent::SubscribeCombinationKey(
80 int32_t preKey, int32_t finalKey, CombinationKeyCallBack callback, bool setFinalKeyDown)
81 {
82 std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
83 std::set<int32_t> preKeys;
84 preKeys.insert(preKey);
85 keyOption->SetPreKeys(preKeys);
86 keyOption->SetFinalKey(finalKey);
87 keyOption->SetFinalKeyDown(setFinalKeyDown);
88 // 0 means press delay 0 ms
89 keyOption->SetFinalKeyDownDuration(0);
90 int32_t subscribeId = InputManager::GetInstance()->SubscribeKeyEvent(keyOption, callback);
91 if (subscribeId < 0) {
92 IMSA_HILOGE("failed to SubscribeKeyEvent, id: %{public}d preKey: %{public}d.", subscribeId, preKey);
93 }
94 }
95 } // namespace MiscServices
96 } // namespace OHOS