diff --git a/src/lib/fcitx/globalconfig.cpp b/src/lib/fcitx/globalconfig.cpp index 23b976260..8e58743b0 100644 --- a/src/lib/fcitx/globalconfig.cpp +++ b/src/lib/fcitx/globalconfig.cpp @@ -72,6 +72,18 @@ FCITX_CONFIGURATION( {Key("Hangul_Romaja")}, KeyListConstrain({KeyConstrainFlag::AllowModifierLess, KeyConstrainFlag::AllowModifierOnly})}; + OptionWithAnnotation consumeRedundantActivateKeys{{ + .parent = this, + .path{"ConsumeRedundantActivateKeys"}, + .description{_("Consume redundant Activate/Deactivate key")}, + .defaultValue = false, + .annotation{ + _("When the input method is already in the state the key " + "requests, pressing an Activate/Deactivate key changes " + "nothing; consume the key event in that case instead of " + "forwarding it to the application. Modifier only keys are " + "never consumed.")}, + }}; KeyListOptionWithAnnotation altTriggerKeys{ {.parent = this, .path{"AltTriggerKeys"}, @@ -298,6 +310,11 @@ const KeyList &GlobalConfig::deactivateKeys() const { return d->hotkey->deactivateKeys.value(); } +bool GlobalConfig::consumeRedundantActivateKeys() const { + FCITX_D(); + return *d->hotkey->consumeRedundantActivateKeys; +} + const KeyList &GlobalConfig::enumerateForwardKeys() const { FCITX_D(); return d->hotkey->enumerateForwardKeys.value(); diff --git a/src/lib/fcitx/globalconfig.h b/src/lib/fcitx/globalconfig.h index 5982d9c9a..583d9b4ed 100644 --- a/src/lib/fcitx/globalconfig.h +++ b/src/lib/fcitx/globalconfig.h @@ -31,6 +31,19 @@ class FCITXCORE_EXPORT GlobalConfig { const KeyList &altTriggerKeys() const; const KeyList &activateKeys() const; const KeyList &deactivateKeys() const; + + /** + * Consume Activate/Deactivate keys even when the state does not change. + * + * When enabled, pressing an activate key while the input method is + * already active (or a deactivate key while inactive) is consumed by + * fcitx instead of being forwarded to the application. Modifier only + * keys are not affected. + * + * @return whether to consume redundant activate/deactivate keys + * @since 5.1.22 + */ + bool consumeRedundantActivateKeys() const; const KeyList &enumerateForwardKeys() const; const KeyList &enumerateBackwardKeys() const; bool enumerateSkipFirst() const; diff --git a/src/lib/fcitx/instance.cpp b/src/lib/fcitx/instance.cpp index 37c5f0bba..e7b74a47b 100644 --- a/src/lib/fcitx/instance.cpp +++ b/src/lib/fcitx/instance.cpp @@ -457,7 +457,8 @@ bool InstancePrivate::canActivate(InputContext *ic) { return false; } auto *inputState = ic->propertyFor(&inputStateFactory_); - return !inputState->isActive(); + return (!inputState->isActive()) || + globalConfig_.consumeRedundantActivateKeys(); } bool InstancePrivate::canDeactivate(InputContext *ic) { @@ -466,7 +467,8 @@ bool InstancePrivate::canDeactivate(InputContext *ic) { return false; } auto *inputState = ic->propertyFor(&inputStateFactory_); - return inputState->isActive(); + return inputState->isActive() || + globalConfig_.consumeRedundantActivateKeys(); } void InstancePrivate::navigateGroup(InputContext *ic, const Key &key,