From 0419c9a3b1511063cc601d9915cba031aa223206 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Mon, 8 Jul 2024 17:14:42 +0200 Subject: [PATCH] [plugins/stickykeys] Show notification when keys are locked If the relevant setting is enabled CCBUG: 395559 --- src/plugins/stickykeys/CMakeLists.txt | 5 +++- src/plugins/stickykeys/stickykeys.cpp | 36 +++++++++++++++++++++++++++ src/plugins/stickykeys/stickykeys.h | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/plugins/stickykeys/CMakeLists.txt b/src/plugins/stickykeys/CMakeLists.txt index bb6422b7a6..b99b5d04ab 100644 --- a/src/plugins/stickykeys/CMakeLists.txt +++ b/src/plugins/stickykeys/CMakeLists.txt @@ -14,5 +14,8 @@ target_sources(StickyKeysPlugin PRIVATE main.cpp stickykeys.cpp ) -target_link_libraries(StickyKeysPlugin PRIVATE kwin KF6::WindowSystem XKB::XKB) +target_link_libraries(StickyKeysPlugin PRIVATE kwin KF6::WindowSystem KF6::I18n XKB::XKB) +if (KWIN_BUILD_NOTIFICATIONS) + target_link_libraries(StickyKeysPlugin PRIVATE KF6::Notifications) +endif() diff --git a/src/plugins/stickykeys/stickykeys.cpp b/src/plugins/stickykeys/stickykeys.cpp index 6b2c3a60ee..0c546b8a7d 100644 --- a/src/plugins/stickykeys/stickykeys.cpp +++ b/src/plugins/stickykeys/stickykeys.cpp @@ -8,6 +8,25 @@ #include "keyboard_input.h" #include "xkb.h" +#include +#if KWIN_BUILD_NOTIFICATIONS +#include +#endif + +struct Modifier +{ + Qt::Key key; + KLazyLocalizedString lockedText; +}; + +static const std::array modifiers = { + Modifier{Qt::Key_Shift, kli18n("The Shift key has been locked and is now active for all of the following keypresses.")}, + Modifier{Qt::Key_Control, kli18n("The Control key has been locked and is now active for all of the following keypresses.")}, + Modifier{Qt::Key_Alt, kli18n("The Alt key has been locked and is now active for all of the following keypresses.")}, + Modifier{Qt::Key_Meta, kli18n("The Meta key has been locked and is now active for all of the following keypresses.")}, + Modifier{Qt::Key_AltGr, kli18n("The AltGr key has been locked and is now active for all of the following keypresses.")}, +}; + StickyKeysFilter::StickyKeysFilter() : m_configWatcher(KConfigWatcher::create(KSharedConfig::openConfig("kaccessrc"))) { @@ -46,6 +65,7 @@ void StickyKeysFilter::loadConfig(const KConfigGroup &group) KWin::input()->uninstallInputEventFilter(this); m_lockKeys = group.readEntry("StickyKeysLatch", true); + m_showNotificationForLockedKeys = group.readEntry("kNotifyModifiers", false); if (!m_lockKeys) { // locking keys is deactivated, unlock all locked keys @@ -89,6 +109,22 @@ bool StickyKeysFilter::keyEvent(KWin::KeyEvent *event) else if (keyState.value() == Latched && m_lockKeys) { keyState.value() = Locked; KWin::input()->keyboard()->xkb()->setModifierLocked(keyToModifier(static_cast(event->key())), true); + + if (m_showNotificationForLockedKeys) { +#if KWIN_BUILD_NOTIFICATIONS + KNotification *noti = new KNotification("modifierkey-locked"); + noti->setComponentName("kaccess"); + + for (const auto mod : modifiers) { + if (mod.key == event->key()) { + noti->setText(mod.lockedText.toString()); + break; + } + } + + noti->sendEvent(); +#endif + } } // A locked modifier was pressed, unlock it else if (keyState.value() == Locked && m_lockKeys) { diff --git a/src/plugins/stickykeys/stickykeys.h b/src/plugins/stickykeys/stickykeys.h index 9a18289b15..2d633554b8 100644 --- a/src/plugins/stickykeys/stickykeys.h +++ b/src/plugins/stickykeys/stickykeys.h @@ -32,4 +32,5 @@ private: QMap m_keyStates; QList m_modifiers = {Qt::Key_Shift, Qt::Key_Control, Qt::Key_Alt, Qt::Key_AltGr, Qt::Key_Meta}; bool m_lockKeys = false; + bool m_showNotificationForLockedKeys = false; };