Results 1 to 7 of 7

Thread: Subscribe to Mac OS keypress event

  1. #1
    Join Date
    May 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Subscribe to Mac OS keypress event

    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/qabstractnati...r.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
    Last edited by felli; 11th April 2021 at 14:58.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Subscribe to Mac OS keypress event

    Quote Originally Posted by felli View Post
    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).

  3. #3
    Join Date
    May 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Subscribe to Mac OS keypress event

    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
    Screenshot 2021-04-12 at 17.25.47.jpg

    .h
    Screenshot 2021-04-12 at 17.25.22.jpg

    .mm
    Screenshot 2021-04-12 at 17.25.01.jpg

    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>

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Subscribe to Mac OS keypress event

    Your code should be implementing MyCocoaEventFilter::nativeEventFilter() (declared in your header file) not CocoaNativeEventFilter::nativeEventFilter() (declared nowhere).

  5. #5
    Join Date
    May 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Subscribe to Mac OS keypress event

    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

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Subscribe to Mac OS keypress event

    Quote Originally Posted by felli View Post
    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/document...nitorforevents
    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.

  7. #7
    Join Date
    May 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Subscribe to Mac OS keypress event

    Quote Originally Posted by ChrisW67 View Post
    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/document...nitorforevents
    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.

Similar Threads

  1. TableView keypress event
    By yur0k in forum Newbie
    Replies: 2
    Last Post: 7th October 2020, 20:54
  2. probelm in keypress event
    By iswaryasenthilkumar in forum Newbie
    Replies: 2
    Last Post: 7th April 2015, 08:51
  3. QDialog keypress event
    By bear35645 in forum Qt Programming
    Replies: 6
    Last Post: 6th February 2015, 07:51
  4. how to combine keypress event and mousebuttonpress event?
    By hildebrand in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2009, 00:08
  5. keypress event
    By vishesh in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2007, 15:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.