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 #define private public
17 #define protected public
18 #include "key_event.h"
19 #undef private
20 #undef protected
21 #include "mock_key_event.h"
22 
23 namespace OHOS {
24 namespace MMI {
25 const int32_t KeyEvent::KEYCODE_UNKNOWN = -1;
26 const int32_t KeyEvent::KEYCODE_VOLUME_UP = 16;
27 const int32_t KeyEvent::KEYCODE_VOLUME_DOWN = 17;
28 const int32_t KeyEvent::KEYCODE_POWER = 18;
29 const int32_t KeyEvent::KEY_ACTION_UNKNOWN = 0X00000000;
30 const int32_t KeyEvent::KEY_ACTION_CANCEL = 0X00000001;
31 const int32_t KeyEvent::KEY_ACTION_DOWN = 0x00000002;
32 const int32_t KeyEvent::KEY_ACTION_UP = 0X00000003;
33 const int32_t KeyEvent::KEYCODE_NUMPAD_1 = 2104;
34 const int32_t KeyEvent::KEYCODE_NUMPAD_2 = 2105;
35 const int32_t KeyEvent::KEYCODE_NUMPAD_3 = 2106;
36 const int32_t KeyEvent::KEYCODE_NUMPAD_4 = 2107;
37 const int32_t KeyEvent::KEYCODE_NUMPAD_5 = 2108;
38 const int32_t KeyEvent::KEYCODE_NUMPAD_6 = 2109;
39 const int32_t KeyEvent::KEYCODE_NUMPAD_7 = 2110;
40 const int32_t KeyEvent::KEYCODE_NUMPAD_8 = 2111;
41 const int32_t KeyEvent::KEYCODE_NUMPAD_9 = 2112;
42 const int32_t KeyEvent::KEYCODE_NUMPAD_DIVIDE = 2113;
43 const int32_t KeyEvent::KEYCODE_NUMPAD_MULTIPLY = 2114;
44 const int32_t KeyEvent::KEYCODE_NUMPAD_SUBTRACT = 2115;
45 const int32_t KeyEvent::KEYCODE_NUMPAD_ADD = 2116;
46 const int32_t KeyEvent::KEYCODE_SHIFT_LEFT = 2047;
47 const int32_t KeyEvent::KEYCODE_SHIFT_RIGHT = 2048;
48 const int32_t KeyEvent::KEYCODE_CTRL_LEFT = 2072;
49 const int32_t KeyEvent::KEYCODE_CTRL_RIGHT = 2073;
50 
KeyItem()51 KeyEvent::KeyItem::KeyItem()
52 {}
53 
~KeyItem()54 KeyEvent::KeyItem::~KeyItem()
55 {}
56 
GetKeyCode() const57 int32_t KeyEvent::KeyItem::GetKeyCode() const
58 {
59     return keyCode_;
60 }
61 
SetKeyCode(int32_t keyCode)62 void KeyEvent::KeyItem::SetKeyCode(int32_t keyCode)
63 {
64     keyCode_ = keyCode;
65 }
66 
GetDownTime() const67 int64_t KeyEvent::KeyItem::GetDownTime() const
68 {
69     return downTime_;
70 }
71 
SetDownTime(int64_t downTime)72 void KeyEvent::KeyItem::SetDownTime(int64_t downTime)
73 {
74     downTime_ = downTime;
75 }
76 
GetDeviceId() const77 int32_t KeyEvent::KeyItem::GetDeviceId() const
78 {
79     return deviceId_;
80 }
81 
SetDeviceId(int32_t deviceId)82 void KeyEvent::KeyItem::SetDeviceId(int32_t deviceId)
83 {
84     deviceId_ = deviceId;
85 }
86 
IsPressed() const87 bool KeyEvent::KeyItem::IsPressed() const
88 {
89     return pressed_;
90 }
91 
SetPressed(bool pressed)92 void KeyEvent::KeyItem::SetPressed(bool pressed)
93 {
94     pressed_ = pressed;
95 }
96 
KeyEvent(int32_t eventType)97 KeyEvent::KeyEvent(int32_t eventType) : InputEvent(eventType)
98 {}
99 
KeyEvent(const KeyEvent & other)100 KeyEvent::KeyEvent(const KeyEvent& other)
101     : InputEvent(other), keyCode_(other.keyCode_), keys_(other.keys_), keyAction_(other.keyAction_)
102 {}
103 
~KeyEvent()104 KeyEvent::~KeyEvent()
105 {}
106 
Create()107 std::shared_ptr<KeyEvent> KeyEvent::Create()
108 {
109     return std::make_shared<KeyEvent>(InputEvent::EVENT_TYPE_KEY);
110 }
111 
Reset()112 void KeyEvent::Reset()
113 {
114     InputEvent::Reset();
115 }
116 
AddKeyItem(const KeyItem & keyItem)117 void KeyEvent::AddKeyItem(const KeyItem& keyItem)
118 {
119     keys_.push_back(keyItem);
120 }
121 
GetKeyCode() const122 int32_t KeyEvent::GetKeyCode() const
123 {
124     return keyCode_;
125 }
126 
SetKeyCode(int32_t keyCode)127 void KeyEvent::SetKeyCode(int32_t keyCode)
128 {
129     keyCode_ = keyCode;
130 }
131 
WriteToParcel(Parcel & out) const132 bool KeyEvent::WriteToParcel(Parcel& out) const
133 {
134     return true;
135 }
136 
ReadFromParcel(Parcel & in)137 bool KeyEvent::ReadFromParcel(Parcel& in)
138 {
139     return true;
140 }
141 
GetPressedKeys() const142 std::vector<int32_t> KeyEvent::GetPressedKeys() const
143 {
144     std::vector<int32_t> result;
145     for (const auto &item : keys_) {
146         if (item.IsPressed()) {
147             result.push_back(item.GetKeyCode());
148         }
149     }
150     return result;
151 }
152 
GetKeyAction() const153 int32_t KeyEvent::GetKeyAction() const
154 {
155     return keyAction_;
156 }
157 } // namespace MMI
158 } // namespace OHOS