Results 1 to 13 of 13

Thread: Problems with recieving Windows messages using winEvent

  1. #1
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Problems with recieving Windows messages using winEvent

    Hi,
    I have some problems with winEvent messages. I noticed that my program recieve Windows events only when it's active window. Furthermore for some reason it doesnt recognize some of keyboard keys. Looks like winEventFilter function is going to 'cure' this. I found in qt documentation:
    To handle system wide messages, such as messages from a registered hot key, you need to install an event filter on the event dispatcher, which is returned from QAbstractEventDispatcher::instance().
    I reimplemented winEvent function in my MainWindow class

    mainwindow.h
    Qt Code:
    1. ...
    2. private:
    3. virtual bool winEvent(MSG *message, long *result);
    4. ...
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. ...
    2. bool MainWindow::winEvent(MSG *message, long *result)
    3. {
    4. switch(message->message)
    5. {
    6. case WM_KEYDOWN:
    7. qDebug("KEYDOWN");
    8. switch(message->wParam)
    9. {
    10. case Qt::Key_A :
    11. qDebug("- A");
    12. break;
    13.  
    14. case Qt::Key_F12 :
    15. qDebug("- F12");
    16. break;
    17. default:
    18. break;
    19. }
    20. break;
    21. default:
    22. return false;
    23. }
    24. return false;
    25. }
    26. ...
    To copy to clipboard, switch view to plain text mode 

    After pressing A Key:
    KEYDOWN
    - A
    After pressing F12 Key:

    KEYDOWN
    The problem is my stupidity. I tried many times but failed. I need some small example but there is nothing I believe...

    So again: What can I do for my app to recieve winEvents while not active? And why this code can't recognize keys like: F10, F11, F12, PrtSc, Ins, Ctrl? Could you do some tiny example how to handle system wide messages, please?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with recieving Windows messages using winEvent

    You are incorrectly assuming WinAPI key identifiers and Qt key identifiers match.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    rbrafi (31st May 2011)

  4. #3
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with recieving Windows messages using winEvent

    Thank you for your reply! One of problem is solved.

    But is there a way for my app to recieve windows messages when it is not active window?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with recieving Windows messages using winEvent

    I guess you'd have to install a hook using WinAPI calls. This is really out of scope of this forum.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with recieving Windows messages using winEvent

    Thank you again. Guess I can only hope that in future relases of Qt there will be a new function added that will allow it in easy way.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with recieving Windows messages using winEvent

    Why double a function that already exists?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with recieving Windows messages using winEvent

    If you mean hooks... well... I don't like it. I started creating in qt because I can't stand structure of WinApi...

    I think about using QAbstractEventDispatcher in my program. If I get some free-time I'll look into doc and try to work with it.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with recieving Windows messages using winEvent

    I don't see how this would help. You want to intercept messages that are not meant for you, your application won't receive them unless you explicitly request that by installing a hook in Windows message loop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with recieving Windows messages using winEvent

    QAbstractEventDispatcher Doc
    An event dispatcher receives events from the window system and other sources. It then sends them to the QCoreApplication or QApplication instance for processing and delivery.
    The event filter function set here is called for all messages taken from the system event loop before the event is dispatched to the respective target, including the messages not meant for Qt objects.
    That looks fine for me. If I understand correctly it can do the work that hooks do. Still, I have a problem with implementing it into my code... Guess, I have to try again...
    Last edited by rbrafi; 1st June 2011 at 22:39.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with recieving Windows messages using winEvent

    It doesn't say anything about events meant for other applications.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with recieving Windows messages using winEvent

    Ugh... You are right... I'm assuming that "messages not meant for Qt objects" are messages for other applications and I am probably wrong. Just in case I want to give it a try...

    I have problem (again ) with using Event Dispatcher.

    My MainWindow constructor:
    Qt Code:
    1. ui->setupUi(this);
    2. QAbstractEventDispatcher::EventFilter MyEventFilter;
    3. QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance();
    4. eventDispatcher->setEventFilter(MyEventFilter);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool MainWindow::MyEventFilter(MSG *message)
    2. {
    3. switch(message->message)
    4. {
    5. case WM_KEYDOWN:
    6. qDebug("KEYDOWN");
    7. switch(message->wParam)
    8. {
    9. case 0x41 :
    10. qDebug("- A");
    11. break;
    12.  
    13. case VK_F12 :
    14. qDebug("- F12");
    15. break;
    16. default:
    17. break;
    18. }
    19. break;
    20. default:
    21. return false;
    22. }
    23. return false;
    24. }
    To copy to clipboard, switch view to plain text mode 

    Errorlog:
    Qt Code:
    1. In constructor 'MainWindow::MainWindow(QWidget*):
    2. warning:'MyEventFilter' may be used uninitialized in this function
    3. In constructor 'MainWindow::MainWindow(QWidget*):
    4. warning:'MyEventFilter' may be used uninitialized in this function
    To copy to clipboard, switch view to plain text mode 
    It runs, but crashes.

    How should I initialize MyEventFilter? I tried couple of times but effects were even worse than that...

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with recieving Windows messages using winEvent

    QAbstractEventDispatcher::EventFilter is supposed to be a function and you are using it as a variable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    rbrafi (6th June 2011)

  15. #13
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with recieving Windows messages using winEvent

    I decided that it is better to use RegisterHotKey() function from winApi. It saves a lot of time in my case. Still, I feel unsatisfied for some reason Thank you for your advices. They helped a lot.

Similar Threads

  1. PyQt - QGraphicsItem not recieving mouse events
    By megamic in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2011, 10:14
  2. Replies: 1
    Last Post: 12th January 2009, 18:05
  3. problem with Recieving grayscale
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 27th September 2008, 22:51
  4. recieving QProcess::write data
    By node_ex in forum Qt Programming
    Replies: 3
    Last Post: 28th July 2008, 12:18
  5. Windows focus / Windows Shutdown Problems
    By December in forum Qt Programming
    Replies: 6
    Last Post: 22nd October 2007, 14:10

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.