File indexing completed on 2024-11-10 04:56:57

0001 /*
0002     SPDX-FileCopyrightText: 2023 Nicolas Fella <nicolas.fella@gmx.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "bouncekeys.h"
0008 #include "keyboard_input.h"
0009 
0010 BounceKeysFilter::BounceKeysFilter()
0011     : m_configWatcher(KConfigWatcher::create(KSharedConfig::openConfig("kaccessrc")))
0012 {
0013     const QLatin1String groupName("Keyboard");
0014     connect(m_configWatcher.get(), &KConfigWatcher::configChanged, this, [this, groupName](const KConfigGroup &group) {
0015         if (group.name() == groupName) {
0016             loadConfig(group);
0017         }
0018     });
0019     loadConfig(m_configWatcher->config()->group(groupName));
0020 }
0021 
0022 void BounceKeysFilter::loadConfig(const KConfigGroup &group)
0023 {
0024     KWin::input()->uninstallInputEventFilter(this);
0025 
0026     if (group.readEntry<bool>("BounceKeys", false)) {
0027         KWin::input()->prependInputEventFilter(this);
0028 
0029         m_delay = std::chrono::milliseconds(group.readEntry<int>("BounceKeysDelay", 500));
0030     } else {
0031         m_lastEvent.clear();
0032     }
0033 }
0034 
0035 bool BounceKeysFilter::keyEvent(KWin::KeyEvent *event)
0036 {
0037     if (event->type() != KWin::KeyEvent::KeyPress) {
0038         return false;
0039     }
0040 
0041     auto it = m_lastEvent.find(event->key());
0042 
0043     if (it == m_lastEvent.end()) {
0044         // first time is always good
0045         m_lastEvent[event->key()] = event->timestamp();
0046         return false;
0047     }
0048 
0049     auto last = *it;
0050     *it = event->timestamp();
0051 
0052     return event->timestamp() - last < m_delay;
0053 }
0054 
0055 #include "moc_bouncekeys.cpp"