Skip to content

Commit

Permalink
Implement Caps Lock check on macOS and override event() instead of po…
Browse files Browse the repository at this point in the history
…lling every 50ms
  • Loading branch information
phoerious committed Oct 21, 2019
1 parent d274916 commit d1f7815
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 33 deletions.
8 changes: 0 additions & 8 deletions src/gui/DatabaseOpenWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
<ui version="4.0">
<class>DatabaseOpenWidget</class>
<widget class="QWidget" name="DatabaseOpenWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>592</width>
<height>522</height>
</rect>
</property>
<property name="accessibleName">
<string>Unlock KePassXC Database</string>
</property>
Expand Down
28 changes: 8 additions & 20 deletions src/gui/PasswordEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@
#include "core/FilePath.h"
#include "gui/Font.h"

#include <QKeyEvent>
#include <QTimer>
#include <QProcessEnvironment>

#if defined(Q_OS_WIN)
#include <windows.h>
#elif defined(Q_OS_MACOS)
// TODO
#include <CoreGraphics/CGEventSource.h>
#elif defined(Q_OS_UNIX)
#include <QProcessEnvironment>
#include <QtX11Extras/QX11Info>
// namespace required to avoid name clashes with declarations in XKBlib.h
namespace X11
Expand All @@ -45,7 +42,6 @@ const QColor PasswordEdit::ErrorColor = QColor(255, 125, 125);
PasswordEdit::PasswordEdit(QWidget* parent)
: QLineEdit(parent)
, m_basePasswordEdit(nullptr)
, m_capslockPollTimer(new QTimer(this))
{
const QIcon errorIcon = filePath()->icon("status", "dialog-error");
m_errorAction = addAction(errorIcon, QLineEdit::TrailingPosition);
Expand All @@ -64,8 +60,6 @@ PasswordEdit::PasswordEdit(QWidget* parent)
QFont passwordFont = Font::fixedFont();
passwordFont.setLetterSpacing(QFont::PercentageSpacing, 110);
setFont(passwordFont);

connect(m_capslockPollTimer, SIGNAL(timeout()), SLOT(checkCapslockState()));
}

void PasswordEdit::enableVerifyMode(PasswordEdit* basePasswordEdit)
Expand Down Expand Up @@ -140,19 +134,13 @@ void PasswordEdit::autocompletePassword(const QString& password)
}
}

void PasswordEdit::hideEvent(QHideEvent* event)
{
QWidget::hideEvent(event);
m_capslockPollTimer->stop();
}

void PasswordEdit::showEvent(QShowEvent* event)
bool PasswordEdit::event(QEvent* event)
{
QWidget::showEvent(event);
if (!m_basePasswordEdit) {
// poll caps lock state only for primary password edits
m_capslockPollTimer->start(50);
// check caps lock state only for primary password edits
checkCapslockState();
}
return QLineEdit::event(event);
}

void PasswordEdit::checkCapslockState()
Expand All @@ -162,14 +150,14 @@ void PasswordEdit::checkCapslockState()
#if defined(Q_OS_WIN)
newCapslockState = (GetKeyState(VK_CAPITAL) == 1);
#elif defined(Q_OS_MACOS)
// TODO
newCapslockState = ((CGEventSourceFlagsState(kCGEventSourceStateHIDSystemState) & kCGEventFlagMaskAlphaShift) != 0);
#elif defined(Q_OS_UNIX)
if (QX11Info::isPlatformX11() && QX11Info::display()) {
unsigned state = 0;
// reinterpret cast needed, since we namespaced the XKBlib.h include
if (X11::XkbGetIndicatorState(
reinterpret_cast<X11::Display*>(QX11Info::display()), XkbUseCoreKbd, &state) == Success) {
newCapslockState = ((state & 1u) == 1);
newCapslockState = ((state & 1u) != 0);
}
}
#endif
Expand Down
6 changes: 1 addition & 5 deletions src/gui/PasswordEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include <QLineEdit>
#include <QPointer>

class QTimer;

class PasswordEdit : public QLineEdit
{
Q_OBJECT
Expand All @@ -41,8 +39,7 @@ public slots:
void setShowPassword(bool show);

protected:
void showEvent(QShowEvent* event) override;
void hideEvent(QHideEvent* event) override;
bool event(QEvent* event) override;

signals:
void showPasswordChanged(bool show);
Expand All @@ -60,7 +57,6 @@ private slots:
QPointer<QAction> m_errorAction;
QPointer<QAction> m_correctAction;
QPointer<PasswordEdit> m_basePasswordEdit;
QPointer<QTimer> m_capslockPollTimer;
};

#endif // KEEPASSX_PASSWORDEDIT_H

0 comments on commit d1f7815

Please sign in to comment.