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