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