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. Catching a key combination like(ALT+G) and Simulating the combination on a Button Push
Forum Update on Monday, May 27th 2025

Catching a key combination like(ALT+G) and Simulating the combination on a Button Push

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 6.2k 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.
  • musimbateM Offline
    musimbateM Offline
    musimbate
    wrote on last edited by
    #1

    Hi all,
    I am trying to catch a key combination in my keypress event.The code I am trying is as follows.But as soon as I press the alt key the code in my if statement is directly executed.Thinking of adding a small delay to give the user some time to add other keys to the combination.Wondering if this is a good way to go about it.

    Also is there an easy way I can simulate the same key combination when a user presses a push button for example.I mean in a slot that responds to that button's clicked() signal.
    Thanks for your time.

    @void Widget::keyPressEvent( QKeyEvent * event )
    {
    if(event->modifiers()&&Qt::AltModifier!=0)
    {
    //--->>DELAY IDEA HERE<<<----
    //DO SOMETHING
    }

    }@

    Why join the navy if you can be a pirate?-Steve Jobs

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Hi musimbate.
      Don't put any delay in event body. Try this:
      @if(event->key() != Qt::Key_Alt)
      {
      if(event->modifiers()&Qt::AltModifier)
      {
      qDebug()<<QString("Alt+%1").arg(event->text());
      }
      }@

      1 Reply Last reply
      0
      • musimbateM Offline
        musimbateM Offline
        musimbate
        wrote on last edited by
        #3

        Thanks qxoz
        Your code is catching my combination.I also want to know if I am simulating the same combination the right way by the following code:
        @void Widget::simulateGKey()
        {
        //QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifiers);
        // QCoreApplication::postEvent (receiver, event);

         QTest::keyClick(this, Qt::Key_G,Qt::AltModifier);
        

        }@

        I know the code that is commented out is the best way to do this but I am only getting a key press and the event->text() code gives nothing.

        Thanks for your time.

        Why join the navy if you can be a pirate?-Steve Jobs

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qxoz
          wrote on last edited by
          #4

          Try this:
          @QKeyEvent key_press(QKeyEvent::KeyPress, Qt::Key_G, Qt::AltModifier, NULL, false, 0 );
          QApplication::sendEvent(this, &key_press);

          QKeyEvent key_release(QKeyEvent::KeyRelease, Qt::Key_G, Qt::AltModifier, NULL, false, 0 );
          QApplication::sendEvent(this, &key_release);@

          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