Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt / C++ - Monitor specified input without focus

Tags:

c++

input

qt

I want to be able to press a specific Qt::Key at any time regardless of focus. For example, ctrl+shift+F activates a method that brings the window back into focus.

I've done google searches for how to monitor input without focus in qt but I can't find anything.

like image 437
Vii Avatar asked Mar 18 '23 03:03

Vii


2 Answers

You can do this either by subclassing QApplication and overriding its event(QEvent *) method, or (more commonly) by calling qApp->installEventFilter(someObject), where (someObject) is some Qt object (whichever one is most convenient for you) whose eventFilter(QObject *, QEvent *) method you have overridden with your own implementation that watches for the appropriate QKeyEvent and does the appropriate action in response.

like image 117
Jeremy Friesner Avatar answered Mar 27 '23 12:03

Jeremy Friesner


This feature is not implemented in Qt. You can use Qxt. Qxt is an extension library for Qt providing a suite of cross-platform utility classes to add functionality not readily available in Qt. It has Global Shortcut (hot keys) which detects key presses even if the application is minimized or hidden.

After compiling Qxt, link your application to it by adding these to your .pro :

CONFIG += qxt
QXT = core gui

And include QxtGlobalShortcut :

#include <QxtGlobalShortcut>

Example usage :

QxtGlobalShortcut* shortcut = new QxtGlobalShortcut(window);
connect(shortcut, SIGNAL(activated()), window, SLOT(toggleVisibility()));
shortcut->setShortcut(QKeySequence("Ctrl+Shift+F"));
like image 31
Nejat Avatar answered Mar 27 '23 13:03

Nejat