• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_COMMON_KEY_EVENT_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_KEY_EVENT_MANAGER_H
18 
19 #include <list>
20 
21 #include "core/components/common/layout/constants.h"
22 #include "core/event/key_event.h"
23 #include "core/event/focus_axis_event.h"
24 
25 namespace OHOS::Ace {
26 namespace NG {
27 class FrameNode;
28 class FocusView;
29 
30 class KeyEventManager : public virtual AceType {
31     DECLARE_ACE_TYPE(KeyEventManager, AceType);
32 
33 public:
34     KeyEventManager() = default;
35     ~KeyEventManager() override = default;
36 
37     void AddKeyboardShortcutNode(const WeakPtr<NG::FrameNode>& node);
38     void DelKeyboardShortcutNode(int32_t nodeId);
39     uint8_t GetKeyboardShortcutKeys(const std::vector<ModifierKey>& keys);
40     bool IsSameKeyboardShortcutNode(const std::string& value, uint8_t keys);
41     bool DispatchKeyboardShortcut(const KeyEvent& event);
42     void ReDispatch(KeyEvent& keyEvent);
43     void SetIsKeyConsumed(bool value);
SetPressedKeyCodes(const std::vector<KeyCode> & pressedKeyCodes)44     void SetPressedKeyCodes(const std::vector<KeyCode>& pressedKeyCodes)
45     {
46         pressedKeyCodes_ = pressedKeyCodes;
47     }
IsTabJustTriggerOnKeyEvent()48     bool IsTabJustTriggerOnKeyEvent() const
49     {
50         return isTabJustTriggerOnKeyEvent_;
51     }
52     // to be obsoleted
IsKeyInPressed(KeyCode tarCode)53     bool IsKeyInPressed(KeyCode tarCode) const
54     {
55         return std::any_of(pressedKeyCodes_.begin(), pressedKeyCodes_.end(),
56             [tarCode](const KeyCode& code) { return code == tarCode; });
57     }
58 
59 protected:
60     virtual int32_t GetInstanceId() = 0;
61 
62     bool OnKeyEvent(const KeyEvent& event);
63     bool OnFocusAxisEvent(const FocusAxisEvent& event);
64 
65 private:
66     // Distribute the key event to the corresponding root node. If the root node is not processed, return false and the
67     // platform will handle it.
68     bool DispatchKeyEventNG(const KeyEvent& event, const RefPtr<NG::FrameNode>& focusNode);
69     bool DispatchTabIndexEventNG(const KeyEvent& event, const RefPtr<NG::FrameNode>& mainView);
70     bool TriggerKeyEventDispatch(const KeyEvent& event);
71     bool IsSystemKeyboardShortcut(const std::string& value, uint8_t keys);
72     bool DispatchTabKey(const KeyEvent& event, const RefPtr<FocusView>& curFocusView);
73     bool IsSkipShortcutAndFocusMove();
74 
75     bool isKeyConsumed_ = false;
76     bool isTabJustTriggerOnKeyEvent_ = false;
77     std::list<WeakPtr<NG::FrameNode>> keyboardShortcutNode_;
78     std::vector<KeyCode> pressedKeyCodes_;
79 };
80 } // namespace NG
81 } // namespace OHOS::Ace
82 #endif