1 /* 2 * Copyright (c) 2025 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 #ifndef FRAMEWORKS_INPUTMETHOD_KEY_EVENT_RESULT_HANDLER_H 16 #define FRAMEWORKS_INPUTMETHOD_KEY_EVENT_RESULT_HANDLER_H 17 #include <map> 18 #include <mutex> 19 20 #include "key_event.h" 21 namespace OHOS { 22 namespace MiscServices { 23 using KeyEventCallback = std::function<void(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed)>; 24 struct KeyEventCbInfo { 25 std::shared_ptr<MMI::KeyEvent> keyEvent{ nullptr }; 26 KeyEventCallback callback{ nullptr }; 27 }; 28 class KeyEventResultHandler { 29 public: 30 uint64_t AddKeyEventCbInfo(const KeyEventCbInfo &cbInfo); 31 void RemoveKeyEventCbInfo(uint64_t cbId); 32 void HandleKeyEventResult(uint64_t cbId, bool consumeResult); 33 void ClearKeyEventCbInfo(); 34 35 private: 36 int32_t GetKeyEventCbInfo(uint64_t cbId, KeyEventCbInfo &info); 37 uint64_t GenerateKeyEventCbId(); 38 std::mutex keyEventCbHandlersMutex_; 39 std::map<uint64_t, KeyEventCbInfo> keyEventCbHandlers_; 40 std::atomic<uint64_t> maxCbId_{1}; 41 }; 42 } // namespace MiscServices 43 } // namespace OHOS 44 #endif // FRAMEWORKS_INPUTMETHOD_KEY_EVENT_RESULT_HANDLER_H