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