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. Multiple keys
Forum Updated to NodeBB v4.3 + New Features

Multiple keys

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.3k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    mohamaddanesh44
    wrote on last edited by
    #1

    i want a multilpe key e.g. shift+a but i have a problem and i cant handle it.
    here is a part of my code
    could you help me?
    thanks a lot

    case Qt::Key_Shift:
    if(event->modifiers().testFlag(Qt::ShiftModifier))
    {
    msg->setText("you pressed : shift + "+event1->text());
    msg->exec();
    }

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Santosh Reddy
      wrote on last edited by
      #2

      Qt::Key_Shift is key modifier and not a key by itself so comparing it in a switch case will not work, instead do somthing like this
      @case Qt::Key_A:
      if(event->modifiers().testFlag(Qt::ShiftModifier))
      qDebug() << "Shift + A"
      else
      qDebug() << "A";@

      SS

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mohamaddanesh44
        wrote on last edited by
        #3

        it didnt work.
        i want to handle all the buttons & multiple ones of keyboard.
        i mean...i want to handle "a" , "shift+a" , "ctrl+a" , ect.
        how can i handle it???

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Code_ReaQtor
          wrote on last edited by
          #4

          make a bitwise AND

          @if (event->modifiers() & Qt::ShiftModifier) {
          //do something
          } else {
          //do another thing
          }@

          Please visit my open-source projects at https://github.com/Code-ReaQtor.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Santosh Reddy
            wrote on last edited by
            #5

            Here is an example.
            @class KeySpy : public QObject
            {
            Q_OBJECT
            public:
            explicit KeySpy(QObject * = 0) { }

            protected:
            bool eventFilter(QObject *, QEvent * event);
            };

            bool KeySpy::eventFilter(QObject *, QEvent * event)
            {
            if(event->type() != QEvent::KeyPress)
            return false;

            QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
            
            int k = keyEvent->key();
            char key_string = '?';
            
            if((Qt::Key_0 <= k) && (k <= Qt::Key_9))
            {
                key_string = k - Qt::Key_0 + '0';
            }
            else if((Qt::Key_A <= k) && (k <= Qt::Key_Z))
            {
                key_string = k - Qt::Key_A + 'A';
            }
            
            QString key;
            if(keyEvent->modifiers().testFlag(Qt::ShiftModifier))
            {
                key = QString("Shitf + %1").arg(key_string);
            }
            else if(keyEvent->modifiers().testFlag(Qt::ControlModifier))
            {
                key = QString("Ctrl + %1").arg(key_string);
            }
            else if(keyEvent->modifiers().testFlag(Qt::AltModifier))
            {
                key = QString("Alt + %1").arg(key_string);
            }
            else
            {
                key = QString("%1").arg(key_string);
            }
            
            qDebug() << key << keyEvent->text();
            
            return false;
            

            }@

            SS

            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