Results 1 to 9 of 9

Thread: Ctrl-C interception

  1. #1
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Ctrl-C interception

    I presume this is a newbie question, but hey, this is the newbie forum, right?

    I have a console application with two threads: a worker thread and a communication thread (communicates over a tcp socket). The program works fine, but if I press ctrl-c, it exits quite raw. What I want is that the worker thread intercepts the ctrl-c and instructs the commthread to send a final message, and then gracefully stop instead of exiting roughly (in the current situation I do not really see how the program flow is going when exiting).
    How do I ensure that the worker thread intercepts the ctrl-c and not the commthread? And how do I intercept the ctrl-C at all?

    I tried this:

    bool MyApp::event(QEvent *evt)
    {
    if (evt->type() == QEvent::KeyPress)
    {
    // check for ctrl-c and if so, handle it and return true
    }
    return QObject::event(evt);
    }

    However it appears that I do not receive any keypress events at all, no matter what I hit on the keyboard...


    Note: I know about the signal aboutToQuit, but I want to make sure that QCoreApplication::exec() returns normally so that anything following that is executed.
    Last edited by T_h_e_o; 25th April 2012 at 16:03.

  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: Ctrl-C interception

    You need to use the signal() call with SIGINT.
    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. #3
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ctrl-C interception

    Quote Originally Posted by wysota View Post
    You need to use the signal() call with SIGINT.
    I figured that in the mean time, but thanks anyway. It works.
    But I'm still stuck with the question: suppose I would want to stop the program if I hit a specific key, not ctrl-C. How do I do that? Polling std::cin from the main loop is ugly. There must be a way to intercept keypress events, right? I tried this:

    Qt Code:
    1. bool MyApp::eventFilter(QObject *obj, QEvent *evt)
    2. {
    3. if (evt->type() == QEvent::keyPress)
    4. {
    5. if ( /* correct key pressed */ )
    6. {
    7. return true;
    8. }
    9. }
    10. return QObject::eventFilter(obj, evt);
    11. }
    12.  
    13.  
    14. bool MyApp::event(QEvent *evt)
    15. {
    16. if (evt->type() == QEvent::keyPress)
    17. {
    18. if ( /* correct key pressed */ )
    19. {
    20. return true;
    21. }
    22. }
    23. return QObject::event(evt);
    24. }
    To copy to clipboard, switch view to plain text mode 
    The class MyApp is derived from QCoreApplication. I tried to call installEventFilter(this) in its constructor (seems a bit weird to install itself as filter but I tried it anyway), but neither of the two listed methods is ever called with a keypress event, whether or not I call installEventFilter.

    So what am I doing wrong?
    Last edited by T_h_e_o; 26th April 2012 at 10:17.

  4. #4
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ctrl-C interception

    Nobody?
    Please, I would really like to know how I can intercept keystrokes!

  5. #5
    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: Ctrl-C interception

    For a key event to be delivered you need to have an active widget that accepts focus. Then this key event is delivered to that widget and will be intercepted by the global event filter.
    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. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Ctrl-C interception

    If you need a platform-independent way of solving your problem, you might need something like ncurses. If you are targeting POSIX-compliant systems, you may use QSocketNotifier to monitor input on stdin. This is a bit primitive, but it should work. For anything fancy I would recommend looking into ncurses (although I have never used it myself).

  7. #7
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ctrl-C interception

    Quote Originally Posted by wysota View Post
    For a key event to be delivered you need to have an active widget that accepts focus. Then this key event is delivered to that widget and will be intercepted by the global event filter.
    Wysota, the application is a console app. There is no widget. Is it in this case impossible to intercept keystrokes with eventFilter?

    Quote Originally Posted by yeye_olive View Post
    If you need a platform-independent way of solving your problem, you might need something like ncurses. If you are targeting POSIX-compliant systems, you may use QSocketNotifier to monitor input on stdin. This is a bit primitive, but it should work. For anything fancy I would recommend looking into ncurses (although I have never used it myself).
    I'd rather not include another lib, but I will have look at QSocketNotifier to see if I understand how to use it to catch keystrokes. Whether it works or not: thanks for the suggestion!

  8. #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: Ctrl-C interception

    Quote Originally Posted by T_h_e_o View Post
    Wysota, the application is a console app. There is no widget.
    Exactly. Thus you will not get any key events. You can use QSocketNotifier on STDIN to get line-buffered input notifications.
    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.


  9. #9
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ctrl-C interception

    Quote Originally Posted by wysota View Post
    Exactly. Thus you will not get any key events. You can use QSocketNotifier on STDIN to get line-buffered input notifications.
    Aha, now I understand the earlier reply from yeye_olive. I don't know the class QSocketNotifier, but I'll look things up (when I have time: I'm too busy to spend much time on this - it's a hobby project, but work prevails!).

    Thanks for the tip!

Similar Threads

  1. CTRL-+ in keyPressEvent
    By The_Fallen in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2011, 22:40
  2. Interception of GET requests to the server
    By artkor in forum Newbie
    Replies: 4
    Last Post: 29th September 2010, 08:59
  3. filterout the Alt+Ctrl+Del
    By sudhansu in forum Qt Programming
    Replies: 6
    Last Post: 25th March 2010, 07:55
  4. disable ALT+CTRL+DEL!!!!
    By phillip_Qt in forum Qt Programming
    Replies: 10
    Last Post: 3rd November 2009, 09:23
  5. CTrl + A
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 23rd October 2008, 09:19

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.