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
18 namespace OHOS {
19 namespace MiscServices {
20 uint32_t InputEventCallback::keyState_ = static_cast<uint32_t>(0);
21 bool InputEventCallback::isKeyHandled_ = false;
22 const std::map<int32_t, uint8_t> MASK_MAP{
23 { MMI::KeyEvent::KEYCODE_CAPS_LOCK, KeyboardEvent::CAPS_MASK },
24 };
25
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const26 void InputEventCallback::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
27 {
28 auto keyCode = keyEvent->GetKeyCode();
29 auto keyAction = keyEvent->GetKeyAction();
30 auto currKey = MASK_MAP.find(keyCode);
31 if (currKey == MASK_MAP.end()) {
32 IMSA_HILOGD("key code is unknown.");
33 keyState_ = 0;
34 return;
35 }
36
37 if (keyAction == MMI::KeyEvent::KEY_ACTION_DOWN) {
38 IMSA_HILOGD("key pressed down.");
39 keyState_ = static_cast<uint32_t>(keyState_ | currKey->second);
40 if (keyCode == MMI::KeyEvent::KEYCODE_CAPS_LOCK) {
41 if (keyHandler_ != nullptr) {
42 int32_t ret = keyHandler_(keyState_);
43 IMSA_HILOGI("handle key event ret: %{public}d.", ret);
44 }
45 isKeyHandled_ = true;
46 return;
47 }
48 isKeyHandled_ = false;
49 return;
50 }
51
52 if (keyAction == MMI::KeyEvent::KEY_ACTION_UP) {
53 if (keyHandler_ != nullptr && !isKeyHandled_) {
54 int32_t ret = keyHandler_(keyState_);
55 IMSA_HILOGI("handle key event ret: %{public}d.", ret);
56 }
57 isKeyHandled_ = true;
58 keyState_ = static_cast<uint32_t>(keyState_ & ~currKey->second);
59 }
60 }
61
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const62 void InputEventCallback::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
63 {
64 }
65
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const66 void InputEventCallback::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const
67 {
68 }
69
SetKeyHandle(KeyHandle handle)70 void InputEventCallback::SetKeyHandle(KeyHandle handle)
71 {
72 keyHandler_ = std::move(handle);
73 }
74
TriggerSwitch()75 void InputEventCallback::TriggerSwitch()
76 {
77 auto state = KeyboardEvent::META_MASK | KeyboardEvent::SPACE_MASK;
78 if (keyHandler_ == nullptr) {
79 IMSA_HILOGI("keyHandler_ is nullptr.");
80 return;
81 }
82 int32_t ret = keyHandler_(state);
83 IMSA_HILOGI("handle combinationkey ret: %{public}d.", ret);
84 }
85 } // namespace MiscServices
86 } // namespace OHOS