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