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