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