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