• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "core/event/key_event.h"
17 
18 namespace OHOS::Ace {
19 
CalculateModifierKeyState(const std::vector<OHOS::Ace::KeyCode> & status)20 uint64_t CalculateModifierKeyState(const std::vector<OHOS::Ace::KeyCode>& status)
21 {
22     uint64_t modifierKeysState = 0;
23     // check ctrl
24     if ((std::find(status.begin(), status.end(), OHOS::Ace::KeyCode::KEY_CTRL_LEFT) != std::end(status)) ||
25         (std::find(status.begin(), status.end(), OHOS::Ace::KeyCode::KEY_CTRL_RIGHT) != std::end(status))) {
26         modifierKeysState |= static_cast<uint64_t>(ModifierKeyName::ModifierKeyCtrl);
27     }
28     // check alt
29     if ((std::find(status.begin(), status.end(), OHOS::Ace::KeyCode::KEY_ALT_LEFT) != std::end(status)) ||
30         (std::find(status.begin(), status.end(), OHOS::Ace::KeyCode::KEY_ALT_RIGHT) != std::end(status))) {
31         modifierKeysState |= static_cast<uint64_t>(ModifierKeyName::ModifierKeyAlt);
32     }
33     // check shift
34     if ((std::find(status.begin(), status.end(), OHOS::Ace::KeyCode::KEY_SHIFT_LEFT) != std::end(status)) ||
35         (std::find(status.begin(), status.end(), OHOS::Ace::KeyCode::KEY_SHIFT_RIGHT) != std::end(status))) {
36         modifierKeysState |= static_cast<uint64_t>(ModifierKeyName::ModifierKeyShift);
37     }
38     // check fn
39     if (std::find(status.begin(), status.end(), OHOS::Ace::KeyCode::KEY_FN) != std::end(status)) {
40         modifierKeysState |= static_cast<uint64_t>(ModifierKeyName::ModifierKeyFn);
41     }
42 
43     return modifierKeysState;
44 }
45 
46 namespace {
47 
48 // key : define aosp key event code, value : define ace key string
49 constexpr int32_t KEYCODE_SIZE = 289;
50 const char* g_aospKeycode2String[KEYCODE_SIZE] = { "Unknown", "SoftLeft", "SoftRight", "Home", "Back", "Call",
51     "EndCall", "Digit0", "Digit1", "Digit2", "Digit3", "Digit4", "Digit5", "Digit6", "Digit7", "Digit8", "Digit9",
52     "Star", "Pound", "DpadUp", "DpadDown", "DpadLeft", "DpadRight", "DpadCenter", "VolumeUp", "VolumeDown", "Power",
53     "Camera", "Clear", "KeyA", "KeyB", "KeyC", "KeyD", "KeyE", "KeyF", "KeyG", "KeyH", "KeyI", "KeyJ", "KeyK", "KeyL",
54     "KeyM", "KeyN", "KeyO", "KeyP", "KeyQ", "KeyR", "KeyS", "KeyT", "KeyU", "KeyV", "KeyW", "KeyX", "KeyY", "KeyZ",
55     "Comma", "Period", "AltLeft", "AltRight", "ShiftLeft", "ShiftRight", "Tab", "Space", "Sym", "Explorer", "Envelope",
56     "Enter", "Del", "Grave", "Minus", "Equals", "LeftBracket", "RightBracket", "BackSlash", "Semicolon", "Apostrophe",
57     "Slash", "At", "Num", "HeadsetHook", "Focus", "Plus", "Menu", "Notification", "Search", "MediaPlayPause",
58     "MediaStop", "MediaNext", "MediaPrevious", "MediaRewind", "MediaFastForward", "Mute", "PageUp", "PageDown",
59     "PictSymbols", "SwitchCharset", "ButtonA", "ButtonB", "ButtonC", "ButtonX", "ButtonY", "ButtonZ", "ButtonL1",
60     "ButtonR1", "ButtonL2", "ButtonR2", "ButtonThumbl", "ButtonThumbr", "ButtonStart", "ButtonSelect", "ButtonMode",
61     "Escape", "ForwardDel", "CtrlLeft", "CtrlRight", "CapsLock", "ScrollLock", "MetaLeft", "MetaRight", "Function",
62     "SysRq", "Break", "MoveHome", "MoveEnd", "Insert", "Forward", "MediaPlay", "MediaPause", "MediaClose",
63     "MediaReject", "MediaRecord", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "NumLock",
64     "Numpad0", "Numpad1", "Numpad2", "Numpad3", "Numpad4", "Numpad5", "Numpad6", "Numpad7", "Numpad8", "NumPad9",
65     "NumpadDivide", "NumpadMultiply", "NumpadSubtract", "NumpadAdd", "NumpadDot", "NumpadComma", "NumpadEnter",
66     "NumpadEquals", "NumpadLeftParen", "NumpadRightParen", "VolumeMute", "Info", "ChannelUp", "ChannelDown", "ZoomIn",
67     "ZoomOut", "Tv", "Window", "Guide", "Dvr", "BookMark", "Captions", "Settings", "TvPower", "TvInput", "StbPower",
68     "StbInput", "AvrPower", "AvrInput", "ProgRed", "ProgGreen", "ProgYellow", "ProgBlue", "AppSwitch", "Button1",
69     "Button2", "Button3", "Button4", "Button5", "Button6", "Button7", "Button8", "Button9", "Button10", "Button11",
70     "Button12", "Button13", "Button14", "Button15", "Button16", "LanguageSwitch", "MannerMode", "3DMode", "Contacts",
71     "Calendar", "Music", "Calculator", "ZenkakuHankaku", "Eisu", "Muhenkan", "Henkan", "KatakanaHiragana", "Yen", "Ro",
72     "Kana", "Assist", "BrightnessDown", "BrightnessUp", "MediaAudioTrack", "Sleep", "WakeUp", "Pairing", "MediaTopMenu",
73     "Digit11", "Digit12", "LastChannel", "TvDataService", "VoiceAssist", "TvRadioService", "TvTeletext",
74     "TvNumberEntry", "TvTerrestrialAnalog", "TvTerrestrialDigital", "TvSatellite", "TvSatelliteBS", "TvSatelliteCS",
75     "TvSatelliteService", "TvNetWork", "TvAntennaCable", "TvInputHdmi1", "TvInputHdmi2", "TvInputHdmi3", "TvInputHdmi4",
76     "TvInputComposite1", "TvInputComposite2", "TvInputComponent1", "TvInputComponent2", "TvInputVGA1",
77     "TvAudioDescription", "TvAudioDescriptionMixUp", "TvAudioDescriptionMixDown", "TvZoomMode", "TvContentsMenu",
78     "TvMediaContextMenu", "TvTimerProgramming", "Help", "NavigatePrevious", "NavigateNext", "NavigateIn", "NavigateOut",
79     "StemPrimary", "Stem1", "Stem2", "Stem3", "DpadUpLeft", "DpadDownLeft", "DpadUpRight", "DpadDownRight",
80     "MediaSkipForward", "MediaSkipBackward", "MediaStepForward", "MediaStepBackward", "SoftSleep", "Cut", "Copy",
81     "Paste", "SystemNavigationUp", "SystemNavigationDown", "SystemNavigationLeft", "SystemNavigationRight", "AllApps",
82     "Refresh", "ThumbsUp", "ThumbsDown", "ProfileSwitch" };
83 
84 const std::unordered_map<KeyCode, char> KEYBOARD_SYMBOLS = {
85     { KeyCode::KEY_GRAVE, '`' },
86     { KeyCode::KEY_MINUS, '-' },
87     { KeyCode::KEY_EQUALS, '=' },
88     { KeyCode::KEY_LEFT_BRACKET, '[' },
89     { KeyCode::KEY_RIGHT_BRACKET, ']' },
90     { KeyCode::KEY_BACKSLASH, '\\' },
91     { KeyCode::KEY_SEMICOLON, ';' },
92     { KeyCode::KEY_APOSTROPHE, '\'' },
93     { KeyCode::KEY_COMMA, ',' },
94     { KeyCode::KEY_PERIOD, '.' },
95     { KeyCode::KEY_SLASH, '/' },
96     { KeyCode::KEY_SPACE, ' ' },
97     { KeyCode::KEY_NUMPAD_DIVIDE, '/' },
98     { KeyCode::KEY_NUMPAD_MULTIPLY, '*' },
99     { KeyCode::KEY_NUMPAD_SUBTRACT, '-' },
100     { KeyCode::KEY_NUMPAD_ADD, '+' },
101     { KeyCode::KEY_NUMPAD_COMMA, ',' },
102     { KeyCode::KEY_NUMPAD_EQUALS, '=' },
103 };
104 
105 const std::unordered_map<KeyCode, char> SHIFT_KEYBOARD_SYMBOLS = {
106     { KeyCode::KEY_GRAVE, '~' },
107     { KeyCode::KEY_MINUS, '_' },
108     { KeyCode::KEY_EQUALS, '+' },
109     { KeyCode::KEY_LEFT_BRACKET, '{' },
110     { KeyCode::KEY_RIGHT_BRACKET, '}' },
111     { KeyCode::KEY_BACKSLASH, '|' },
112     { KeyCode::KEY_SEMICOLON, ':' },
113     { KeyCode::KEY_APOSTROPHE, '\"' },
114     { KeyCode::KEY_COMMA, '<' },
115     { KeyCode::KEY_PERIOD, '>' },
116     { KeyCode::KEY_SLASH, '?' },
117 };
118 
119 } // namespace
120 
121 
122 
KeyToString(int32_t code)123 const char* KeyToString(int32_t code)
124 {
125     if (code >= 0 && code < KEYCODE_SIZE) {
126         return g_aospKeycode2String[code];
127     } else {
128         return g_aospKeycode2String[0]; // "UnKnown"
129     }
130 }
131 
ConvertInputCodeToString() const132 std::string KeyEvent::ConvertInputCodeToString() const
133 {
134     static const std::map<KeyCode, std::string> VISIBILITY_CODE = { { KeyCode::KEY_0, "0)" }, { KeyCode::KEY_1, "1!" },
135         { KeyCode::KEY_2, "2@" }, { KeyCode::KEY_3, "3#" }, { KeyCode::KEY_4, "4$" }, { KeyCode::KEY_5, "5%" },
136         { KeyCode::KEY_6, "6^" }, { KeyCode::KEY_7, "7&" }, { KeyCode::KEY_8, "8*" }, { KeyCode::KEY_9, "9(" },
137         { KeyCode::KEY_COMMA, ",<" }, { KeyCode::KEY_PERIOD, ".>" }, { KeyCode::KEY_GRAVE, "`~" },
138         { KeyCode::KEY_MINUS, "-_" }, { KeyCode::KEY_EQUALS, "=+" }, { KeyCode::KEY_LEFT_BRACKET, "[{" },
139         { KeyCode::KEY_RIGHT_BRACKET, "]}" }, { KeyCode::KEY_BACKSLASH, "\\|" }, { KeyCode::KEY_SEMICOLON, ";:" },
140         { KeyCode::KEY_APOSTROPHE, "\'\"" }, { KeyCode::KEY_SLASH, "/?" }, { KeyCode::KEY_AT, "@" },
141         { KeyCode::KEY_PLUS, "+=" }, { KeyCode::KEY_NUMPAD_DIVIDE, "/" }, { KeyCode::KEY_NUMPAD_MULTIPLY, "*" },
142         { KeyCode::KEY_NUMPAD_SUBTRACT, "-" }, { KeyCode::KEY_NUMPAD_ADD, "+" }, { KeyCode::KEY_NUMPAD_DOT, "." },
143         { KeyCode::KEY_NUMPAD_COMMA, "," }, { KeyCode::KEY_NUMPAD_EQUALS, "=" }, { KeyCode::KEY_TAB, "TAB" },
144         { KeyCode::KEY_SPACE, " " }, { KeyCode::KEY_ESCAPE, "ESC" }, { KeyCode::KEY_F1, "F1" },
145         { KeyCode::KEY_F2, "F2" }, { KeyCode::KEY_F3, "F3" }, { KeyCode::KEY_F4, "F4" }, { KeyCode::KEY_F5, "F5" },
146         { KeyCode::KEY_F6, "F6" }, { KeyCode::KEY_F7, "F7" }, { KeyCode::KEY_F8, "F8" }, { KeyCode::KEY_F9, "F9" },
147         { KeyCode::KEY_F10, "F10" }, { KeyCode::KEY_F11, "F11" }, { KeyCode::KEY_F12, "F12" },
148         { KeyCode::KEY_DPAD_UP, "DPAD_UP" }, { KeyCode::KEY_DPAD_DOWN, "DPAD_DOWN" },
149         { KeyCode::KEY_DPAD_LEFT, "DPAD_LEFT" }, { KeyCode::KEY_DPAD_RIGHT, "DPAD_RIGHT" } };
150     if (KeyCode::KEY_NUMPAD_0 <= code && code <= KeyCode::KEY_NUMPAD_9) {
151         return std::to_string(static_cast<int32_t>(code) - static_cast<int32_t>(KeyCode::KEY_NUMPAD_0));
152     }
153     if (IsLetterKey()) {
154         int32_t codeValue = static_cast<int32_t>(code) - static_cast<int32_t>(KeyCode::KEY_A);
155         return std::string(1, static_cast<char>(codeValue + ASCII_START_UPPER_CASE_LETTER));
156     }
157     auto iter = VISIBILITY_CODE.find(code);
158     if (iter != VISIBILITY_CODE.end()) {
159         return iter->second;
160     }
161     return "";
162 }
163 
ConvertCodeToString() const164 std::string KeyEvent::ConvertCodeToString() const
165 {
166     static const std::string NUM_SYMBOLS = ")!@#$%^&*(";
167 
168     if (KeyCode::KEY_0 <= code && code <= KeyCode::KEY_9) {
169         if (IsShiftWith(code)) {
170             return std::string(1, NUM_SYMBOLS[static_cast<int32_t>(code) - static_cast<int32_t>(KeyCode::KEY_0)]);
171         }
172         return std::to_string(static_cast<int32_t>(code) - static_cast<int32_t>(KeyCode::KEY_0));
173     }
174     if (numLock) {
175         if (KeyCode::KEY_NUMPAD_0 <= code && code <= KeyCode::KEY_NUMPAD_9) {
176             return std::to_string(static_cast<int32_t>(code) - static_cast<int32_t>(KeyCode::KEY_NUMPAD_0));
177         } else if (code == KeyCode::KEY_NUMPAD_DOT) {
178             return std::string(1, '.');
179         }
180     }
181     if (IsLetterKey()) {
182         int32_t codeValue = static_cast<int32_t>(code) - static_cast<int32_t>(KeyCode::KEY_A);
183         auto shiftWithLetter = IsShiftWith(code);
184         if ((shiftWithLetter || enableCapsLock) && !(shiftWithLetter && enableCapsLock)) {
185             return std::string(1, static_cast<char>(codeValue + ASCII_START_UPPER_CASE_LETTER));
186         }
187         return std::string(1, static_cast<char>(codeValue + ASCII_START_LOWER_CASE_LETTER));
188     }
189     if (IsShiftWith(code)) {
190         auto iterCode = SHIFT_KEYBOARD_SYMBOLS.find(code);
191         if (iterCode != SHIFT_KEYBOARD_SYMBOLS.end()) {
192             return std::string(1, iterCode->second);
193         }
194     }
195     auto iterCode = KEYBOARD_SYMBOLS.find(code);
196     if (iterCode != KEYBOARD_SYMBOLS.end()) {
197         return std::string(1, iterCode->second);
198     }
199     return "";
200 }
201 
ParseKeyEvent(KeyEvent & keyEvent)202 void KeyEventInfo::ParseKeyEvent(KeyEvent& keyEvent)
203 {
204     keyEvent.action = GetKeyType();
205     keyEvent.code = GetKeyCode();
206     keyEvent.key.assign(GetKeyText().c_str());
207     keyEvent.sourceType = GetKeySource();
208     keyEvent.deviceId = GetDeviceId();
209     keyEvent.metaKey = GetMetaKey();
210     keyEvent.unicode = GetUnicode();
211     keyEvent.timeStamp = GetTimeStamp();
212     keyEvent.keyIntention = GetKeyIntention();
213     keyEvent.pressedCodes = GetPressedKeyCodes();
214     keyEvent.numLock = GetNumLock();
215     keyEvent.scrollLock = GetScrollLock();
216     keyEvent.enableCapsLock = GetCapsLock();
217 }
218 
219 } // namespace OHOS::Ace