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 #ifndef KEY_EVENT_NAPI_H 17 #define KEY_EVENT_NAPI_H 18 19 #include "key_event.h" 20 #include "napi/native_node_api.h" 21 22 namespace OHOS { 23 namespace MMI { 24 class KeyEventNapi { 25 public: 26 /** 27 * @brief Write KeyEvent into a JS object. 28 * @param env Indicates the environment that the Node-API call is invoked under. 29 * @param in Indicates the KeyEvent object from which data will be read. 30 * @param out Indicates the JS object into which data will be written. 31 * @return Returns <b>napi_ok</b> if the data is successfully written; returns error status otherwise. 32 * @since 10 33 */ 34 static napi_status CreateKeyEvent(napi_env env, const std::shared_ptr<KeyEvent> &in, napi_value &out); 35 36 /** 37 * @brief Read KeyEvent from a JS object. 38 * @param env Indicates the environment that the Node-API call is invoked under. 39 * @param in Indicates the JS object from which data will be read. 40 * @param out Indicates the KeyEvent object into which data will be written. 41 * @return Returns <b>napi_ok</b> if the data is successfully written; returns error status otherwise. 42 * @since 10 43 */ 44 static napi_status GetKeyEvent(napi_env env, napi_value in, std::shared_ptr<KeyEvent> &out); 45 46 /** 47 * @brief Write KeyItem into a JS object. 48 * @param env Indicates the environment that the Node-API call is invoked under. 49 * @param in Indicates the KeyItem object from which data will be read. 50 * @param out Indicates the JS object into which data will be written. 51 * @return Returns <b>napi_ok</b> if the data is successfully written; returns error status otherwise. 52 * @since 10 53 */ 54 static napi_status CreateKeyItem(napi_env env, const std::optional<KeyEvent::KeyItem> in, napi_value &out); 55 56 /** 57 * @brief Read KeyItem from a JS object. 58 * @param in Indicates the JS object from which data will be read. 59 * @param out Indicates the KeyItem object into which data will be written. 60 * @return Returns <b>napi_ok</b> if the data is successfully written; returns error status otherwise. 61 * @since 10 62 */ 63 static napi_status GetKeyItem(napi_env env, napi_value in, KeyEvent::KeyItem &out); 64 65 private: 66 static napi_status WriteKeyStatusToJs(napi_env env, const std::vector<int32_t> &pressedKeys, napi_value &out); 67 static napi_status WriteFunctionKeyStatusToJs(napi_env env, const std::shared_ptr<KeyEvent> &in, napi_value &out); 68 static bool HasKeyCode(const std::vector<int32_t> &pressedKeys, int32_t keyCode); 69 }; 70 } // namespace MMI 71 } // namespace OHOS 72 #endif // KEY_EVENT_NAPI_H 73