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 #include "interfaces/native/native_key_event.h"
17 #include "interfaces/native/node/event_converter.h"
18 #include "interfaces/native/node/node_model.h"
19 #include "interfaces/native/event/ui_input_event_impl.h"
20 #include "frameworks/core/interfaces/arkoala/arkoala_api.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
OH_ArkUI_KeyEvent_GetType(const ArkUI_UIInputEvent * event)26 ArkUI_KeyEventType OH_ArkUI_KeyEvent_GetType(const ArkUI_UIInputEvent *event)
27 {
28 if (!event) {
29 return static_cast<ArkUI_KeyEventType>(-1);
30 }
31 const auto* keyEvent = reinterpret_cast<ArkUIKeyEvent*>(event->inputEvent);
32 if (!keyEvent) {
33 return static_cast<ArkUI_KeyEventType>(-1);
34 }
35 auto result = static_cast<ArkUI_KeyEventType>(keyEvent->type);
36 return result;
37 }
38
OH_ArkUI_KeyEvent_GetKeyCode(const ArkUI_UIInputEvent * event)39 int32_t OH_ArkUI_KeyEvent_GetKeyCode(const ArkUI_UIInputEvent *event)
40 {
41 if (!event) {
42 return -1;
43 }
44 const auto* keyEvent = reinterpret_cast<ArkUIKeyEvent*>(event->inputEvent);
45 if (!keyEvent) {
46 return -1;
47 }
48 auto result = static_cast<int32_t>(keyEvent->keyCode);
49 return result;
50 }
51
OH_ArkUI_KeyEvent_GetKeyText(const ArkUI_UIInputEvent * event)52 const char* OH_ArkUI_KeyEvent_GetKeyText(const ArkUI_UIInputEvent *event)
53 {
54 if (!event) {
55 return nullptr;
56 }
57 const auto* keyEvent = reinterpret_cast<ArkUIKeyEvent*>(event->inputEvent);
58 if (!keyEvent) {
59 return nullptr;
60 }
61 return keyEvent->keyText;
62 }
63
OH_ArkUI_KeyEvent_GetKeySource(const ArkUI_UIInputEvent * event)64 ArkUI_KeySourceType OH_ArkUI_KeyEvent_GetKeySource(const ArkUI_UIInputEvent *event)
65 {
66 if (!event) {
67 return static_cast<ArkUI_KeySourceType>(-1);
68 }
69 const auto* keyEvent = reinterpret_cast<ArkUIKeyEvent*>(event->inputEvent);
70 if (!keyEvent) {
71 return static_cast<ArkUI_KeySourceType>(-1);
72 }
73 auto result = static_cast<ArkUI_KeySourceType>(keyEvent->keySource);
74 return result;
75 }
76
OH_ArkUI_KeyEvent_StopPropagation(const ArkUI_UIInputEvent * event,bool stopPropagation)77 void OH_ArkUI_KeyEvent_StopPropagation(const ArkUI_UIInputEvent *event, bool stopPropagation)
78 {
79 if (!event) {
80 return;
81 }
82 auto* keyEvent = reinterpret_cast<ArkUIKeyEvent*>(event->inputEvent);
83 if (!keyEvent) {
84 return;
85 }
86 keyEvent->stopPropagation = stopPropagation;
87 }
88
OH_ArkUI_KeyEvent_GetKeyIntensionCode(const ArkUI_UIInputEvent * event)89 ArkUI_KeyIntension OH_ArkUI_KeyEvent_GetKeyIntensionCode(const ArkUI_UIInputEvent *event)
90 {
91 if (!event) {
92 return static_cast<ArkUI_KeyIntension>(-1);
93 }
94 const auto* keyEvent = reinterpret_cast<ArkUIKeyEvent*>(event->inputEvent);
95 if (!keyEvent) {
96 return static_cast<ArkUI_KeyIntension>(-1);
97 }
98 auto result = static_cast<ArkUI_KeyIntension>(keyEvent->intentionCode);
99 return result;
100 }
101
OH_ArkUI_KeyEvent_GetUnicode(const ArkUI_UIInputEvent * event)102 uint32_t OH_ArkUI_KeyEvent_GetUnicode(const ArkUI_UIInputEvent *event)
103 {
104 if (!event) {
105 return 0;
106 }
107 const auto* keyEvent = reinterpret_cast<ArkUIKeyEvent*>(event->inputEvent);
108 if (!keyEvent) {
109 return 0;
110 }
111 auto result = static_cast<uint32_t>(keyEvent->unicode);
112 return result;
113 }
114
OH_ArkUI_KeyEvent_SetConsumed(const ArkUI_UIInputEvent * event,bool isConsumed)115 void OH_ArkUI_KeyEvent_SetConsumed(const ArkUI_UIInputEvent *event, bool isConsumed)
116 {
117 if (!event) {
118 return;
119 }
120 auto* keyEvent = reinterpret_cast<ArkUIKeyEvent*>(event->inputEvent);
121 if (!keyEvent) {
122 return;
123 }
124 keyEvent->isConsumed = isConsumed;
125 }
126
OH_ArkUI_KeyEvent_Dispatch(ArkUI_NodeHandle node,const ArkUI_UIInputEvent * event)127 void OH_ArkUI_KeyEvent_Dispatch(ArkUI_NodeHandle node, const ArkUI_UIInputEvent* event)
128 {
129 if (!node || !event) {
130 return;
131 }
132 auto* keyEvent = reinterpret_cast<ArkUIKeyEvent*>(event->inputEvent);
133 if (!keyEvent) {
134 return;
135 }
136 auto fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
137 fullImpl->getNodeModifiers()->getCommonModifier()->dispatchKeyEvent(node->uiNodeHandle, keyEvent);
138 }
139 #ifdef __cplusplus
140 };
141 #endif