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