122736c2fSopenharmony_ci/* 222736c2fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 322736c2fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 422736c2fSopenharmony_ci * you may not use this file except in compliance with the License. 522736c2fSopenharmony_ci * You may obtain a copy of the License at 622736c2fSopenharmony_ci * 722736c2fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 822736c2fSopenharmony_ci * 922736c2fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1022736c2fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1122736c2fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1222736c2fSopenharmony_ci * See the License for the specific language governing permissions and 1322736c2fSopenharmony_ci * limitations under the License. 1422736c2fSopenharmony_ci */ 1522736c2fSopenharmony_ci 1622736c2fSopenharmony_ci#include "keyboard_event.h" 1722736c2fSopenharmony_ci 1822736c2fSopenharmony_ci#include <global.h> 1922736c2fSopenharmony_ci#include <memory> 2022736c2fSopenharmony_ci 2122736c2fSopenharmony_ci#include "global.h" 2222736c2fSopenharmony_ci#include "input_event_callback.h" 2322736c2fSopenharmony_ci#include "key_event.h" 2422736c2fSopenharmony_ci 2522736c2fSopenharmony_cinamespace OHOS { 2622736c2fSopenharmony_cinamespace MiscServices { 2722736c2fSopenharmony_ciusing namespace MMI; 2822736c2fSopenharmony_ciKeyboardEvent &KeyboardEvent::GetInstance() 2922736c2fSopenharmony_ci{ 3022736c2fSopenharmony_ci static KeyboardEvent keyboardEvent; 3122736c2fSopenharmony_ci return keyboardEvent; 3222736c2fSopenharmony_ci} 3322736c2fSopenharmony_ci 3422736c2fSopenharmony_ciint32_t KeyboardEvent::AddKeyEventMonitor(KeyHandle handle) 3522736c2fSopenharmony_ci{ 3622736c2fSopenharmony_ci IMSA_HILOGI("KeyboardEvent::AddKeyEventMonitor start."); 3722736c2fSopenharmony_ci std::shared_ptr<InputEventCallback> callback = std::make_shared<InputEventCallback>(); 3822736c2fSopenharmony_ci callback->SetKeyHandle(handle); 3922736c2fSopenharmony_ci int32_t monitorId = InputManager::GetInstance()->AddMonitor([callback](std::shared_ptr<MMI::KeyEvent> keyEvent) { 4022736c2fSopenharmony_ci if (callback == nullptr) { 4122736c2fSopenharmony_ci IMSA_HILOGE("callback is nullptr!"); 4222736c2fSopenharmony_ci return; 4322736c2fSopenharmony_ci } 4422736c2fSopenharmony_ci callback->OnInputEvent(keyEvent); 4522736c2fSopenharmony_ci }); 4622736c2fSopenharmony_ci if (monitorId < 0) { 4722736c2fSopenharmony_ci IMSA_HILOGE("add monitor failed, id: %{public}d!", monitorId); 4822736c2fSopenharmony_ci return ErrorCode::ERROR_SUBSCRIBE_KEYBOARD_EVENT; 4922736c2fSopenharmony_ci } 5022736c2fSopenharmony_ci IMSA_HILOGD("add monitor success, id: %{public}d.", monitorId); 5122736c2fSopenharmony_ci 5222736c2fSopenharmony_ci CombinationKeyCallBack combinationKeyCallBack = [callback](std::shared_ptr<MMI::KeyEvent> keyEvent) { 5322736c2fSopenharmony_ci if (callback == nullptr) { 5422736c2fSopenharmony_ci IMSA_HILOGE("callback is nullptr!"); 5522736c2fSopenharmony_ci return; 5622736c2fSopenharmony_ci } 5722736c2fSopenharmony_ci callback->TriggerSwitch(); 5822736c2fSopenharmony_ci }; 5922736c2fSopenharmony_ci SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_META_LEFT, MMI::KeyEvent::KEYCODE_SPACE, combinationKeyCallBack); 6022736c2fSopenharmony_ci SubscribeCombinationKey(MMI::KeyEvent::KEYCODE_META_RIGHT, MMI::KeyEvent::KEYCODE_SPACE, combinationKeyCallBack); 6122736c2fSopenharmony_ci return ErrorCode::NO_ERROR; 6222736c2fSopenharmony_ci} 6322736c2fSopenharmony_ci 6422736c2fSopenharmony_civoid KeyboardEvent::SubscribeCombinationKey(int32_t preKey, int32_t finalKey, CombinationKeyCallBack callback) 6522736c2fSopenharmony_ci{ 6622736c2fSopenharmony_ci std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>(); 6722736c2fSopenharmony_ci std::set<int32_t> preKeys; 6822736c2fSopenharmony_ci preKeys.insert(preKey); 6922736c2fSopenharmony_ci keyOption->SetPreKeys(preKeys); 7022736c2fSopenharmony_ci keyOption->SetFinalKey(finalKey); 7122736c2fSopenharmony_ci keyOption->SetFinalKeyDown(true); 7222736c2fSopenharmony_ci // 0 means press delay 0 ms 7322736c2fSopenharmony_ci keyOption->SetFinalKeyDownDuration(0); 7422736c2fSopenharmony_ci int32_t subscribeId = InputManager::GetInstance()->SubscribeKeyEvent(keyOption, callback); 7522736c2fSopenharmony_ci if (subscribeId < 0) { 7622736c2fSopenharmony_ci IMSA_HILOGE("failed to SubscribeKeyEvent, id: %{public}d preKey: %{public}d.", subscribeId, preKey); 7722736c2fSopenharmony_ci } 7822736c2fSopenharmony_ci} 7922736c2fSopenharmony_ci} // namespace MiscServices 8022736c2fSopenharmony_ci} // namespace OHOS