Qt: Holding Enter key on focused QPushButton causes repeated system/screen-reader sound (auto-repeat)
-
I am implementing custom keyboard handling for a QPushButton (press vs hold behavior).
The logic works correctly, but holding the Enter key while the button has focus produces a repeated system / screen-reader sound (for example “return return return” as"rrrrr" on Linux with Orca). how to stop that sound#include <QApplication>
#include <QPushButton>
#include <QTimer>
#include <QKeyEvent>
#include <QDebug>class SilentBtn : public QPushButton {
Q_OBJECT
QTimer t; bool held = false;
public:
SilentBtn() : QPushButton("Hold or Press Me") {
t.setInterval(500); t.setSingleShot(true);
connect(&t, &QTimer::timeout, this, [=]{
held = true; setText("HELD!"); qDebug() << "HOLD";
});
}
protected:
void press() { held=false; t.start(); setDown(true); }
void release() { t.stop(); setDown(false);
if(!held){ setText("PRESSED!"); qDebug()<<"PRESS"; }
QTimer::singleShot(1000, this, [=]{ setText("Hold or Press Me"); });
}
void mousePressEvent(QMouseEvent *e) override { if(e->button()==Qt::LeftButton) press(); e->accept(); }
void mouseReleaseEvent(QMouseEvent *e) override { if(e->button()==Qt::LeftButton) release(); e->accept(); }
void keyPressEvent(QKeyEvent *e) override {
if(!e->isAutoRepeat() &&
(e->key()==Qt::Key_Return||e->key()==Qt::Key_Enter||e->key()==Qt::Key_Space)) press();
e->accept();
}
void keyReleaseEvent(QKeyEvent *e) override {
if(!e->isAutoRepeat() &&
(e->key()==Qt::Key_Return||e->key()==Qt::Key_Enter||e->key()==Qt::Key_Space)) release();
e->accept();
}
};int main(int c,char**v){
QApplication a(c,v); SilentBtn b; b.resize(300,100); b.show(); return a.exec();
}#include "main.moc"
-
Hi and welcome to the Qt Forum!
Please format your code, using the </> formatting tabs.
It's too hard to read (at least for me) otherwise. -
I am implementing custom keyboard handling for a QPushButton (press vs hold behavior).
The logic works correctly, but holding the Enter key while the button has focus produces a repeated system / screen-reader sound (like “return return return” as"rrrrr" on Linux with Orca). how to stop that sound or how to handle that in qtvoid keyPressEvent(QKeyEvent *e) override { if (!e->isAutoRepeat() && (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter || e->key() == Qt::Key_Space)) { // custom press handling } e->accept(); } void keyReleaseEvent(QKeyEvent *e) override { if (!e->isAutoRepeat() && (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter || e->key() == Qt::Key_Space)) { // custom release handling } e->accept(); } -
Thanks for formatting the code. Much easier to read now!
The code handles single press and release events of the three specified keys individually. Everything else, incl. all autorepeats is accepted and ignored. The repeat sound is not created or controlled by Qt.If memory serves well, you are experiencing the Echo feature of Orca.
Just turn it off. I think it's under preferences -> Echo and called something like "Enable key echo".
If you don't want autorepeat in general, turn in off on GNOME level. -
Thanks for formatting the code. Much easier to read now!
The code handles single press and release events of the three specified keys individually. Everything else, incl. all autorepeats is accepted and ignored. The repeat sound is not created or controlled by Qt.If memory serves well, you are experiencing the Echo feature of Orca.
Just turn it off. I think it's under preferences -> Echo and called something like "Enable key echo".
If you don't want autorepeat in general, turn in off on GNOME level.@Axel-Spoerl Thanks for the clarification.
My actual requirement is to implement a hold-to-record interaction (press and hold a key to start voice recording, release to stop) at the application level.
Since key echo from screen readers (for example Orca) cannot be controlled by Qt, what is the recommended Qt- and accessibility-friendly approach for implementing such a feature?
-
If the requirement is to implement it with a keyboard, I recommend appropriate documentation: The application works only if autorepeat is turned off in the underlying window manager. By design, Qt can't control or override the window manager.
One alternative is to use the mouse instead.
Another alternative is to toggle recording with a click.
That's essentially what all the recording and DAW applications that I know/use do: Audacity, Ardour, ...