1 /* 2 * Copyright (c) 2021-2023 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_KEY_EVENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_KEY_EVENT_H 18 19 #include <map> 20 21 #include "core/event/ace_events.h" 22 23 namespace OHOS::MMI { 24 class KeyEvent; 25 } 26 27 namespace OHOS::Ace { 28 29 enum class KeyCode : int32_t { 30 KEY_UNKNOWN = -1, 31 KEY_FN = 0, // New 32 KEY_HOME = 1, 33 KEY_BACK = 2, 34 KEY_CALL = 3, 35 KEY_ENDCALL = 4, 36 KEY_CLEAR = 5, 37 KEY_HEADSETHOOK = 6, 38 KEY_FOCUS = 7, 39 KEY_NOTIFICATION = 8, 40 KEY_SEARCH = 9, 41 KEY_MEDIA_PLAY_PAUSE = 10, 42 KEY_MEDIA_STOP = 11, 43 KEY_MEDIA_NEXT = 12, 44 KEY_MEDIA_PREVIOUS = 13, 45 KEY_MEDIA_REWIND = 14, 46 KEY_MEDIA_FAST_FORWARD = 15, 47 KEY_VOLUME_UP = 16, 48 KEY_VOLUME_DOWN = 17, 49 KEY_POWER = 18, 50 KEY_CAMERA = 19, 51 KEY_VOICE_ASSISTANT = 20, 52 KEY_CUSTOM1 = 21, 53 KEY_VOLUME_MUTE = 22, // New 54 KEY_MUTE = 23, // New 55 KEY_BRIGHTNESS_UP = 40, 56 KEY_BRIGHTNESS_DOWN = 41, 57 KEY_WEAR_1 = 1001, 58 KEY_0 = 2000, 59 KEY_1 = 2001, 60 KEY_2 = 2002, 61 KEY_3 = 2003, 62 KEY_4 = 2004, 63 KEY_5 = 2005, 64 KEY_6 = 2006, 65 KEY_7 = 2007, 66 KEY_8 = 2008, 67 KEY_9 = 2009, 68 KEY_STAR = 2010, 69 KEY_POUND = 2011, 70 KEY_DPAD_UP = 2012, 71 KEY_DPAD_DOWN = 2013, 72 KEY_DPAD_LEFT = 2014, 73 KEY_DPAD_RIGHT = 2015, 74 KEY_DPAD_CENTER = 2016, 75 KEY_A = 2017, 76 KEY_B = 2018, 77 KEY_C = 2019, 78 KEY_D = 2020, 79 KEY_E = 2021, 80 KEY_F = 2022, 81 KEY_G = 2023, 82 KEY_H = 2024, 83 KEY_I = 2025, 84 KEY_J = 2026, 85 KEY_K = 2027, 86 KEY_L = 2028, 87 KEY_M = 2029, 88 KEY_N = 2030, 89 KEY_O = 2031, 90 KEY_P = 2032, 91 KEY_Q = 2033, 92 KEY_R = 2034, 93 KEY_S = 2035, 94 KEY_T = 2036, 95 KEY_U = 2037, 96 KEY_V = 2038, 97 KEY_W = 2039, 98 KEY_X = 2040, 99 KEY_Y = 2041, 100 KEY_Z = 2042, 101 KEY_COMMA = 2043, 102 KEY_PERIOD = 2044, 103 KEY_ALT_LEFT = 2045, 104 KEY_ALT_RIGHT = 2046, 105 KEY_SHIFT_LEFT = 2047, 106 KEY_SHIFT_RIGHT = 2048, 107 KEY_TAB = 2049, 108 KEY_SPACE = 2050, 109 KEY_SYM = 2051, 110 KEY_EXPLORER = 2052, 111 KEY_ENVELOPE = 2053, 112 KEY_ENTER = 2054, 113 KEY_DEL = 2055, 114 KEY_GRAVE = 2056, 115 KEY_MINUS = 2057, 116 KEY_EQUALS = 2058, 117 KEY_LEFT_BRACKET = 2059, 118 KEY_RIGHT_BRACKET = 2060, 119 KEY_BACKSLASH = 2061, 120 KEY_SEMICOLON = 2062, 121 KEY_APOSTROPHE = 2063, 122 KEY_SLASH = 2064, 123 KEY_AT = 2065, 124 KEY_PLUS = 2066, 125 KEY_MENU = 2067, 126 KEY_PAGE_UP = 2068, 127 KEY_PAGE_DOWN = 2069, 128 KEY_ESCAPE = 2070, 129 KEY_FORWARD_DEL = 2071, 130 KEY_CTRL_LEFT = 2072, 131 KEY_CTRL_RIGHT = 2073, 132 KEY_CAPS_LOCK = 2074, 133 KEY_SCROLL_LOCK = 2075, 134 KEY_META_LEFT = 2076, 135 KEY_META_RIGHT = 2077, 136 KEY_FUNCTION = 2078, 137 KEY_SYSRQ = 2079, 138 KEY_BREAK = 2080, 139 KEY_MOVE_HOME = 2081, 140 KEY_MOVE_END = 2082, 141 KEY_INSERT = 2083, 142 KEY_FORWARD = 2084, 143 KEY_MEDIA_PLAY = 2085, 144 KEY_MEDIA_PAUSE = 2086, 145 KEY_MEDIA_CLOSE = 2087, 146 KEY_MEDIA_EJECT = 2088, 147 KEY_MEDIA_RECORD = 2089, 148 KEY_F1 = 2090, 149 KEY_F2 = 2091, 150 KEY_F3 = 2092, 151 KEY_F4 = 2093, 152 KEY_F5 = 2094, 153 KEY_F6 = 2095, 154 KEY_F7 = 2096, 155 KEY_F8 = 2097, 156 KEY_F9 = 2098, 157 KEY_F10 = 2099, 158 KEY_F11 = 2100, 159 KEY_F12 = 2101, 160 KEY_NUM_LOCK = 2102, 161 KEY_NUMPAD_0 = 2103, 162 KEY_NUMPAD_1 = 2104, 163 KEY_NUMPAD_2 = 2105, 164 KEY_NUMPAD_3 = 2106, 165 KEY_NUMPAD_4 = 2107, 166 KEY_NUMPAD_5 = 2108, 167 KEY_NUMPAD_6 = 2109, 168 KEY_NUMPAD_7 = 2110, 169 KEY_NUMPAD_8 = 2111, 170 KEY_NUMPAD_9 = 2112, 171 KEY_NUMPAD_DIVIDE = 2113, 172 KEY_NUMPAD_MULTIPLY = 2114, 173 KEY_NUMPAD_SUBTRACT = 2115, 174 KEY_NUMPAD_ADD = 2116, 175 KEY_NUMPAD_DOT = 2117, 176 KEY_NUMPAD_COMMA = 2118, 177 KEY_NUMPAD_ENTER = 2119, 178 KEY_NUMPAD_EQUALS = 2120, 179 KEY_NUMPAD_LEFT_PAREN = 2121, 180 KEY_NUMPAD_RIGHT_PAREN = 2122, 181 // New Start 182 KEY_VIRTUAL_MULTITASK = 2210, 183 KEY_BUTTON_A = 2301, 184 KEY_BUTTON_B = 2302, 185 KEY_BUTTON_C = 2303, 186 KEY_BUTTON_X = 2304, 187 KEY_BUTTON_Y = 2305, 188 KEY_BUTTON_Z = 2306, 189 KEY_BUTTON_L1 = 2307, 190 KEY_BUTTON_R1 = 2308, 191 KEY_BUTTON_L2 = 2309, 192 KEY_BUTTON_R2 = 2310, 193 KEY_BUTTON_SELECT = 2311, 194 KEY_BUTTON_START = 2312, 195 KEY_BUTTON_MODE = 2313, 196 KEY_BUTTON_THUMBL = 2314, 197 KEY_BUTTON_THUMBR = 2315, 198 KEY_BUTTON_TRIGGER = 2401, 199 KEY_BUTTON_THUMB = 2402, 200 KEY_BUTTON_THUMB2 = 2403, 201 KEY_BUTTON_TOP = 2404, 202 KEY_BUTTON_TOP2 = 2405, 203 KEY_BUTTON_PINKIE = 2406, 204 KEY_BUTTON_BASE1 = 2407, 205 KEY_BUTTON_BASE2 = 2408, 206 KEY_BUTTON_BASE3 = 2409, 207 KEY_BUTTON_BASE4 = 2410, 208 KEY_BUTTON_BASE5 = 2411, 209 KEY_BUTTON_BASE6 = 2412, 210 KEY_BUTTON_BASE7 = 2413, 211 KEY_BUTTON_BASE8 = 2414, 212 KEY_BUTTON_BASE9 = 2415, 213 KEY_BUTTON_DEAD = 2416, 214 KEY_SLEEP = 2600, 215 KEY_ZENKAKU_HANKAKU = 2601, 216 KEY_102ND = 2602, 217 KEY_RO = 2603, 218 KEY_KATAKANA = 2604, 219 KEY_HIRAGANA = 2605, 220 KEY_HENKAN = 2606, 221 KEY_KATAKANA_HIRAGANA = 2607, 222 KEY_MUHENKAN = 2608, 223 KEY_LINEFEED = 2609, 224 KEY_MACRO = 2610, 225 KEY_NUMPAD_PLUSMINUS = 2611, 226 KEY_SCALE = 2612, 227 KEY_HANGUEL = 2613, 228 KEY_HANJA = 2614, 229 KEY_YEN = 2615, 230 KEY_STOP = 2616, 231 KEY_AGAIN = 2617, 232 KEY_PROPS = 2618, 233 KEY_UNDO = 2619, 234 KEY_COPY = 2620, 235 KEY_OPEN = 2621, 236 KEY_PASTE = 2622, 237 KEY_FIND = 2623, 238 KEY_CUT = 2624, 239 KEY_HELP = 2625, 240 KEY_CALC = 2626, 241 KEY_FILE = 2627, 242 KEY_BOOKMARKS = 2628, 243 KEY_NEXT = 2629, 244 KEY_PLAYPAUSE = 2630, 245 KEY_PREVIOUS = 2631, 246 KEY_STOPCD = 2632, 247 KEY_CONFIG = 2634, 248 KEY_REFRESH = 2635, 249 KEY_EXIT = 2636, 250 KEY_EDIT = 2637, 251 KEY_SCROLLUP = 2638, 252 KEY_SCROLLDOWN = 2639, 253 KEY_NEW = 2640, 254 KEY_REDO = 2641, 255 KEY_CLOSE = 2642, 256 KEY_PLAY = 2643, 257 KEY_BASSBOOST = 2644, 258 KEY_PRINT = 2645, 259 KEY_CHAT = 2646, 260 KEY_FINANCE = 2647, 261 KEY_CANCEL = 2648, 262 KEY_KBDILLUM_TOGGLE = 2649, 263 KEY_KBDILLUM_DOWN = 2650, 264 KEY_KBDILLUM_UP = 2651, 265 KEY_SEND = 2652, 266 KEY_REPLY = 2653, 267 KEY_FORWARDMAIL = 2654, 268 KEY_SAVE = 2655, 269 KEY_DOCUMENTS = 2656, 270 KEY_VIDEO_NEXT = 2657, 271 KEY_VIDEO_PREV = 2658, 272 KEY_BRIGHTNESS_CYCLE = 2659, 273 KEY_BRIGHTNESS_ZERO = 2660, 274 KEY_DISPLAY_OFF = 2661, 275 KEY_BTN_MISC = 2662, 276 KEY_GOTO = 2663, 277 KEY_INFO = 2664, 278 KEY_PROGRAM = 2665, 279 KEY_PVR = 2666, 280 KEY_SUBTITLE = 2667, 281 KEY_FULL_SCREEN = 2668, 282 KEY_KEYBOARD = 2669, 283 KEY_ASPECT_RATIO = 2670, 284 KEY_PC = 2671, 285 KEY_TV = 2672, 286 KEY_TV2 = 2673, 287 KEY_VCR = 2674, 288 KEY_VCR2 = 2675, 289 KEY_SAT = 2676, 290 KEY_CD = 2677, 291 KEY_TAPE = 2678, 292 KEY_TUNER = 2679, 293 KEY_PLAYER = 2680, 294 KEY_DVD = 2681, 295 KEY_AUDIO = 2682, 296 KEY_VIDEO = 2683, 297 KEY_MEMO = 2684, 298 KEY_CALENDAR = 2685, 299 KEY_RED = 2686, 300 KEY_GREEN = 2687, 301 KEY_YELLOW = 2688, 302 KEY_BLUE = 2689, 303 KEY_CHANNELUP = 2690, 304 KEY_CHANNELDOWN = 2691, 305 KEY_LAST = 2692, 306 KEY_RESTART = 2693, 307 KEY_SLOW = 2694, 308 KEY_SHUFFLE = 2695, 309 KEY_VIDEOPHONE = 2696, 310 KEY_GAMES = 2697, 311 KEY_ZOOMIN = 2698, 312 KEY_ZOOMOUT = 2699, 313 KEY_ZOOMRESET = 2700, 314 KEY_WORDPROCESSOR = 2701, 315 KEY_EDITOR = 2702, 316 KEY_SPREADSHEET = 2703, 317 KEY_GRAPHICSEDITOR = 2704, 318 KEY_PRESENTATION = 2705, 319 KEY_DATABASE = 2706, 320 KEY_NEWS = 2707, 321 KEY_VOICEMAIL = 2708, 322 KEY_ADDRESSBOOK = 2709, 323 KEY_MESSENGER = 2710, 324 KEY_BRIGHTNESS_TOGGLE = 2711, 325 KEY_SPELLCHECK = 2712, 326 KEY_COFFEE = 2713, 327 KEY_MEDIA_REPEAT = 2714, 328 KEY_IMAGES = 2715, 329 KEY_BUTTONCONFIG = 2716, 330 KEY_TASKMANAGER = 2717, 331 KEY_JOURNAL = 2718, 332 KEY_CONTROLPANEL = 2719, 333 KEY_APPSELECT = 2720, 334 KEY_SCREENSAVER = 2721, 335 KEY_ASSISTANT = 2722, 336 KEY_KBD_LAYOUT_NEXT = 2723, 337 KEY_BRIGHTNESS_MIN = 2724, 338 KEY_BRIGHTNESS_MAX = 2725, 339 KEY_KBDINPUTASSIST_PREV = 2726, 340 KEY_KBDINPUTASSIST_NEXT = 2727, 341 KEY_KBDINPUTASSIST_PREVGROUP = 2728, 342 KEY_KBDINPUTASSIST_NEXTGROUP = 2729, 343 KEY_KBDINPUTASSIST_ACCEPT = 2730, 344 KEY_KBDINPUTASSIST_CANCEL = 2731, 345 KEY_FRONT = 2800, 346 KEY_SETUP = 2801, 347 KEY_WAKEUP = 2802, 348 KEY_SENDFILE = 2803, 349 KEY_DELETEFILE = 2804, 350 KEY_XFER = 2805, 351 KEY_PROG1 = 2806, 352 KEY_PROG2 = 2807, 353 KEY_MSDOS = 2808, 354 KEY_SCREENLOCK = 2809, 355 KEY_DIRECTION_ROTATE_DISPLAY = 2810, 356 KEY_CYCLEWINDOWS = 2811, 357 KEY_COMPUTER = 2812, 358 KEY_EJECTCLOSECD = 2813, 359 KEY_ISO = 2814, 360 KEY_MOVE = 2815, 361 KEY_F13 = 2816, 362 KEY_F14 = 2817, 363 KEY_F15 = 2818, 364 KEY_F16 = 2819, 365 KEY_F17 = 2820, 366 KEY_F18 = 2821, 367 KEY_F19 = 2822, 368 KEY_F20 = 2823, 369 KEY_F21 = 2824, 370 KEY_F22 = 2825, 371 KEY_F23 = 2826, 372 KEY_F24 = 2827, 373 KEY_PROG3 = 2828, 374 KEY_PROG4 = 2829, 375 KEY_DASHBOARD = 2830, 376 KEY_SUSPEND = 2831, 377 KEY_HP = 2832, 378 KEY_SOUND = 2833, 379 KEY_QUESTION = 2834, 380 KEY_CONNECT = 2836, 381 KEY_SPORT = 2837, 382 KEY_SHOP = 2838, 383 KEY_ALTERASE = 2839, 384 KEY_SWITCHVIDEOMODE = 2841, 385 KEY_BATTERY = 2842, 386 KEY_BLUETOOTH = 2843, 387 KEY_WLAN = 2844, 388 KEY_UWB = 2845, 389 KEY_WWAN_WIMAX = 2846, 390 KEY_RFKILL = 2847, 391 KEY_CHANNEL = 3001, 392 KEY_BTN_0 = 3100, 393 KEY_BTN_1 = 3101, 394 KEY_BTN_2 = 3102, 395 KEY_BTN_3 = 3103, 396 KEY_BTN_4 = 3104, 397 KEY_BTN_5 = 3105, 398 KEY_BTN_6 = 3106, 399 KEY_BTN_7 = 3107, 400 KEY_BTN_8 = 3108, 401 KEY_BTN_9 = 3109, 402 KEY_BRL_DOT1 = 3201, 403 KEY_BRL_DOT2 = 3202, 404 KEY_BRL_DOT3 = 3203, 405 KEY_BRL_DOT4 = 3204, 406 KEY_BRL_DOT5 = 3205, 407 KEY_BRL_DOT6 = 3206, 408 KEY_BRL_DOT7 = 3207, 409 KEY_BRL_DOT8 = 3208, 410 KEY_BRL_DOT9 = 3209, 411 KEY_BRL_DOT10 = 3210, 412 // New End 413 KEY_LEFT_KNOB_ROLL_UP = 10001, 414 KEY_LEFT_KNOB_ROLL_DOWN = 10002, 415 KEY_LEFT_KNOB = 10003, 416 KEY_RIGHT_KNOB_ROLL_UP = 10004, 417 KEY_RIGHT_KNOB_ROLL_DOWN = 10005, 418 KEY_RIGHT_KNOB = 10006, 419 KEY_VOICE_SOURCE_SWITCH = 10007, 420 KEY_LAUNCHER_MENU = 10008, 421 422 TV_CONTROL_BACK = KEY_BACK, 423 TV_CONTROL_UP = KEY_DPAD_UP, 424 TV_CONTROL_DOWN = KEY_DPAD_DOWN, 425 TV_CONTROL_LEFT = KEY_DPAD_LEFT, 426 TV_CONTROL_RIGHT = KEY_DPAD_RIGHT, 427 TV_CONTROL_CENTER = KEY_DPAD_CENTER, 428 TV_CONTROL_ENTER = KEY_ENTER, 429 TV_CONTROL_MEDIA_PLAY = KEY_MEDIA_PLAY, 430 }; 431 432 enum class KeyAction : int32_t { 433 UNKNOWN = -1, 434 DOWN = 0, 435 UP = 1, 436 LONG_PRESS = 2, 437 CLICK = 3, 438 }; 439 440 enum class KeyIntention : int32_t { 441 INTENTION_UNKNOWN = -1, 442 INTENTION_UP = 1, 443 INTENTION_DOWN = 2, 444 INTENTION_LEFT = 3, 445 INTENTION_RIGHT = 4, 446 INTENTION_SELECT = 5, 447 INTENTION_ESCAPE = 6, 448 INTENTION_BACK = 7, 449 INTENTION_FORWARD = 8, 450 INTENTION_MENU = 9, 451 INTENTION_HOME = 10, 452 INTENTION_PAGE_UP = 11, 453 INTENTION_PAGE_DOWN = 12, 454 INTENTION_ZOOM_OUT = 13, 455 INTENTION_ZOOM_IN = 14, 456 457 INTENTION_MEDIA_PLAY_PAUSE = 100, 458 INTENTION_MEDIA_FAST_FORWARD = 101, 459 INTENTION_MEDIA_FAST_REWIND = 102, 460 INTENTION_MEDIA_FAST_PLAYBACK = 103, 461 INTENTION_MEDIA_NEXT = 104, 462 INTENTION_MEDIA_PREVIOUS = 105, 463 INTENTION_MEDIA_MUTE = 106, 464 INTENTION_VOLUTE_UP = 107, 465 INTENTION_VOLUTE_DOWN = 108, 466 467 INTENTION_CALL = 200, 468 INTENTION_ENDCALL = 201, 469 INTENTION_REJECTCALL = 202, 470 471 INTENTION_CAMERA = 300, 472 }; 473 474 constexpr int32_t ASCII_START_UPPER_CASE_LETTER = 65; 475 constexpr int32_t ASCII_START_LOWER_CASE_LETTER = 97; 476 477 ACE_FORCE_EXPORT const char* KeyToString(int32_t code); 478 479 struct KeyEvent final { 480 KeyEvent() = default; KeyEventfinal481 KeyEvent(KeyCode code, KeyAction action, std::vector<KeyCode> pressedCodes, int32_t repeatTime, TimeStamp timeStamp, 482 int32_t metaKey, int64_t deviceId, SourceType sourceType) 483 : code(code), action(action), pressedCodes(std::move(pressedCodes)), repeatTime(repeatTime), 484 timeStamp(timeStamp), metaKey(metaKey), deviceId(deviceId), sourceType(sourceType) 485 {} 486 KeyEvent(KeyCode code, KeyAction action, int32_t repeatTime = 0, int64_t timeStamp = 0, int64_t deviceId = 0, 487 SourceType sourceType = SourceType::KEYBOARD) 488 { 489 std::vector<KeyCode> pCodes; 490 std::chrono::milliseconds milliseconds(timeStamp); 491 TimeStamp time(milliseconds); 492 new (this) KeyEvent(code, action, pCodes, repeatTime, time, 0, deviceId, sourceType); 493 } 494 ~KeyEvent() = default; 495 SetTimeStampfinal496 void SetTimeStamp(int64_t timeInt) 497 { 498 std::chrono::milliseconds milliseconds(timeInt); 499 TimeStamp time(milliseconds); 500 timeStamp = time; 501 } 502 IsKeyfinal503 bool IsKey(std::vector<KeyCode> expectCodes) const 504 { 505 if (expectCodes.size() > pressedCodes.size() || pressedCodes.empty()) { 506 return false; 507 } 508 auto curExpectCode = expectCodes.rbegin(); 509 auto curPressedCode = pressedCodes.rbegin(); 510 while (curExpectCode != expectCodes.rend()) { 511 if (*curExpectCode != *curPressedCode) { 512 return false; 513 } 514 ++curExpectCode; 515 ++curPressedCode; 516 } 517 return true; 518 } 519 IsCtrlWithfinal520 bool IsCtrlWith(KeyCode expectCodes) const 521 { 522 return IsKey({ KeyCode::KEY_CTRL_LEFT, expectCodes }) || IsKey({ KeyCode::KEY_CTRL_RIGHT, expectCodes }); 523 } IsShiftWithfinal524 bool IsShiftWith(KeyCode expectCodes) const 525 { 526 return IsKey({ KeyCode::KEY_SHIFT_LEFT, expectCodes }) || IsKey({ KeyCode::KEY_SHIFT_RIGHT, expectCodes }); 527 } IsNumberKeyfinal528 bool IsNumberKey() const 529 { 530 return ((KeyCode::KEY_0 <= code && code <= KeyCode::KEY_9) || 531 (KeyCode::KEY_NUMPAD_0 <= code && code <= KeyCode::KEY_NUMPAD_9)); 532 } IsDirectionalKeyfinal533 bool IsDirectionalKey() const 534 { 535 return KeyCode::KEY_DPAD_UP <= code && code <= KeyCode::KEY_DPAD_RIGHT; 536 } IsLetterKeyfinal537 bool IsLetterKey() const 538 { 539 return (KeyCode::KEY_A <= code && code <= KeyCode::KEY_Z); 540 } IsCombinationKeyfinal541 bool IsCombinationKey() const 542 { 543 return pressedCodes.size() > 1; 544 } IsFunctionKeyfinal545 bool IsFunctionKey() const 546 { 547 return KeyCode::KEY_F1 <= code && code <= KeyCode::KEY_F12; 548 } IsEscapeKeyfinal549 bool IsEscapeKey() const 550 { 551 return KeyCode::KEY_ESCAPE == code; 552 } ConvertCodeToStringfinal553 std::string ConvertCodeToString() const 554 { 555 if (KeyCode::KEY_0 <= code && code <= KeyCode::KEY_9) { 556 return std::to_string(static_cast<int32_t>(code) - static_cast<int32_t>(KeyCode::KEY_0)); 557 } 558 if (KeyCode::KEY_NUMPAD_0 <= code && code <= KeyCode::KEY_NUMPAD_9) { 559 return std::to_string(static_cast<int32_t>(code) - static_cast<int32_t>(KeyCode::KEY_NUMPAD_0)); 560 } 561 if (IsLetterKey()) { 562 int32_t codeValue = static_cast<int32_t>(code) - static_cast<int32_t>(KeyCode::KEY_A); 563 auto shiftWithLetter = IsShiftWith(code); 564 if ((shiftWithLetter || enableCapsLock) && !(shiftWithLetter && enableCapsLock)) { 565 return std::string(1, static_cast<char>(codeValue + ASCII_START_UPPER_CASE_LETTER)); 566 } 567 return std::string(1, static_cast<char>(codeValue + ASCII_START_LOWER_CASE_LETTER)); 568 } 569 return ""; 570 } 571 572 std::string ConvertInputCodeToString() const; 573 574 KeyCode code { KeyCode::KEY_UNKNOWN }; 575 const char* key = ""; 576 KeyAction action { KeyAction::UNKNOWN }; 577 std::vector<KeyCode> pressedCodes; 578 // When the key is held down for a long period of time, it will be accumulated once in a while. 579 // Note that In the long press scene, you will receive a DOWN and an extra LONG_PRESS event. If you only want to 580 // identify the click event, you can use CLICK event. 581 int32_t repeatTime = 0; 582 TimeStamp timeStamp; 583 int32_t metaKey = 0; 584 int64_t deviceId = 0; 585 SourceType sourceType { SourceType::NONE }; 586 KeyIntention keyIntention { KeyIntention::INTENTION_UNKNOWN }; 587 bool enableCapsLock = false; 588 std::shared_ptr<MMI::KeyEvent> rawKeyEvent; 589 }; 590 591 class ACE_EXPORT KeyEventInfo : public BaseEventInfo { DECLARE_RELATIONSHIP_OF_CLASSES(KeyEventInfo,BaseEventInfo)592 DECLARE_RELATIONSHIP_OF_CLASSES(KeyEventInfo, BaseEventInfo) 593 594 public: 595 explicit KeyEventInfo(const KeyEvent& event) : BaseEventInfo("keyEvent") 596 { 597 keyCode_ = event.code; 598 keyText_ = event.key; 599 keyType_ = event.action; 600 keySource_ = event.sourceType; 601 keyIntention_ = event.keyIntention; 602 metaKey_ = event.metaKey; 603 SetDeviceId(event.deviceId); 604 SetTimeStamp(event.timeStamp); 605 }; 606 ~KeyEventInfo() override = default; 607 GetKeyType()608 KeyAction GetKeyType() const 609 { 610 return keyType_; 611 } GetKeyCode()612 KeyCode GetKeyCode() const 613 { 614 return keyCode_; 615 } GetKeyText()616 const char* GetKeyText() const 617 { 618 return keyText_; 619 } GetMetaKey()620 int32_t GetMetaKey() const 621 { 622 return metaKey_; 623 } GetKeySource()624 SourceType GetKeySource() const 625 { 626 return keySource_; 627 } GetKeyIntention()628 KeyIntention GetKeyIntention() const 629 { 630 return keyIntention_; 631 } 632 SetMetaKey(int32_t metaKey)633 void SetMetaKey(int32_t metaKey) 634 { 635 metaKey_ = metaKey; 636 } 637 private: 638 KeyCode keyCode_ = KeyCode::KEY_UNKNOWN; 639 const char* keyText_ = ""; 640 KeyAction keyType_ = KeyAction::UNKNOWN; 641 int32_t metaKey_ = 0; 642 SourceType keySource_ = SourceType::NONE; 643 KeyIntention keyIntention_ = KeyIntention::INTENTION_UNKNOWN; 644 }; 645 646 enum class BlurReason : int32_t { 647 FOCUS_SWITCH = 0, 648 WINDOW_BLUR = 1, 649 FRAME_DESTROY = 2, 650 }; 651 652 using OnKeyEventFunc = std::function<bool(const KeyEvent&)>; 653 using OnKeyCallbackFunc = std::function<void(KeyEventInfo&)>; 654 using OnFocusFunc = std::function<void()>; 655 using OnClearFocusStateFunc = std::function<void()>; 656 using OnPaintFocusStateFunc = std::function<bool()>; 657 using OnBlurFunc = std::function<void()>; 658 using OnBlurReasonFunc = std::function<void(BlurReason reason)>; 659 using OnPreFocusFunc = std::function<void()>; 660 } // namespace OHOS::Ace 661 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_KEY_EVENT_H 662