1 /*
2 * Copyright (c) 2021 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 "frameworks/bridge/declarative_frontend/engine/functions/js_key_function.h"
17 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
18
19 namespace OHOS::Ace::Framework {
20
createKeyEvent(KeyEventInfo & event)21 JSRef<JSObject> JsKeyFunction::createKeyEvent(KeyEventInfo& event)
22 {
23 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
24 objectTemplate->SetInternalFieldCount(1);
25 JSRef<JSObject> keyEventObj = objectTemplate->NewInstance();
26 keyEventObj->SetProperty<int32_t>("type", static_cast<int32_t>(event.GetKeyType()));
27 keyEventObj->SetProperty<int32_t>("keyCode", static_cast<int32_t>(event.GetKeyCode()));
28 keyEventObj->SetProperty<const char*>("keyText", event.GetKeyText());
29 keyEventObj->SetProperty<int32_t>("keySource", static_cast<int32_t>(event.GetKeySource()));
30 keyEventObj->SetProperty<int64_t>("deviceId", event.GetDeviceId());
31 keyEventObj->SetProperty<int32_t>("metaKey", event.GetMetaKey());
32 keyEventObj->SetProperty<uint32_t>("unicode", event.GetUnicode());
33 keyEventObj->SetProperty<double>("timestamp", static_cast<double>(event.GetTimeStamp().time_since_epoch().count()));
34 keyEventObj->SetPropertyObject("stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
35 keyEventObj->SetPropertyObject("getModifierKeyState",
36 JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
37 keyEventObj->SetProperty<int32_t>("intentionCode", static_cast<int32_t>(event.GetKeyIntention()));
38 keyEventObj->Wrap<KeyEventInfo>(&event);
39 return keyEventObj;
40 }
41
Execute(OHOS::Ace::KeyEventInfo & event)42 void JsKeyFunction::Execute(OHOS::Ace::KeyEventInfo& event)
43 {
44 JSRef<JSVal> param = JSRef<JSObject>::Cast(createKeyEvent(event));
45 JsFunction::ExecuteJS(1, ¶m);
46 }
47
ExecuteWithValue(OHOS::Ace::KeyEventInfo & event)48 JSRef<JSVal> JsKeyFunction::ExecuteWithValue(OHOS::Ace::KeyEventInfo& event)
49 {
50 JSRef<JSVal> param = JSRef<JSObject>::Cast(createKeyEvent(event));
51 return JsFunction::ExecuteJS(1, ¶m);
52 }
53
54 } // namespace OHOS::Ace::Framework
55