• 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  */
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");
38     std::shared_ptr<InputEventCallback> callback = std::make_shared<InputEventCallback>();
39     callback->SetKeyHandle(handle);
40     int32_t monitorId =
41         InputManager::GetInstance()->AddMonitor(std::static_pointer_cast<MMI::IInputEventConsumer>(callback));
42     if (monitorId < 0) {
43         IMSA_HILOGE("add monitor failed, id: %{public}d", monitorId);
44         return ErrorCode::ERROR_SUBSCRIBE_KEYBOARD_EVENT;
45     }
46     IMSA_HILOGD("add monitor success, id: %{public}d", monitorId);
47 
48     CombinationKeyCallBack combinationKeyCallBack = [callback](std::shared_ptr<MMI::KeyEvent> keyEvent) {
49         if (callback == nullptr) {
50             IMSA_HILOGE("callback is nullptr.");
51         }
52         callback->TriggerSwitch();
53     };
54     SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_META_LEFT, MMI::KeyEvent::KEYCODE_SPACE, combinationKeyCallBack);
55     SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_META_RIGHT, MMI::KeyEvent::KEYCODE_SPACE, combinationKeyCallBack);
56     return ErrorCode::NO_ERROR;
57 }
58 
SubscribeCombinationKey(int32_t preKey,int32_t finalKey,CombinationKeyCallBack callback)59 void KeyboardEvent::SubscribeCombinationKey(int32_t preKey, int32_t finalKey, CombinationKeyCallBack callback)
60 {
61     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
62     std::set<int32_t> preKeys;
63     preKeys.insert(preKey);
64     keyOption->SetPreKeys(preKeys);
65     keyOption->SetFinalKey(finalKey);
66     keyOption->SetFinalKeyDown(true);
67     // 0 means press delay 0 ms
68     keyOption->SetFinalKeyDownDuration(0);
69     int32_t subscribeId = InputManager::GetInstance()->SubscribeKeyEvent(keyOption, callback);
70     if (subscribeId < 0) {
71         IMSA_HILOGE("SubscribeKeyEvent failed, id: %{public}d prekey: %{public}d", subscribeId, preKey);
72     }
73 }
74 } // namespace MiscServices
75 } // namespace OHOS
76