• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<double>("timestamp", static_cast<double>(event.GetTimeStamp().time_since_epoch().count()));
33     keyEventObj->SetPropertyObject("stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
34     keyEventObj->SetPropertyObject("getModifierKeyState",
35         JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
36     keyEventObj->SetProperty<int32_t>("intentionCode", static_cast<int32_t>(event.GetKeyIntention()));
37     keyEventObj->Wrap<KeyEventInfo>(&event);
38     return keyEventObj;
39 }
40 
Execute(OHOS::Ace::KeyEventInfo & event)41 void JsKeyFunction::Execute(OHOS::Ace::KeyEventInfo& event)
42 {
43     JSRef<JSVal> param = JSRef<JSObject>::Cast(createKeyEvent(event));
44     JsFunction::ExecuteJS(1, &param);
45 }
46 
ExecuteWithValue(OHOS::Ace::KeyEventInfo & event)47 JSRef<JSVal> JsKeyFunction::ExecuteWithValue(OHOS::Ace::KeyEventInfo& event)
48 {
49     JSRef<JSVal> param = JSRef<JSObject>::Cast(createKeyEvent(event));
50     return JsFunction::ExecuteJS(1, &param);
51 }
52 
53 } // namespace OHOS::Ace::Framework
54