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. Button Press vs Key Press
Qt 6.11 is out! See what's new in the release blog

Button Press vs Key Press

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.4k Views 2 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.
  • A Offline
    A Offline
    admkrk
    wrote on last edited by
    #1

    I am working on a synthesizer application that can use a button press or a key press to emit a signal. There was no problem with just the buttons, but when I added the key events, that does not work unless a button is pressed first.

    void KeyPad::on_key_A_pressed()
    {
        emit noteOn(A);
    }
    
    void KeyPad::keyPressEvent(QKeyEvent *event)
    {
        switch(event->key())
        {
        case Qt::Key_Z:
            ui->key_A->setDown(true);
            emit noteOn(A);
            break;
    	// ...
        }
    }
    

    If I change the wave shape, the keyboard goes back to not working until I press a button again. Can someone explain to me why a button needs to be pressed before the key event is triggered?

    JonBJ 1 Reply Last reply
    0
    • A admkrk

      I am working on a synthesizer application that can use a button press or a key press to emit a signal. There was no problem with just the buttons, but when I added the key events, that does not work unless a button is pressed first.

      void KeyPad::on_key_A_pressed()
      {
          emit noteOn(A);
      }
      
      void KeyPad::keyPressEvent(QKeyEvent *event)
      {
          switch(event->key())
          {
          case Qt::Key_Z:
              ui->key_A->setDown(true);
              emit noteOn(A);
              break;
      	// ...
          }
      }
      

      If I change the wave shape, the keyboard goes back to not working until I press a button again. Can someone explain to me why a button needs to be pressed before the key event is triggered?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @admkrk
      I'm not sure I understand, but if you call QAbstractButton::setDown(true) explicitly, aren't you responsible for also calling QAbstractButton::setDown(false)? Else Qt will see that button as down but not up, and be in a messed state?

      1 Reply Last reply
      1
      • A Offline
        A Offline
        admkrk
        wrote on last edited by
        #3

        Yes, I also handle the release.

        void KeyPad::keyReleaseEvent(QKeyEvent *event)
        {
            switch(event->key())
            {
            case Qt::Key_Z:
                ui->key_A->setDown(false);
                break;
        	// ...
            }
        
            emit noteOff();
        }
        

        That is just so you can see something happening in the GUI. I had the same behavior before adding that bit, so I do not see it as being related.

        I tried calling on_key_A_pressed() instead of emitting the signal directly, but that made no difference. This leads me to believe that the press event is not registering until a button is pressed first. Further, any change to the GUI, other than pressing a button, resets it to not registering until a button is pressed again. I do not understand why this is happening.

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you please explain what you're doing and what exactly is wrong? I read it twice but still don't understand the problem.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3
          • A Offline
            A Offline
            admkrk
            wrote on last edited by
            #5

            Sorry for the bad explanation, I will try to do better. I think the confusion is because the buttons are representing keys on a keyboard(the GUI element) while I am referring to key presses on the computer keyboard. Maybe an image will help?

            gui.png

            When I press one of the assigned keys (on the computer keyboard), nothing happens until I first press one of the buttons. Then it all works fine. If I interact with anything else in the GUI, it no longer responds to the keyboard, until I press a button again.

            OK, I narrowed it down a bit. It seems the problem is that the key presses are only registered when my KeyPad has focus. I never considered that would make a difference. The sliders and dials all respond to the mouse regardless of what has focus. Is there a way to have the key presses work the same way?

            I hope that is clearer.

            1 Reply Last reply
            0
            • hskoglundH Offline
              hskoglundH Offline
              hskoglund
              wrote on last edited by
              #6

              The key presses go to the window that has focus, so if your KeyPad is only a small window (a small part of your app) then it's better to check for the key presses in your main app window.

              1 Reply Last reply
              4
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by SGaist
                #7

                Hi,

                Aren't you re-implementing QShortcut ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • A Offline
                  A Offline
                  admkrk
                  wrote on last edited by
                  #8

                  @SGaist said in Button Press vs Key Press:

                  Hi,

                  Aren't you re-implementing QShortcut ?

                  I thought about that at first, but that seems to only trigger clicked() and holding the key just repeatedly triggers it.

                  @hskoglund said in Button Press vs Key Press:

                  The key presses go to the window that has focus, so if your KeyPad is only a small window (a small part of your app) then it's better to check for the key presses in your main app window.

                  It is actually a custom widget. I am already getting a bit of latency and that will probably make it worse, but I guess that might be my best option. I will give that a try and see how it works.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    admkrk
                    wrote on last edited by
                    #9

                    Handling the key presses in the main window did solve my problem, thanks.

                    I am unsure about how it effected the latency, I can work on that later.

                    Thanks again!

                    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