From 7298ebb432f541a6efb9d7e7a29f34da588a6b7f Mon Sep 17 00:00:00 2001 From: Nick Bosma Date: Sat, 15 Oct 2011 20:56:06 +0200 Subject: [PATCH] Added Caps Lock check and warning at askpassphrasedialog. --- src/qt/askpassphrasedialog.cpp | 53 ++++++++++++++++++++++++++++- src/qt/askpassphrasedialog.h | 3 ++ src/qt/forms/askpassphrasedialog.ui | 17 ++++++++- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/qt/askpassphrasedialog.cpp b/src/qt/askpassphrasedialog.cpp index a297513a62..f55491fce5 100644 --- a/src/qt/askpassphrasedialog.cpp +++ b/src/qt/askpassphrasedialog.cpp @@ -6,17 +6,25 @@ #include #include +#include AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) : QDialog(parent), ui(new Ui::AskPassphraseDialog), mode(mode), - model(0) + model(0), + fCapsLock(false) { ui->setupUi(this); ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE); ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE); ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE); + + // Setup Caps Lock detection. + ui->passEdit1->installEventFilter(this); + ui->passEdit2->installEventFilter(this); + ui->passEdit3->installEventFilter(this); + ui->capsLabel->clear(); switch(mode) { @@ -184,3 +192,46 @@ void AskPassphraseDialog::textChanged() } ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable); } + +bool AskPassphraseDialog::event(QEvent *event) +{ + // Detect Caps Lock key press. + if (event->type() == QEvent::KeyPress) { + QKeyEvent *ke = static_cast(event); + if (ke->key() == Qt::Key_CapsLock) { + fCapsLock = !fCapsLock; + } + if (fCapsLock) { + ui->capsLabel->setText(tr("Warning: The Caps Lock key is on.")); + } else { + ui->capsLabel->clear(); + } + } + return QWidget::event(event); +} + +bool AskPassphraseDialog::eventFilter(QObject *, QEvent *event) +{ + /* Detect Caps Lock. + * There is no good OS-independent way to check a key state in Qt, but we + * can detect Caps Lock by checking for the following condition: + * Shift key is down and the result is a lower case character, or + * Shift key is not down and the result is an upper case character. + */ + if (event->type() == QEvent::KeyPress) { + QKeyEvent *ke = static_cast(event); + QString str = ke->text(); + if (str.length() != 0) { + const QChar *psz = str.unicode(); + bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0; + if ((fShift && psz->isLower()) || (!fShift && psz->isUpper())) { + fCapsLock = true; + ui->capsLabel->setText(tr("Warning: The Caps Lock key is on.")); + } else if (psz->isLetter()) { + fCapsLock = false; + ui->capsLabel->clear(); + } + } + } + return false; +} diff --git a/src/qt/askpassphrasedialog.h b/src/qt/askpassphrasedialog.h index 761612cbfd..0c708918cf 100644 --- a/src/qt/askpassphrasedialog.h +++ b/src/qt/askpassphrasedialog.h @@ -32,9 +32,12 @@ private: Ui::AskPassphraseDialog *ui; Mode mode; WalletModel *model; + bool fCapsLock; private slots: void textChanged(); + bool event(QEvent *event); + bool eventFilter(QObject *, QEvent *event); }; #endif // ASKPASSPHRASEDIALOG_H diff --git a/src/qt/forms/askpassphrasedialog.ui b/src/qt/forms/askpassphrasedialog.ui index 70d9180e75..f1f2ef3731 100644 --- a/src/qt/forms/askpassphrasedialog.ui +++ b/src/qt/forms/askpassphrasedialog.ui @@ -7,7 +7,7 @@ 0 0 589 - 228 + 239 @@ -83,6 +83,21 @@ + + + + #capsLabel { + font: bold; +} + + + TextLabel + + + Qt::AlignCenter + + +