diff --git a/kcm_hotkeys/actions/keyboard_input_action_widget.h b/kcm_hotkeys/actions/keyboard_input_action_widget.h --- a/kcm_hotkeys/actions/keyboard_input_action_widget.h +++ b/kcm_hotkeys/actions/keyboard_input_action_widget.h @@ -58,6 +58,11 @@ Ui::KeyboardInputActionWidget ui; +private: + bool eventFilter(QObject *watched, QEvent *event) override; + +private slots: + void on_toolButtonRecordKeyPresses_clicked(bool checked); }; diff --git a/kcm_hotkeys/actions/keyboard_input_action_widget.cpp b/kcm_hotkeys/actions/keyboard_input_action_widget.cpp --- a/kcm_hotkeys/actions/keyboard_input_action_widget.cpp +++ b/kcm_hotkeys/actions/keyboard_input_action_widget.cpp @@ -18,6 +18,8 @@ #include "keyboard_input_action_widget.h" +#include + #include KeyboardInputActionWidget::KeyboardInputActionWidget( @@ -51,6 +53,8 @@ ui.specific_radio, SIGNAL(clicked(bool)), _changedSignals, SLOT(map()) ); _changedSignals->setMapping(ui.specific_radio, "specific_radio" ); + + ui.input->installEventFilter(this); } @@ -121,9 +125,64 @@ { action()->setDestination(KHotKeys::KeyboardInputAction::SpecificWindow); ui.windowdef_list->copyToObject(); + } +} + +bool KeyboardInputActionWidget::eventFilter(QObject *watched, QEvent *event) +{ + Q_UNUSED(watched) // only installed on ui.input + if (ui.toolButtonRecordKeyPresses->isChecked()) { + if (event->type() == QEvent::KeyPress) { + QKeyEvent *keyEvent = static_cast(event); + keyEvent->ignore(); + + if (keyEvent->isAutoRepeat() + || keyEvent->key() == Qt::Key_Alt + || keyEvent->key() == Qt::Key_Control + || keyEvent->key() == Qt::Key_Meta + || keyEvent->key() == Qt::Key_Shift) + return true; + + QString currentCommand = ui.input->toPlainText(); + QString newKeyString; + + // For the supported modifier key names see the QKeySequence documentation + // Create a map manually because Qt::Key is not registered in the Meta object system + QMap supportedModifiers; + supportedModifiers[Qt::AltModifier] = QStringLiteral("Alt"); + supportedModifiers[Qt::ControlModifier] = QStringLiteral("Ctrl"); + supportedModifiers[Qt::MetaModifier] = QStringLiteral("Meta"); + supportedModifiers[Qt::ShiftModifier] = QStringLiteral("Shift"); + + for (Qt::KeyboardModifier modifier : supportedModifiers.keys()) { + if (keyEvent->modifiers() & modifier) { + if (!newKeyString.isEmpty()) + newKeyString.append(QStringLiteral("+")); + newKeyString.append(supportedModifiers.value(modifier)); + } + } + + // query the key name via the Qt's meta system + QString keyString = QKeySequence(keyEvent->key()).toString(); + // do not add unknown keys + if (keyString.isEmpty()) + return true; + + if (!newKeyString.isEmpty()) + newKeyString.append(QStringLiteral("+")); + + // strip out the Key_ prefix + newKeyString.append(keyString); + + if (!currentCommand.isEmpty()) + newKeyString.prepend(":"); + + ui.input->setPlainText(currentCommand.append(newKeyString)); + return true; } } - + return QObject::eventFilter(watched, event); +} bool KeyboardInputActionWidget::isChanged() const { @@ -147,4 +206,30 @@ return false; } +void KeyboardInputActionWidget::on_toolButtonRecordKeyPresses_clicked(bool checked) + { + if (checked) { + if (!ui.input->toPlainText().isEmpty()) { + const int choice = KMessageBox::warningContinueCancel( + this, + i18n("You have recorded key sequence.\n" + "Do you want to discard and record a new key sequence?"), + i18n("Discard current key sequence") + ); + + if (choice == KMessageBox::Cancel) { + ui.toolButtonRecordKeyPresses->setChecked(false); + return; + } + } + ui.input->clear(); + ui.input->setReadOnly(true); + ui.input->setFocus(); + ui.toolButtonRecordKeyPresses->setText(i18n("Finish recording")); + } else { + ui.input->setReadOnly(false); + ui.toolButtonRecordKeyPresses->setText(i18n("Record key strokes")); + } + } + #include "moc_keyboard_input_action_widget.cpp" diff --git a/kcm_hotkeys/actions/keyboard_input_action_widget.ui b/kcm_hotkeys/actions/keyboard_input_action_widget.ui --- a/kcm_hotkeys/actions/keyboard_input_action_widget.ui +++ b/kcm_hotkeys/actions/keyboard_input_action_widget.ui @@ -1,7 +1,8 @@ - + + KeyboardInputActionWidget - - + + 0 0 @@ -9,43 +10,53 @@ 489 - + - + - - + + + Record key strokes + + + true + + + + + + Window - + - + - - - Active window + + + Active &window - - - Specific window + + + S&pecific window - - - Action window + + + A&ction window - +