From 7ba7a3dfb7cb31051d789935794e0fcfc247b51b Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 7 Jan 2023 17:00:39 +0800 Subject: [PATCH 1/8] fix combination mechanics Signed-off-by: zhaolinglan --- .../keyboard/include/combination_key.h | 32 +++++++++++++ .../keyboard/include/input_event_callback.h | 1 + .../adapter/keyboard/include/keyboard_event.h | 7 +-- .../adapter/keyboard/src/combination_key.cpp | 47 +++++++++++++++++++ .../keyboard/src/input_event_callback.cpp | 16 ++++++- .../include/input_method_system_ability.h | 4 +- services/src/input_method_system_ability.cpp | 22 ++++----- 7 files changed, 106 insertions(+), 23 deletions(-) create mode 100644 services/adapter/keyboard/include/combination_key.h create mode 100644 services/adapter/keyboard/src/combination_key.cpp diff --git a/services/adapter/keyboard/include/combination_key.h b/services/adapter/keyboard/include/combination_key.h new file mode 100644 index 00000000..2d867194 --- /dev/null +++ b/services/adapter/keyboard/include/combination_key.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INPUTMETHOD_IMF_COMBINATION_KEY_H +#define INPUTMETHOD_IMF_COMBINATION_KEY_H + +#include + + +namespace OHOS { +namespace MiscServices { +enum class CombinationKeyFunction : uint32_t { SWITCH_LANGUAGE = 0, SWITCH_MODE, SWITCH_IME }; +class CombinationKey { +public: + static bool IsMatch(CombinationKeyFunction combinationKey, uint32_t state, uint8_t lastPressedKey); +}; +} +} // namespace OHOS + +#endif //INPUTMETHOD_IMF_COMBINATION_KEY_H diff --git a/services/adapter/keyboard/include/input_event_callback.h b/services/adapter/keyboard/include/input_event_callback.h index 1612fbe3..7179099f 100644 --- a/services/adapter/keyboard/include/input_event_callback.h +++ b/services/adapter/keyboard/include/input_event_callback.h @@ -36,6 +36,7 @@ public: private: KeyHandle keyHandler_ = nullptr; static uint32_t keyState_; + static int32_t lastPressedKey_; }; } // namespace MiscServices } // namespace OHOS diff --git a/services/adapter/keyboard/include/keyboard_event.h b/services/adapter/keyboard/include/keyboard_event.h index 2ad7df15..1c6faadf 100644 --- a/services/adapter/keyboard/include/keyboard_event.h +++ b/services/adapter/keyboard/include/keyboard_event.h @@ -18,14 +18,13 @@ #include #include -#include #include "global.h" #include "key_event.h" namespace OHOS { namespace MiscServices { -using KeyHandle = std::function; +using KeyHandle = std::function; class KeyboardEvent { public: @@ -36,10 +35,6 @@ public: static constexpr uint8_t CTRL_LEFT_MASK = 0X1 << 2; static constexpr uint8_t CTRL_RIGHT_MASK = 0X1 << 3; static constexpr uint8_t CAPS_MASK = 0X1 << 4; - static constexpr bool IS_KEYS_DOWN(uint32_t state, uint8_t mask) - { - return state == mask; - } private: static constexpr int32_t PRESS_KEY_DELAY_MS = 200; diff --git a/services/adapter/keyboard/src/combination_key.cpp b/services/adapter/keyboard/src/combination_key.cpp new file mode 100644 index 00000000..f3ef6702 --- /dev/null +++ b/services/adapter/keyboard/src/combination_key.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "combination_key.h" + +#include +#include + +#include "keyboard_event.h" + +namespace OHOS { +namespace MiscServices { +const std::map> COMBINATION_KEY_MAP{ + { CombinationKeyFunction::SWITCH_LANGUAGE, { KeyboardEvent::SHIFT_LEFT_MASK | KeyboardEvent::SHIFT_RIGHT_MASK } }, + { CombinationKeyFunction::SWITCH_IME, { KeyboardEvent::SHIFT_LEFT_MASK | KeyboardEvent::SHIFT_RIGHT_MASK, + KeyboardEvent::CTRL_LEFT_MASK | KeyboardEvent::CTRL_RIGHT_MASK } }, + { CombinationKeyFunction::SWITCH_MODE, { KeyboardEvent::CAPS_MASK } }, +}; + +bool CombinationKey::IsMatch(CombinationKeyFunction combinationKey, uint32_t state, uint8_t lastPressedKey) +{ + auto keys = COMBINATION_KEY_MAP.find(combinationKey); + bool isLastKeyMatch = false; + for (const auto &key : keys->second) { + if ((state & key) == 0) { + return false; + } + if ((lastPressedKey & key) != 0) { + isLastKeyMatch = true; + } + } + return isLastKeyMatch; +} +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/services/adapter/keyboard/src/input_event_callback.cpp b/services/adapter/keyboard/src/input_event_callback.cpp index 3d213405..1e7ea7aa 100644 --- a/services/adapter/keyboard/src/input_event_callback.cpp +++ b/services/adapter/keyboard/src/input_event_callback.cpp @@ -20,6 +20,7 @@ namespace OHOS { namespace MiscServices { uint32_t InputEventCallback::keyState_ = static_cast(0); +int32_t InputEventCallback::lastPressedKey_ = MMI::KeyEvent::KEYCODE_UNKNOWN; const std::map MASK_MAP{ { MMI::KeyEvent::KEYCODE_SHIFT_LEFT, KeyboardEvent::SHIFT_LEFT_MASK }, { MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, KeyboardEvent::SHIFT_RIGHT_MASK }, @@ -35,6 +36,9 @@ void InputEventCallback::OnInputEvent(std::shared_ptr keyEvent) c auto currKey = MASK_MAP.find(keyCode); if (currKey == MASK_MAP.end()) { IMSA_HILOGD("key code unknown"); + if (keyAction == MMI::KeyEvent::KEY_ACTION_DOWN) { + lastPressedKey_ = MMI::KeyEvent::KEYCODE_UNKNOWN; + } return; } IMSA_HILOGD("keyCode: %{public}d, keyAction: %{public}d", keyCode, keyAction); @@ -42,12 +46,20 @@ void InputEventCallback::OnInputEvent(std::shared_ptr keyEvent) c if (keyAction == MMI::KeyEvent::KEY_ACTION_DOWN) { IMSA_HILOGD("key %{public}d pressed down", keyCode); keyState_ = static_cast(keyState_ | currKey->second); + lastPressedKey_ = keyCode; + if (keyCode == MMI::KeyEvent::KEYCODE_CAPS_LOCK) { + if (keyHandler_ != nullptr) { + int32_t ret = keyHandler_(keyState_, KeyboardEvent::CAPS_MASK); + IMSA_HILOGI("handle key event ret: %{public}d", ret); + } + } return; } if (keyAction == MMI::KeyEvent::KEY_ACTION_UP) { - if (keyHandler_ != nullptr) { - int32_t ret = keyHandler_(keyState_); + auto lastPressedKey = MASK_MAP.find(lastPressedKey_); + if (keyHandler_ != nullptr && lastPressedKey != MASK_MAP.end()) { + int32_t ret = keyHandler_(keyState_, lastPressedKey->second); IMSA_HILOGI("handle key event ret: %{public}d", ret); } keyState_ = static_cast(keyState_ & ~currKey->second); diff --git a/services/include/input_method_system_ability.h b/services/include/input_method_system_ability.h index 657ac53e..482b4496 100644 --- a/services/include/input_method_system_ability.h +++ b/services/include/input_method_system_ability.h @@ -23,12 +23,12 @@ #include "ability_manager_interface.h" #include "application_info.h" #include "bundle_mgr_proxy.h" +#include "combination_key.h" #include "event_handler.h" #include "input_method_status.h" #include "input_method_system_ability_stub.h" #include "inputmethod_dump.h" #include "inputmethod_trace.h" -#include "keyboard_event.h" #include "peruser_session.h" #include "system_ability.h" @@ -124,7 +124,7 @@ private: using CompareHandler = std::function; SubProperty FindSubPropertyByCompare(const std::string &bundleName, CompareHandler compare); SubProperty GetExtends(const std::vector &metaData); - int32_t SwitchByCombinationKey(uint32_t state); + int32_t SwitchByCombinationKey(uint32_t state, uint8_t lastPressedKey); int32_t QueryImeInfos(int32_t userId, std::vector &infos); }; diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index f31c1480..0157933a 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -431,7 +431,7 @@ int32_t InputMethodSystemAbility::ListDisabledInputMethod(int32_t userId, std::v return ErrorCode::ERROR_NULL_POINTER; } for (auto iter = props.begin(); iter != props.end();) { - if (iter->name == filter->name && iter->id == filter->id) { + if (iter->name == filter->name) { iter = props.erase(iter); continue; } @@ -988,7 +988,7 @@ SubProperty InputMethodSystemAbility::FindSubPropertyByCompare(const std::string return {}; } -int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state) +int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state, uint8_t lastPressedKey) { IMSA_HILOGI("InputMethodSystemAbility::SwitchByCombinationKey"); auto current = GetCurrentInputMethodSubtype(); @@ -996,8 +996,8 @@ int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state) IMSA_HILOGE("GetCurrentInputMethodSubtype failed"); return ErrorCode::ERROR_EX_NULL_POINTER; } - if (KeyboardEvent::IS_KEYS_DOWN(state, KeyboardEvent::CAPS_MASK)) { - IMSA_HILOGI("CAPS press"); + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_MODE, state, lastPressedKey)) { + IMSA_HILOGI("switch mode"); auto target = current->mode == "upper" ? FindSubPropertyByCompare(current->id, [¤t](const SubProperty &property) { return property.mode == "lower"; }) : @@ -1005,9 +1005,8 @@ int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state) [¤t](const SubProperty &property) { return property.mode == "upper"; }); return SwitchInputMethod(target.id, target.label); } - if (KeyboardEvent::IS_KEYS_DOWN(state, KeyboardEvent::SHIFT_LEFT_MASK) || - KeyboardEvent::IS_KEYS_DOWN(state, KeyboardEvent::SHIFT_RIGHT_MASK)) { - IMSA_HILOGI("SHIFT press"); + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_LANGUAGE, state, lastPressedKey)) { + IMSA_HILOGI("switch language"); auto target = current->language == "chinese" ? FindSubPropertyByCompare(current->id, [¤t](const SubProperty &property) { return property.language == "english"; }) : @@ -1015,11 +1014,8 @@ int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state) [¤t](const SubProperty &property) { return property.language == "chinese"; }); return SwitchInputMethod(target.id, target.label); } - if (KeyboardEvent::IS_KEYS_DOWN(state, KeyboardEvent::CTRL_LEFT_MASK | KeyboardEvent::SHIFT_LEFT_MASK) || - KeyboardEvent::IS_KEYS_DOWN(state, KeyboardEvent::CTRL_LEFT_MASK | KeyboardEvent::SHIFT_RIGHT_MASK) || - KeyboardEvent::IS_KEYS_DOWN(state, KeyboardEvent::CTRL_RIGHT_MASK | KeyboardEvent::SHIFT_LEFT_MASK) || - KeyboardEvent::IS_KEYS_DOWN(state, KeyboardEvent::CTRL_RIGHT_MASK | KeyboardEvent::SHIFT_RIGHT_MASK)) { - IMSA_HILOGI("CTRL_SHIFT press"); + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_IME, state, lastPressedKey)) { + IMSA_HILOGI("switch ime"); std::vector props = {}; auto ret = ListProperty(MAIN_USER_ID, props); if (ret != ErrorCode::NO_ERROR) { @@ -1040,7 +1036,7 @@ int32_t InputMethodSystemAbility::InitKeyEventMonitor() { IMSA_HILOGI("InputMethodSystemAbility::InitKeyEventMonitor"); bool ret = ImCommonEventManager::GetInstance()->SubscribeKeyboardEvent( - [this](uint32_t keyCode) { return SwitchByCombinationKey(keyCode); }); + [this](uint32_t keyCode, uint8_t lastPressedKey) { return SwitchByCombinationKey(keyCode, lastPressedKey); }); return ret ? ErrorCode::NO_ERROR : ErrorCode::ERROR_SERVICE_START_FAILED; } } // namespace MiscServices From 0aac7e1b33ae45c1b19a4d411a68c30924bdc97e Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 7 Jan 2023 17:25:19 +0800 Subject: [PATCH 2/8] modify gn files Signed-off-by: zhaolinglan --- services/adapter/keyboard/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/services/adapter/keyboard/BUILD.gn b/services/adapter/keyboard/BUILD.gn index cac9aca9..3a6ce21f 100644 --- a/services/adapter/keyboard/BUILD.gn +++ b/services/adapter/keyboard/BUILD.gn @@ -24,6 +24,7 @@ config("inputmethod_adapter_native_config") { ohos_static_library("keboard_event_static") { sources = [ + "src/combination_key.cpp", "src/input_event_callback.cpp", "src/keyboard_event.cpp", ] From 19ff718975d410e7538e45e29d097d882690fc7f Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 7 Jan 2023 21:00:32 +0800 Subject: [PATCH 3/8] fix combination mechanism Signed-off-by: zhaolinglan --- .../keyboard/include/combination_key.h | 10 +++--- .../keyboard/include/input_event_callback.h | 2 +- .../adapter/keyboard/include/keyboard_event.h | 2 +- .../adapter/keyboard/src/combination_key.cpp | 23 +++++++------- .../keyboard/src/input_event_callback.cpp | 19 ++++++------ .../include/input_method_system_ability.h | 3 +- services/src/input_method_system_ability.cpp | 31 ++++++++++--------- 7 files changed, 46 insertions(+), 44 deletions(-) diff --git a/services/adapter/keyboard/include/combination_key.h b/services/adapter/keyboard/include/combination_key.h index 2d867194..aa00f079 100644 --- a/services/adapter/keyboard/include/combination_key.h +++ b/services/adapter/keyboard/include/combination_key.h @@ -18,15 +18,15 @@ #include - namespace OHOS { namespace MiscServices { -enum class CombinationKeyFunction : uint32_t { SWITCH_LANGUAGE = 0, SWITCH_MODE, SWITCH_IME }; +enum class CombinationKeyFunction { SWITCH_LANGUAGE = 0, SWITCH_MODE, SWITCH_IME }; + class CombinationKey { public: - static bool IsMatch(CombinationKeyFunction combinationKey, uint32_t state, uint8_t lastPressedKey); + static bool IsMatch(CombinationKeyFunction combinationKey, uint32_t state, int32_t pressedKeyNum); }; -} +} // namespace MiscServices } // namespace OHOS -#endif //INPUTMETHOD_IMF_COMBINATION_KEY_H +#endif // INPUTMETHOD_IMF_COMBINATION_KEY_H diff --git a/services/adapter/keyboard/include/input_event_callback.h b/services/adapter/keyboard/include/input_event_callback.h index 7179099f..6f249357 100644 --- a/services/adapter/keyboard/include/input_event_callback.h +++ b/services/adapter/keyboard/include/input_event_callback.h @@ -36,7 +36,7 @@ public: private: KeyHandle keyHandler_ = nullptr; static uint32_t keyState_; - static int32_t lastPressedKey_; + static bool isKeyHandled_; }; } // namespace MiscServices } // namespace OHOS diff --git a/services/adapter/keyboard/include/keyboard_event.h b/services/adapter/keyboard/include/keyboard_event.h index 1c6faadf..42421e3d 100644 --- a/services/adapter/keyboard/include/keyboard_event.h +++ b/services/adapter/keyboard/include/keyboard_event.h @@ -24,7 +24,7 @@ namespace OHOS { namespace MiscServices { -using KeyHandle = std::function; +using KeyHandle = std::function; class KeyboardEvent { public: diff --git a/services/adapter/keyboard/src/combination_key.cpp b/services/adapter/keyboard/src/combination_key.cpp index f3ef6702..6a689834 100644 --- a/services/adapter/keyboard/src/combination_key.cpp +++ b/services/adapter/keyboard/src/combination_key.cpp @@ -15,33 +15,34 @@ #include "combination_key.h" -#include #include #include "keyboard_event.h" namespace OHOS { namespace MiscServices { -const std::map> COMBINATION_KEY_MAP{ +const std::map> COMBINATION_KEY_MAP{ { CombinationKeyFunction::SWITCH_LANGUAGE, { KeyboardEvent::SHIFT_LEFT_MASK | KeyboardEvent::SHIFT_RIGHT_MASK } }, { CombinationKeyFunction::SWITCH_IME, { KeyboardEvent::SHIFT_LEFT_MASK | KeyboardEvent::SHIFT_RIGHT_MASK, KeyboardEvent::CTRL_LEFT_MASK | KeyboardEvent::CTRL_RIGHT_MASK } }, { CombinationKeyFunction::SWITCH_MODE, { KeyboardEvent::CAPS_MASK } }, }; -bool CombinationKey::IsMatch(CombinationKeyFunction combinationKey, uint32_t state, uint8_t lastPressedKey) +bool CombinationKey::IsMatch(CombinationKeyFunction combinationKey, uint32_t state, int32_t pressedKeyNum) { - auto keys = COMBINATION_KEY_MAP.find(combinationKey); - bool isLastKeyMatch = false; - for (const auto &key : keys->second) { - if ((state & key) == 0) { + auto expectedKeys = COMBINATION_KEY_MAP.find(combinationKey); + if (expectedKeys == COMBINATION_KEY_MAP.end()) { + return false; + } + if (expectedKeys->second.size() != pressedKeyNum) { + return false; + } + for (const auto &key : expectedKeys->second) { + if ((key & state) == 0) { return false; } - if ((lastPressedKey & key) != 0) { - isLastKeyMatch = true; - } } - return isLastKeyMatch; + return true; } } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/services/adapter/keyboard/src/input_event_callback.cpp b/services/adapter/keyboard/src/input_event_callback.cpp index 1e7ea7aa..0e2cefca 100644 --- a/services/adapter/keyboard/src/input_event_callback.cpp +++ b/services/adapter/keyboard/src/input_event_callback.cpp @@ -20,7 +20,7 @@ namespace OHOS { namespace MiscServices { uint32_t InputEventCallback::keyState_ = static_cast(0); -int32_t InputEventCallback::lastPressedKey_ = MMI::KeyEvent::KEYCODE_UNKNOWN; +bool InputEventCallback::isKeyHandled_ = false; const std::map MASK_MAP{ { MMI::KeyEvent::KEYCODE_SHIFT_LEFT, KeyboardEvent::SHIFT_LEFT_MASK }, { MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, KeyboardEvent::SHIFT_RIGHT_MASK }, @@ -33,12 +33,11 @@ void InputEventCallback::OnInputEvent(std::shared_ptr keyEvent) c { auto keyCode = keyEvent->GetKeyCode(); auto keyAction = keyEvent->GetKeyAction(); + int32_t pressedKeyNums = keyEvent->GetPressedKeys().size(); auto currKey = MASK_MAP.find(keyCode); if (currKey == MASK_MAP.end()) { IMSA_HILOGD("key code unknown"); - if (keyAction == MMI::KeyEvent::KEY_ACTION_DOWN) { - lastPressedKey_ = MMI::KeyEvent::KEYCODE_UNKNOWN; - } + keyState_ = 0; return; } IMSA_HILOGD("keyCode: %{public}d, keyAction: %{public}d", keyCode, keyAction); @@ -46,22 +45,24 @@ void InputEventCallback::OnInputEvent(std::shared_ptr keyEvent) c if (keyAction == MMI::KeyEvent::KEY_ACTION_DOWN) { IMSA_HILOGD("key %{public}d pressed down", keyCode); keyState_ = static_cast(keyState_ | currKey->second); - lastPressedKey_ = keyCode; if (keyCode == MMI::KeyEvent::KEYCODE_CAPS_LOCK) { if (keyHandler_ != nullptr) { - int32_t ret = keyHandler_(keyState_, KeyboardEvent::CAPS_MASK); + int32_t ret = keyHandler_(keyState_, pressedKeyNums); IMSA_HILOGI("handle key event ret: %{public}d", ret); } + isKeyHandled_ = true; + return; } + isKeyHandled_ = false; return; } if (keyAction == MMI::KeyEvent::KEY_ACTION_UP) { - auto lastPressedKey = MASK_MAP.find(lastPressedKey_); - if (keyHandler_ != nullptr && lastPressedKey != MASK_MAP.end()) { - int32_t ret = keyHandler_(keyState_, lastPressedKey->second); + if (keyHandler_ != nullptr && !isKeyHandled_) { + int32_t ret = keyHandler_(keyState_, pressedKeyNums); IMSA_HILOGI("handle key event ret: %{public}d", ret); } + isKeyHandled_ = true; keyState_ = static_cast(keyState_ & ~currKey->second); } } diff --git a/services/include/input_method_system_ability.h b/services/include/input_method_system_ability.h index 482b4496..5eab8916 100644 --- a/services/include/input_method_system_ability.h +++ b/services/include/input_method_system_ability.h @@ -23,7 +23,6 @@ #include "ability_manager_interface.h" #include "application_info.h" #include "bundle_mgr_proxy.h" -#include "combination_key.h" #include "event_handler.h" #include "input_method_status.h" #include "input_method_system_ability_stub.h" @@ -124,7 +123,7 @@ private: using CompareHandler = std::function; SubProperty FindSubPropertyByCompare(const std::string &bundleName, CompareHandler compare); SubProperty GetExtends(const std::vector &metaData); - int32_t SwitchByCombinationKey(uint32_t state, uint8_t lastPressedKey); + int32_t SwitchByCombinationKey(uint32_t state, int32_t pressedKeyNum); int32_t QueryImeInfos(int32_t userId, std::vector &infos); }; diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 0157933a..4a8ccfe2 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -23,6 +23,7 @@ #include "ability_manager_interface.h" #include "application_info.h" #include "bundle_mgr_proxy.h" +#include "combination_key.h" #include "common_event_support.h" #include "errors.h" #include "global.h" @@ -988,7 +989,7 @@ SubProperty InputMethodSystemAbility::FindSubPropertyByCompare(const std::string return {}; } -int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state, uint8_t lastPressedKey) +int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state, int32_t pressedKeyNum) { IMSA_HILOGI("InputMethodSystemAbility::SwitchByCombinationKey"); auto current = GetCurrentInputMethodSubtype(); @@ -996,25 +997,25 @@ int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state, uint8_t IMSA_HILOGE("GetCurrentInputMethodSubtype failed"); return ErrorCode::ERROR_EX_NULL_POINTER; } - if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_MODE, state, lastPressedKey)) { + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_MODE, state, pressedKeyNum)) { IMSA_HILOGI("switch mode"); - auto target = current->mode == "upper" ? - FindSubPropertyByCompare(current->id, - [¤t](const SubProperty &property) { return property.mode == "lower"; }) : - FindSubPropertyByCompare(current->id, - [¤t](const SubProperty &property) { return property.mode == "upper"; }); + auto target = current->mode == "upper" + ? FindSubPropertyByCompare(current->id, + [¤t](const SubProperty &property) { return property.mode == "lower"; }) + : FindSubPropertyByCompare(current->id, + [¤t](const SubProperty &property) { return property.mode == "upper"; }); return SwitchInputMethod(target.id, target.label); } - if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_LANGUAGE, state, lastPressedKey)) { + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_LANGUAGE, state, pressedKeyNum)) { IMSA_HILOGI("switch language"); - auto target = current->language == "chinese" ? - FindSubPropertyByCompare(current->id, - [¤t](const SubProperty &property) { return property.language == "english"; }) : - FindSubPropertyByCompare(current->id, - [¤t](const SubProperty &property) { return property.language == "chinese"; }); + auto target = current->language == "chinese" + ? FindSubPropertyByCompare(current->id, + [¤t](const SubProperty &property) { return property.language == "english"; }) + : FindSubPropertyByCompare(current->id, + [¤t](const SubProperty &property) { return property.language == "chinese"; }); return SwitchInputMethod(target.id, target.label); } - if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_IME, state, lastPressedKey)) { + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_IME, state, pressedKeyNum)) { IMSA_HILOGI("switch ime"); std::vector props = {}; auto ret = ListProperty(MAIN_USER_ID, props); @@ -1036,7 +1037,7 @@ int32_t InputMethodSystemAbility::InitKeyEventMonitor() { IMSA_HILOGI("InputMethodSystemAbility::InitKeyEventMonitor"); bool ret = ImCommonEventManager::GetInstance()->SubscribeKeyboardEvent( - [this](uint32_t keyCode, uint8_t lastPressedKey) { return SwitchByCombinationKey(keyCode, lastPressedKey); }); + [this](uint32_t keyCode, int32_t pressedKeyNum) { return SwitchByCombinationKey(keyCode, pressedKeyNum); }); return ret ? ErrorCode::NO_ERROR : ErrorCode::ERROR_SERVICE_START_FAILED; } } // namespace MiscServices From 3b5d2f8d8387b9184b49f940090e86e68aac279a Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 7 Jan 2023 21:37:18 +0800 Subject: [PATCH 4/8] modify code Signed-off-by: zhaolinglan --- services/adapter/keyboard/src/combination_key.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/adapter/keyboard/src/combination_key.cpp b/services/adapter/keyboard/src/combination_key.cpp index 6a689834..936745f3 100644 --- a/services/adapter/keyboard/src/combination_key.cpp +++ b/services/adapter/keyboard/src/combination_key.cpp @@ -30,15 +30,19 @@ const std::map> COMBINATION_KEY_MAP bool CombinationKey::IsMatch(CombinationKeyFunction combinationKey, uint32_t state, int32_t pressedKeyNum) { + IMSA_HILOGD("combinationKey: %{public}d", combinationKey); auto expectedKeys = COMBINATION_KEY_MAP.find(combinationKey); if (expectedKeys == COMBINATION_KEY_MAP.end()) { + IMSA_HILOGD("known key function"); return false; } if (expectedKeys->second.size() != pressedKeyNum) { + IMSA_HILOGD("pressed key num not match"); return false; } for (const auto &key : expectedKeys->second) { if ((key & state) == 0) { + IMSA_HILOGD("key not match"); return false; } } From e6afc3212723f21d6005f1784b1ab27ae961a4af Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 7 Jan 2023 21:59:07 +0800 Subject: [PATCH 5/8] modify code Signed-off-by: zhaolinglan --- services/adapter/keyboard/src/combination_key.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/adapter/keyboard/src/combination_key.cpp b/services/adapter/keyboard/src/combination_key.cpp index 936745f3..fe9dfa0f 100644 --- a/services/adapter/keyboard/src/combination_key.cpp +++ b/services/adapter/keyboard/src/combination_key.cpp @@ -30,14 +30,15 @@ const std::map> COMBINATION_KEY_MAP bool CombinationKey::IsMatch(CombinationKeyFunction combinationKey, uint32_t state, int32_t pressedKeyNum) { - IMSA_HILOGD("combinationKey: %{public}d", combinationKey); + IMSA_HILOGD("combinationKey: %{public}d, state: %{public}d", combinationKey, state); auto expectedKeys = COMBINATION_KEY_MAP.find(combinationKey); if (expectedKeys == COMBINATION_KEY_MAP.end()) { IMSA_HILOGD("known key function"); return false; } if (expectedKeys->second.size() != pressedKeyNum) { - IMSA_HILOGD("pressed key num not match"); + IMSA_HILOGD("pressed key num not match, size = %{public}d, pressedKeyNum = %{public}d", + expectedKeys->second.size(), pressedKeyNum); return false; } for (const auto &key : expectedKeys->second) { From 0a84fd284bc868106bc9b976444f6e4c61c2aaf4 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 7 Jan 2023 22:25:36 +0800 Subject: [PATCH 6/8] modify code Signed-off-by: zhaolinglan --- services/adapter/keyboard/src/input_event_callback.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/adapter/keyboard/src/input_event_callback.cpp b/services/adapter/keyboard/src/input_event_callback.cpp index 0e2cefca..a3e1c8f3 100644 --- a/services/adapter/keyboard/src/input_event_callback.cpp +++ b/services/adapter/keyboard/src/input_event_callback.cpp @@ -59,7 +59,7 @@ void InputEventCallback::OnInputEvent(std::shared_ptr keyEvent) c if (keyAction == MMI::KeyEvent::KEY_ACTION_UP) { if (keyHandler_ != nullptr && !isKeyHandled_) { - int32_t ret = keyHandler_(keyState_, pressedKeyNums); + int32_t ret = keyHandler_(keyState_, pressedKeyNums + 1); IMSA_HILOGI("handle key event ret: %{public}d", ret); } isKeyHandled_ = true; From 7e08873650ec6733e2256c0d389cd6d63aa5c953 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 7 Jan 2023 23:10:01 +0800 Subject: [PATCH 7/8] modify code Signed-off-by: zhaolinglan --- .../keyboard/include/combination_key.h | 2 +- .../adapter/keyboard/include/keyboard_event.h | 2 +- .../adapter/keyboard/src/combination_key.cpp | 29 ++++++++----------- .../keyboard/src/input_event_callback.cpp | 5 ++-- .../include/input_method_system_ability.h | 2 +- services/src/input_method_system_ability.cpp | 8 ++--- 6 files changed, 21 insertions(+), 27 deletions(-) diff --git a/services/adapter/keyboard/include/combination_key.h b/services/adapter/keyboard/include/combination_key.h index aa00f079..e2cbc00f 100644 --- a/services/adapter/keyboard/include/combination_key.h +++ b/services/adapter/keyboard/include/combination_key.h @@ -24,7 +24,7 @@ enum class CombinationKeyFunction { SWITCH_LANGUAGE = 0, SWITCH_MODE, SWITCH_IME class CombinationKey { public: - static bool IsMatch(CombinationKeyFunction combinationKey, uint32_t state, int32_t pressedKeyNum); + static bool IsMatch(CombinationKeyFunction combinationKey, uint32_t state); }; } // namespace MiscServices } // namespace OHOS diff --git a/services/adapter/keyboard/include/keyboard_event.h b/services/adapter/keyboard/include/keyboard_event.h index 42421e3d..5e450348 100644 --- a/services/adapter/keyboard/include/keyboard_event.h +++ b/services/adapter/keyboard/include/keyboard_event.h @@ -24,7 +24,7 @@ namespace OHOS { namespace MiscServices { -using KeyHandle = std::function; +using KeyHandle = std::function; class KeyboardEvent { public: diff --git a/services/adapter/keyboard/src/combination_key.cpp b/services/adapter/keyboard/src/combination_key.cpp index fe9dfa0f..c78b2cae 100644 --- a/services/adapter/keyboard/src/combination_key.cpp +++ b/services/adapter/keyboard/src/combination_key.cpp @@ -16,19 +16,25 @@ #include "combination_key.h" #include +#include #include "keyboard_event.h" namespace OHOS { namespace MiscServices { -const std::map> COMBINATION_KEY_MAP{ - { CombinationKeyFunction::SWITCH_LANGUAGE, { KeyboardEvent::SHIFT_LEFT_MASK | KeyboardEvent::SHIFT_RIGHT_MASK } }, - { CombinationKeyFunction::SWITCH_IME, { KeyboardEvent::SHIFT_LEFT_MASK | KeyboardEvent::SHIFT_RIGHT_MASK, - KeyboardEvent::CTRL_LEFT_MASK | KeyboardEvent::CTRL_RIGHT_MASK } }, +const std::map> COMBINATION_KEY_MAP{ + { CombinationKeyFunction::SWITCH_LANGUAGE, { KeyboardEvent::SHIFT_LEFT_MASK, KeyboardEvent::SHIFT_RIGHT_MASK } }, + { CombinationKeyFunction::SWITCH_IME, + { + KeyboardEvent::SHIFT_LEFT_MASK | KeyboardEvent::CTRL_LEFT_MASK, + KeyboardEvent::SHIFT_RIGHT_MASK | KeyboardEvent::CTRL_RIGHT_MASK, + KeyboardEvent::SHIFT_LEFT_MASK | KeyboardEvent::CTRL_RIGHT_MASK, + KeyboardEvent::SHIFT_RIGHT_MASK | KeyboardEvent::CTRL_LEFT_MASK, + } }, { CombinationKeyFunction::SWITCH_MODE, { KeyboardEvent::CAPS_MASK } }, }; -bool CombinationKey::IsMatch(CombinationKeyFunction combinationKey, uint32_t state, int32_t pressedKeyNum) +bool CombinationKey::IsMatch(CombinationKeyFunction combinationKey, uint32_t state) { IMSA_HILOGD("combinationKey: %{public}d, state: %{public}d", combinationKey, state); auto expectedKeys = COMBINATION_KEY_MAP.find(combinationKey); @@ -36,18 +42,7 @@ bool CombinationKey::IsMatch(CombinationKeyFunction combinationKey, uint32_t sta IMSA_HILOGD("known key function"); return false; } - if (expectedKeys->second.size() != pressedKeyNum) { - IMSA_HILOGD("pressed key num not match, size = %{public}d, pressedKeyNum = %{public}d", - expectedKeys->second.size(), pressedKeyNum); - return false; - } - for (const auto &key : expectedKeys->second) { - if ((key & state) == 0) { - IMSA_HILOGD("key not match"); - return false; - } - } - return true; + return expectedKeys->second.find(state) != expectedKeys->second.end(); } } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/services/adapter/keyboard/src/input_event_callback.cpp b/services/adapter/keyboard/src/input_event_callback.cpp index a3e1c8f3..fdae509e 100644 --- a/services/adapter/keyboard/src/input_event_callback.cpp +++ b/services/adapter/keyboard/src/input_event_callback.cpp @@ -33,7 +33,6 @@ void InputEventCallback::OnInputEvent(std::shared_ptr keyEvent) c { auto keyCode = keyEvent->GetKeyCode(); auto keyAction = keyEvent->GetKeyAction(); - int32_t pressedKeyNums = keyEvent->GetPressedKeys().size(); auto currKey = MASK_MAP.find(keyCode); if (currKey == MASK_MAP.end()) { IMSA_HILOGD("key code unknown"); @@ -47,7 +46,7 @@ void InputEventCallback::OnInputEvent(std::shared_ptr keyEvent) c keyState_ = static_cast(keyState_ | currKey->second); if (keyCode == MMI::KeyEvent::KEYCODE_CAPS_LOCK) { if (keyHandler_ != nullptr) { - int32_t ret = keyHandler_(keyState_, pressedKeyNums); + int32_t ret = keyHandler_(keyState_); IMSA_HILOGI("handle key event ret: %{public}d", ret); } isKeyHandled_ = true; @@ -59,7 +58,7 @@ void InputEventCallback::OnInputEvent(std::shared_ptr keyEvent) c if (keyAction == MMI::KeyEvent::KEY_ACTION_UP) { if (keyHandler_ != nullptr && !isKeyHandled_) { - int32_t ret = keyHandler_(keyState_, pressedKeyNums + 1); + int32_t ret = keyHandler_(keyState_); IMSA_HILOGI("handle key event ret: %{public}d", ret); } isKeyHandled_ = true; diff --git a/services/include/input_method_system_ability.h b/services/include/input_method_system_ability.h index 5eab8916..9c23de78 100644 --- a/services/include/input_method_system_ability.h +++ b/services/include/input_method_system_ability.h @@ -123,7 +123,7 @@ private: using CompareHandler = std::function; SubProperty FindSubPropertyByCompare(const std::string &bundleName, CompareHandler compare); SubProperty GetExtends(const std::vector &metaData); - int32_t SwitchByCombinationKey(uint32_t state, int32_t pressedKeyNum); + int32_t SwitchByCombinationKey(uint32_t state); int32_t QueryImeInfos(int32_t userId, std::vector &infos); }; diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 4a8ccfe2..527f3679 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -989,7 +989,7 @@ SubProperty InputMethodSystemAbility::FindSubPropertyByCompare(const std::string return {}; } -int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state, int32_t pressedKeyNum) +int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state) { IMSA_HILOGI("InputMethodSystemAbility::SwitchByCombinationKey"); auto current = GetCurrentInputMethodSubtype(); @@ -997,7 +997,7 @@ int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state, int32_t IMSA_HILOGE("GetCurrentInputMethodSubtype failed"); return ErrorCode::ERROR_EX_NULL_POINTER; } - if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_MODE, state, pressedKeyNum)) { + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_MODE, state)) { IMSA_HILOGI("switch mode"); auto target = current->mode == "upper" ? FindSubPropertyByCompare(current->id, @@ -1006,7 +1006,7 @@ int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state, int32_t [¤t](const SubProperty &property) { return property.mode == "upper"; }); return SwitchInputMethod(target.id, target.label); } - if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_LANGUAGE, state, pressedKeyNum)) { + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_LANGUAGE, state)) { IMSA_HILOGI("switch language"); auto target = current->language == "chinese" ? FindSubPropertyByCompare(current->id, @@ -1015,7 +1015,7 @@ int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state, int32_t [¤t](const SubProperty &property) { return property.language == "chinese"; }); return SwitchInputMethod(target.id, target.label); } - if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_IME, state, pressedKeyNum)) { + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_IME, state)) { IMSA_HILOGI("switch ime"); std::vector props = {}; auto ret = ListProperty(MAIN_USER_ID, props); From 462e2f9e4bb0d26ae76a2a767b92aac32e7da507 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 7 Jan 2023 23:11:27 +0800 Subject: [PATCH 8/8] modify code Signed-off-by: zhaolinglan --- services/src/input_method_system_ability.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 527f3679..82889d06 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -1037,7 +1037,7 @@ int32_t InputMethodSystemAbility::InitKeyEventMonitor() { IMSA_HILOGI("InputMethodSystemAbility::InitKeyEventMonitor"); bool ret = ImCommonEventManager::GetInstance()->SubscribeKeyboardEvent( - [this](uint32_t keyCode, int32_t pressedKeyNum) { return SwitchByCombinationKey(keyCode, pressedKeyNum); }); + [this](uint32_t keyCode) { return SwitchByCombinationKey(keyCode); }); return ret ? ErrorCode::NO_ERROR : ErrorCode::ERROR_SERVICE_START_FAILED; } } // namespace MiscServices