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 } // namespace
59
KeyToString(int32_t code)60 const char* KeyToString(int32_t code)
61 {
62 if (code >= 0 && code < KEYCODE_SIZE) {
63 return g_aospKeycode2String[code];
64 } else {
65 return g_aospKeycode2String[0]; // "UnKnown"
66 }
67 }
68
ConvertInputCodeToString() const69 std::string KeyEvent::ConvertInputCodeToString() const
70 {
71 static const std::map<KeyCode, std::string> VISIBILITY_CODE = { { KeyCode::KEY_0, "0)" }, { KeyCode::KEY_1, "1!" },
72 { KeyCode::KEY_2, "2@" }, { KeyCode::KEY_3, "3#" }, { KeyCode::KEY_4, "4$" }, { KeyCode::KEY_5, "5%" },
73 { KeyCode::KEY_6, "6^" }, { KeyCode::KEY_7, "7&" }, { KeyCode::KEY_8, "8*" }, { KeyCode::KEY_9, "9(" },
74 { KeyCode::KEY_COMMA, ",<" }, { KeyCode::KEY_PERIOD, ".>" }, { KeyCode::KEY_GRAVE, "`~" },
75 { KeyCode::KEY_MINUS, "-_" }, { KeyCode::KEY_EQUALS, "=+" }, { KeyCode::KEY_LEFT_BRACKET, "[{" },
76 { KeyCode::KEY_RIGHT_BRACKET, "]}" }, { KeyCode::KEY_BACKSLASH, "\\|" }, { KeyCode::KEY_SEMICOLON, ";:" },
77 { KeyCode::KEY_APOSTROPHE, "\'\"" }, { KeyCode::KEY_SLASH, "/?" }, { KeyCode::KEY_AT, "@" },
78 { KeyCode::KEY_PLUS, "+=" }, { KeyCode::KEY_NUMPAD_DIVIDE, "/" }, { KeyCode::KEY_NUMPAD_MULTIPLY, "*" },
79 { KeyCode::KEY_NUMPAD_SUBTRACT, "-" }, { KeyCode::KEY_NUMPAD_ADD, "+" }, { KeyCode::KEY_NUMPAD_DOT, "." },
80 { KeyCode::KEY_NUMPAD_COMMA, "," }, { KeyCode::KEY_NUMPAD_EQUALS, "=" }, { KeyCode::KEY_TAB, "\t" },
81 { KeyCode::KEY_SPACE, " " }, { KeyCode::KEY_ESCAPE, "ESC" }, { KeyCode::KEY_F1, "F1" },
82 { KeyCode::KEY_F2, "F2" }, { KeyCode::KEY_F3, "F3" }, { KeyCode::KEY_F4, "F4" }, { KeyCode::KEY_F5, "F5" },
83 { KeyCode::KEY_F6, "F6" }, { KeyCode::KEY_F7, "F7" }, { KeyCode::KEY_F8, "F8" }, { KeyCode::KEY_F9, "F9" },
84 { KeyCode::KEY_F10, "F10" }, { KeyCode::KEY_F11, "F11" }, { KeyCode::KEY_F12, "F12" } };
85 if (KeyCode::KEY_NUMPAD_0 <= code && code <= KeyCode::KEY_NUMPAD_9) {
86 return std::to_string(static_cast<int32_t>(code) - static_cast<int32_t>(KeyCode::KEY_NUMPAD_0));
87 }
88 if (IsLetterKey()) {
89 int32_t codeValue = static_cast<int32_t>(code) - static_cast<int32_t>(KeyCode::KEY_A);
90 return std::string(1, static_cast<char>(codeValue + ASCII_START_UPPER_CASE_LETTER));
91 }
92 auto iter = VISIBILITY_CODE.find(code);
93 if (iter != VISIBILITY_CODE.end()) {
94 return iter->second;
95 }
96 return "";
97 }
98
99 } // namespace OHOS::Ace