Skip to content

Commit

Permalink
LineEdit: translate Ctrl+[ to Esc
Browse files Browse the repository at this point in the history
  • Loading branch information
tamlok committed Jul 8, 2021
1 parent 4e7cac6 commit 19b5163
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/widgets/lineedit.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "lineedit.h"

#include <QKeyEvent>
#include <QCoreApplication>

#include <utils/widgetutils.h>

Expand All @@ -21,30 +22,47 @@ void LineEdit::keyPressEvent(QKeyEvent *p_event)
// Note that QKeyEvent starts with isAccepted() == true, so you do not
// need to call QKeyEvent::accept() - just do not call the base class
// implementation if you act upon the key.
bool accept = false;
bool accepted = false;
int modifiers = p_event->modifiers();
switch (p_event->key()) {
case Qt::Key_BracketLeft:
{
if (WidgetUtils::isViControlModifier(modifiers)) {
auto escEvent = new QKeyEvent(QEvent::KeyPress,
Qt::Key_Escape,
Qt::NoModifier);
QCoreApplication::postEvent(this, escEvent);
accepted = true;
}

break;
}

case Qt::Key_H:
{
// Backspace.
if (WidgetUtils::isViControlModifier(modifiers)) {
backspace();
accept = true;
accepted = true;
}

break;
}

case Qt::Key_W:
{
// Delete one word backward.
if (WidgetUtils::isViControlModifier(modifiers)) {
if (!hasSelectedText()) {
cursorWordBackward(true);
}

backspace();
accept = true;
accepted = true;
}

break;
}

case Qt::Key_U:
{
Expand All @@ -59,7 +77,7 @@ void LineEdit::keyPressEvent(QKeyEvent *p_event)
}
}

accept = true;
accepted = true;
}

break;
Expand All @@ -69,7 +87,7 @@ void LineEdit::keyPressEvent(QKeyEvent *p_event)
break;
}

if (!accept) {
if (!accepted) {
QLineEdit::keyPressEvent(p_event);
}
}
Expand Down

0 comments on commit 19b5163

Please sign in to comment.