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
18 #include "base/log/log.h"
19
20 namespace OHOS::Ace::Framework {
21
createKeyEvent(KeyEventInfo & event)22 JSRef<JSObject> JsKeyFunction::createKeyEvent(KeyEventInfo& event)
23 {
24 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
25 objectTemplate->SetInternalFieldCount(1);
26 JSRef<JSObject> keyEventObj = objectTemplate->NewInstance();
27 keyEventObj->SetProperty<int32_t>("type", static_cast<int32_t>(event.GetKeyType()));
28 keyEventObj->SetProperty<int32_t>("keyCode", static_cast<int32_t>(event.GetKeyCode()));
29 keyEventObj->SetProperty<const char*>("keyText", event.GetKeyText());
30 keyEventObj->SetProperty<int32_t>("keySource", static_cast<int32_t>(event.GetKeySource()));
31 keyEventObj->SetProperty<int64_t>("deviceId", event.GetDeviceId());
32 keyEventObj->SetProperty<int32_t>("metaKey", event.GetMetaKey());
33 keyEventObj->SetProperty<float>("timestamp", event.GetTimeStamp().time_since_epoch().count());
34 keyEventObj->SetPropertyObject("stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
35 keyEventObj->Wrap<KeyEventInfo>(&event);
36 return keyEventObj;
37 }
38
Execute(OHOS::Ace::KeyEventInfo & event)39 void JsKeyFunction::Execute(OHOS::Ace::KeyEventInfo& event)
40 {
41 LOGD("JsKeyFunction: eventType[%{public}d]", event.GetKeyType());
42 JSRef<JSVal> param = JSRef<JSObject>::Cast(createKeyEvent(event));
43 JsFunction::ExecuteJS(1, ¶m);
44 }
45
ExecuteWithValue(OHOS::Ace::KeyEventInfo & event)46 JSRef<JSVal> JsKeyFunction::ExecuteWithValue(OHOS::Ace::KeyEventInfo& event)
47 {
48 JSRef<JSVal> param = JSRef<JSObject>::Cast(createKeyEvent(event));
49 return JsFunction::ExecuteJS(1, ¶m);
50 }
51
52 } // namespace OHOS::Ace::Framework
53