1 /* 2 * Copyright (c) 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_COMPONENTS_NG_PATTERNS_TEXT_FIELD_KEYEVENT_HANDLER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_FIELD_KEYEVENT_HANDLER_H 18 19 #include <algorithm> 20 #include <functional> 21 #include <string> 22 #include <unordered_map> 23 24 #include "base/utils/string_utils.h" 25 #include "core/components_ng/pattern/pattern.h" 26 #include "core/event/key_event.h" 27 28 namespace OHOS::Ace::NG { 29 const wchar_t UPPER_CASE_A = L'A'; 30 const wchar_t LOWER_CASE_A = L'a'; 31 const wchar_t CASE_0 = L'0'; 32 const std::wstring NUM_SYMBOLS = L")!@#$%^&*("; 33 const std::unordered_map<KeyCode, wchar_t> KEYBOARD_SYMBOLS = { 34 { KeyCode::KEY_GRAVE, L'`' }, 35 { KeyCode::KEY_MINUS, L'-' }, 36 { KeyCode::KEY_EQUALS, L'=' }, 37 { KeyCode::KEY_LEFT_BRACKET, L'[' }, 38 { KeyCode::KEY_RIGHT_BRACKET, L']' }, 39 { KeyCode::KEY_BACKSLASH, L'\\' }, 40 { KeyCode::KEY_SEMICOLON, L';' }, 41 { KeyCode::KEY_APOSTROPHE, L'\'' }, 42 { KeyCode::KEY_COMMA, L',' }, 43 { KeyCode::KEY_PERIOD, L'.' }, 44 { KeyCode::KEY_SLASH, L'/' }, 45 { KeyCode::KEY_SPACE, L' ' }, 46 { KeyCode::KEY_NUMPAD_DIVIDE, L'/' }, 47 { KeyCode::KEY_NUMPAD_MULTIPLY, L'*' }, 48 { KeyCode::KEY_NUMPAD_SUBTRACT, L'-' }, 49 { KeyCode::KEY_NUMPAD_ADD, L'+' }, 50 { KeyCode::KEY_NUMPAD_DOT, L'.' }, 51 { KeyCode::KEY_NUMPAD_COMMA, L',' }, 52 { KeyCode::KEY_NUMPAD_EQUALS, L'=' }, 53 }; 54 55 static const std::unordered_map<KeyCode, wchar_t> SHIFT_KEYBOARD_SYMBOLS = { 56 { KeyCode::KEY_GRAVE, L'~' }, 57 { KeyCode::KEY_MINUS, L'_' }, 58 { KeyCode::KEY_EQUALS, L'+' }, 59 { KeyCode::KEY_LEFT_BRACKET, L'{' }, 60 { KeyCode::KEY_RIGHT_BRACKET, L'}' }, 61 { KeyCode::KEY_BACKSLASH, L'|' }, 62 { KeyCode::KEY_SEMICOLON, L':' }, 63 { KeyCode::KEY_APOSTROPHE, L'\"' }, 64 { KeyCode::KEY_COMMA, L'<' }, 65 { KeyCode::KEY_PERIOD, L'>' }, 66 { KeyCode::KEY_SLASH, L'?' }, 67 }; 68 69 class KeyEventHandler : public AceType { 70 DECLARE_ACE_TYPE(KeyEventHandler, AceType) 71 72 public: 73 KeyEventHandler() = default; 74 ~KeyEventHandler() = default; 75 UpdateWeakPattern(const WeakPtr<Pattern> & pattern)76 void UpdateWeakPattern(const WeakPtr<Pattern>& pattern) 77 { 78 weakPattern_ = pattern; 79 } 80 ClearClient()81 void ClearClient() 82 { 83 weakPattern_ = nullptr; 84 } 85 86 bool HandleKeyEvent(const KeyEvent& keyEvent); 87 88 private: 89 bool HandleShiftPressedEvent(const KeyEvent& event); 90 bool HandleDirectionalKey(const KeyEvent& keyEvent); 91 bool HandleDirectionalMoveKey(const KeyEvent& keyEvent); 92 void ParseAppendValue(KeyCode keycode, std::string& appendElement); 93 bool IsCtrlShiftWith(const KeyEvent& keyEvent, const KeyCode expectCode); 94 WeakPtr<Pattern> weakPattern_; 95 ACE_DISALLOW_COPY_AND_MOVE(KeyEventHandler); 96 }; 97 98 } // namespace OHOS::Ace::NG 99 100 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_FIELD_KEYEVENT_HANDLER_H