Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextedit to catch "SHIFT + 1" key
Forum Update on Monday, May 27th 2025

QTextedit to catch "SHIFT + 1" key

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qjqj
    wrote on last edited by
    #1

    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...";
    }
    
    
    K 1 Reply Last reply
    0
    • Q qjqj

      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...";
      }
      
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      4
      • Q Offline
        Q Offline
        qjqj
        wrote on last edited by
        #3

        @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;
            if(key.contains("@"))
            {
                //Do other stuff
            }
            QTextEdit::keyPressEvent(event);
        }
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved