Button Press vs Key Press
-
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?
-
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?
-
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. -
Can you please explain what you're doing and what exactly is wrong? I read it twice but still don't understand the problem.
-
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?
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.
-
@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.