Important: Please read the Qt Code of Conduct -

QTextedit to catch "SHIFT + 1" key

  • Hi,

    I am trying to catch the key Shift + 1, however when QTextEdit is in focus it wont catch it. Instead it display '! 'out how can I make it detect Shift +1 instead of '!' ? Below is one example that i wrote to detect but not successful.

    textEdit::textEdit(QWidget *parent) : QTextEdit(parent)
       QShortcut* shortCut = new QShortcut(QKeySequence("Shift+1"), this);
        connect(shortCut, SIGNAL(activated()), this, SLOT(testing()));
    void textEdit::testing()
        qDebug() << "SHIFT 1 DETECTED...";

  • @qjqj

    Looks like Shift+1 is the regular keystroke for '!' on your key board. This would be typically handled by your key board driver.
    Did you make a decision on what shall happen when a user wants to type a '!' ?
    When the the user shall be able to input '!' as usual with Shift+1, you would need some witchcraft for your app.

  • @koahnig

    I manage to solve it by using modifiers() from keyPressEvent. When it detect "Shift+1" I will do other stuff.

    Below is my example.

    void textEdit::keyPressEvent(QKeyEvent *event)
        QString modifier;
        QString key;
        if (event->modifiers() & Qt::ShiftModifier)
                modifier += "Shift+";
        key = QKeySequence(event->key()).toString();
        qDebug() << "key: " << key;
            //Do other stuff

Log in to reply