• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "key_event_napi.h"
17 
18 #include "util_napi.h"
19 #include "util_napi_value.h"
20 
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "KeyEventNapi"
23 
24 namespace OHOS {
25 namespace MMI {
CreateKeyEvent(napi_env env,const std::shared_ptr<KeyEvent> & in,napi_value & out)26 napi_status KeyEventNapi::CreateKeyEvent(napi_env env, const std::shared_ptr<KeyEvent> &in, napi_value &out)
27 {
28     CHKPR(in, napi_invalid_arg);
29     auto status = SetNameProperty(env, out, "action", in->GetKeyAction() - KeyEvent::KEY_ACTION_CANCEL);
30     CHKRR(status, "set action property", status);
31 
32     CHECK_RETURN(in->GetKeyItem(), "get key item", status);
33     auto keyItem = in->GetKeyItem();
34     status = SetNameProperty(env, out, "key", keyItem);
35     CHKRR(status, "set key property", status);
36 
37     status = SetNameProperty(env, out, "unicodeChar", in->GetKeyItem()->GetUnicode());
38     CHKRR(status, "set unicodeChar property", status);
39 
40     auto keyItems = in->GetKeyItems();
41     status = SetNameProperty(env, out, "keys", keyItems);
42     CHKRR(status, "set keys property", status);
43 
44     status = WriteKeyStatusToJs(env, in->GetPressedKeys(), out);
45     CHKRR(status, "set pressed key property", status);
46 
47     status = WriteFunctionKeyStatusToJs(env, in, out);
48     CHKRR(status, "set function key property", status);
49 
50     return napi_ok;
51 }
52 
GetKeyEvent(napi_env env,napi_value in,std::shared_ptr<KeyEvent> & out)53 napi_status KeyEventNapi::GetKeyEvent(napi_env env, napi_value in, std::shared_ptr<KeyEvent> &out)
54 {
55     napi_valuetype valueType = napi_undefined;
56     auto status = napi_typeof(env, in, &valueType);
57     CHECK_RETURN((status == napi_ok) && (valueType == napi_object), "object type invalid", status);
58 
59     KeyEvent::KeyItem item = GetNamePropertyKeyItem(env, in, "key");
60     out->SetKeyCode(item.GetKeyCode());
61 
62     uint32_t unicode = GetNamePropertyUint32(env, in, "unicodeChar");
63     if (auto keyItem = out->GetKeyItem()) {
64         keyItem->SetUnicode(unicode);
65     }
66 
67     int32_t keyAction = GetNamePropertyInt32(env, in, "action");
68     out->SetKeyAction(keyAction + KeyEvent::KEY_ACTION_CANCEL);
69 
70     std::vector<KeyEvent::KeyItem> keyItems = GetNamePropertyKeyItems(env, in, "keys");
71     for (const auto &keyItem : keyItems) {
72         out->AddKeyItem(keyItem);
73     }
74 
75     bool lock = GetNamePropertyBool(env, in, "capsLock");
76     out->SetFunctionKey(KeyEvent::CAPS_LOCK_FUNCTION_KEY, lock);
77     lock = GetNamePropertyBool(env, in, "numLock");
78     out->SetFunctionKey(KeyEvent::NUM_LOCK_FUNCTION_KEY, lock);
79     lock = GetNamePropertyBool(env, in, "scrollLock");
80     out->SetFunctionKey(KeyEvent::SCROLL_LOCK_FUNCTION_KEY, lock);
81 
82     return napi_ok;
83 }
84 
CreateKeyItem(napi_env env,const std::optional<KeyEvent::KeyItem> in,napi_value & out)85 napi_status KeyEventNapi::CreateKeyItem(napi_env env, const std::optional<KeyEvent::KeyItem> in, napi_value &out)
86 {
87     if (in.has_value()) {
88         auto status = SetNameProperty(env, out, "code", in->GetKeyCode());
89         CHKRR(status, "set code property", status);
90 
91         status = SetNameProperty(env, out, "pressedTime", in->GetDownTime());
92         CHKRR(status, "set pressedTime property", status);
93 
94         status = SetNameProperty(env, out, "deviceId", in->GetDeviceId());
95         CHKRR(status, "set deviceId property", status);
96     }
97 
98     return napi_ok;
99 }
100 
GetKeyItem(napi_env env,napi_value in,KeyEvent::KeyItem & out)101 napi_status KeyEventNapi::GetKeyItem(napi_env env, napi_value in, KeyEvent::KeyItem &out)
102 {
103     int32_t keyCode = GetNamePropertyInt32(env, in, "code");
104     out.SetKeyCode(keyCode);
105     int64_t downTime = GetNamePropertyInt64(env, in, "pressedTime");
106     out.SetDownTime(downTime);
107     int32_t deviceId = GetNamePropertyInt32(env, in, "deviceId");
108     out.SetDeviceId(deviceId);
109     return napi_ok;
110 }
111 
WriteKeyStatusToJs(napi_env env,const std::vector<int32_t> & pressedKeys,napi_value & out)112 napi_status KeyEventNapi::WriteKeyStatusToJs(napi_env env, const std::vector<int32_t> &pressedKeys, napi_value &out)
113 {
114     bool isExists = HasKeyCode(pressedKeys, KeyEvent::KEYCODE_CTRL_LEFT)
115                     || HasKeyCode(pressedKeys, KeyEvent::KEYCODE_CTRL_RIGHT);
116     auto status = SetNameProperty(env, out, "ctrlKey", isExists);
117     CHKRR(status, "set ctrlKey property", status);
118 
119     isExists = HasKeyCode(pressedKeys, KeyEvent::KEYCODE_ALT_LEFT)
120                || HasKeyCode(pressedKeys, KeyEvent::KEYCODE_ALT_RIGHT);
121     status = SetNameProperty(env, out, "altKey", isExists);
122     CHKRR(status, "set altKey property", status);
123 
124     isExists = HasKeyCode(pressedKeys, KeyEvent::KEYCODE_SHIFT_LEFT)
125                || HasKeyCode(pressedKeys, KeyEvent::KEYCODE_SHIFT_RIGHT);
126     status = SetNameProperty(env, out, "shiftKey", isExists);
127     CHKRR(status, "set shiftKey property", status);
128 
129     isExists = HasKeyCode(pressedKeys, KeyEvent::KEYCODE_META_LEFT)
130                || HasKeyCode(pressedKeys, KeyEvent::KEYCODE_META_RIGHT);
131     status = SetNameProperty(env, out, "logoKey", isExists);
132     CHKRR(status, "set logoKey property", status);
133 
134     isExists = HasKeyCode(pressedKeys, KeyEvent::KEYCODE_FN);
135     status = SetNameProperty(env, out, "fnKey", isExists);
136     CHKRR(status, "set fnKey property", status);
137 
138     return napi_ok;
139 }
140 
WriteFunctionKeyStatusToJs(napi_env env,const std::shared_ptr<KeyEvent> & in,napi_value & out)141 napi_status KeyEventNapi::WriteFunctionKeyStatusToJs(napi_env env, const std::shared_ptr<KeyEvent> &in, napi_value &out)
142 {
143     auto status = SetNameProperty(env, out, "capsLock", in->GetFunctionKey(KeyEvent::CAPS_LOCK_FUNCTION_KEY));
144     CHKRR(status, "set capsLock property", status);
145 
146     status = SetNameProperty(env, out, "numLock", in->GetFunctionKey(KeyEvent::NUM_LOCK_FUNCTION_KEY));
147     CHKRR(status, "set numLock property", status);
148 
149     status = SetNameProperty(env, out, "scrollLock", in->GetFunctionKey(KeyEvent::SCROLL_LOCK_FUNCTION_KEY));
150     CHKRR(status, "set scrollLock property", status);
151 
152     return napi_ok;
153 }
154 
HasKeyCode(const std::vector<int32_t> & pressedKeys,int32_t keyCode)155 bool KeyEventNapi::HasKeyCode(const std::vector<int32_t> &pressedKeys, int32_t keyCode)
156 {
157     return std::find(pressedKeys.begin(), pressedKeys.end(), keyCode) != pressedKeys.end();
158 }
159 } // namespace MMI
160 } // namespace OHOS