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().c_str());
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->SetProperty<bool>("isNumLockOn", event.GetNumLock());
39 keyEventObj->SetProperty<bool>("isCapsLockOn", event.GetCapsLock());
40 keyEventObj->SetProperty<bool>("isScrollLockOn", event.GetScrollLock());
41 keyEventObj->Wrap<KeyEventInfo>(&event);
42 return keyEventObj;
43 }
44
Execute(OHOS::Ace::KeyEventInfo & event)45 void JsKeyFunction::Execute(OHOS::Ace::KeyEventInfo& event)
46 {
47 JSRef<JSVal> param = JSRef<JSObject>::Cast(createKeyEvent(event));
48 JsFunction::ExecuteJS(1, ¶m);
49 }
50
ExecuteWithValue(OHOS::Ace::KeyEventInfo & event)51 JSRef<JSVal> JsKeyFunction::ExecuteWithValue(OHOS::Ace::KeyEventInfo& event)
52 {
53 JSRef<JSVal> param = JSRef<JSObject>::Cast(createKeyEvent(event));
54 return JsFunction::ExecuteJS(1, ¶m);
55 }
56
57 } // namespace OHOS::Ace::Framework
58