PDA

View Full Version : Subscribe to Mac OS keypress event



felli
11th April 2021, 12:46
Hi,
I'm trying basically to detect a keypress when my qt applikation is not in focus.
Is this even possible just using Qt?

I found this example:
https://doc.qt.io/qt-5/qabstractnativeeventfilter.html#details

But this doesn't seem to work, getting compile errors.


I would be very grateful if anyone in here could help me out :)

ChrisW67
12th April 2021, 08:57
I would be very grateful if anyone in here could help me out :)

Your link does not point at a complete program and we cannot see your code, compilation errors, or what platform(s) you are targeting so it is difficult to help there.

I do not know exactly what can and cannot be seen through these native event interfaces on the various p[latforms.
I suspect that you will not see every keypress on Windows but there are separate hooks you could try to use (see SetWindowsHookEx).

felli
12th April 2021, 16:33
Hi Chrisw67,

Thank you for you reply. The link was all that I could find unfortunately.
Please see the images below to see the project setup and compile errors.

.pro
13627

.h
13628

.mm
13629

I'm developing on macOS Big Sur, the target platforms are Mac OS and windows 10.
Any help would very appreciated :)

<code>
/Volumes/Data/qt/nativeEvent/mycocoaeventfilter.mm:4: error: use of undeclared identifier 'CocoaNativeEventFilter'
../nativeEvent/mycocoaeventfilter.mm:4:6: error: use of undeclared identifier 'CocoaNativeEventFilter'
bool CocoaNativeEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *)
^
</code>

ChrisW67
13th April 2021, 03:04
Your code should be implementing MyCocoaEventFilter::nativeEventFilter() (declared in your header file) not CocoaNativeEventFilter::nativeEventFilter() (declared nowhere).

felli
17th April 2021, 09:56
thanks for the feedback ChrisW67. I thought maybe the CocoaNativeEventFilter::nativeEventFilter() was implemented <Appkit.h>, updated that and getting other compile errors now...

But my main question is how I can detect keypress events when my application is not in focus, is this possible with Qt?

thank you

ChrisW67
18th April 2021, 01:27
But my main question is how I can detect key press events when my application is not in focus, is this possible with Qt?
It is not really a Qt question: Qt deals with the key presses the operating system sends it when it has focus. The question is whether the OS gives you a way to intercept key presses regardless of where they would naturally flow.

NSEvent addGlobalMonitorForEvents() looks promising to turn on a capture:
https://developer.apple.com/documentation/appkit/nsevent/1535472-addglobalmonitorforevents
Exactly how you access this in your C++ code is not something I am familiar with although I expect that the resulting events should be seen in the Qt event filter.

felli
20th April 2021, 14:23
It is not really a Qt question: Qt deals with the key presses the operating system sends it when it has focus. The question is whether the OS gives you a way to intercept key presses regardless of where they would naturally flow.

NSEvent addGlobalMonitorForEvents() looks promising to turn on a capture:
https://developer.apple.com/documentation/appkit/nsevent/1535472-addglobalmonitorforevents
Exactly how you access this in your C++ code is not something I am familiar with although I expect that the resulting events should be seen in the Qt event filter.

Thank you ChrisW67, just the answer I needed. I will look into that. :)