* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef KEY_EVENT_HANDLER_H
#define KEY_EVENT_HANDLER_H
#include <arkui/native_key_event.h>
#include <arkui/native_node.h>
#include <arkui/native_type.h>
#include <arkui/ui_input_event.h>
#include <string>
namespace KeyEventSample {
struct KeyEventCallbackData {
ArkUI_NodeHandle buttonNode;
std::string originalLabel;
std::string currentLabel;
int eventCount;
ArkUI_NodeEventType eventType;
};
class KeyEventHandler {
public:
static KeyEventHandler* GetInstance();
void Initialize();
ArkUI_NodeHandle CreateKeyEventNode();
ArkUI_NodeHandle CreateKeyEventButton(const std::string& label, ArkUI_NodeEventType eventType);
ArkUI_NodeHandle CreateEventPropagationDemo();
static void GlobalEventReceiver(ArkUI_NodeEvent* event);
static void HandleKeyEventWithUserData(ArkUI_NodeEvent* event);
static void UpdateButtonLabel(KeyEventCallbackData* callbackData, const ArkUI_UIInputEvent* inputEvent);
static void OnButtonClick(ArkUI_NodeEvent* event);
static void UpdateButtonFocusState(ArkUI_NodeHandle buttonNode, bool hasFocus);
static void DemoKeyEventSetConsumed(const ArkUI_UIInputEvent* event);
static void DemoKeyEventStopPropagation(const ArkUI_UIInputEvent* event);
static void UpdateParentContainerEventFeedback(ArkUI_NodeHandle containerNode, bool eventReceived);
std::string GetKeyEventInfo(const ArkUI_UIInputEvent* event);
std::string GetKeyCodeName(int32_t keyCode);
std::string GetKeyEventTypeName(int32_t eventType);
void UpdateKeyEventDisplay(const std::string& info);
ArkUI_NodeHandle GetDisplayNode()
{
return displayNode_;
}
static ArkUI_ContextHandle context_;
private:
KeyEventHandler() = default;
~KeyEventHandler() = default;
static KeyEventHandler* instance_;
ArkUI_NodeHandle displayNode_;
std::string lastKeyEventInfo_;
};
}
#endif