Results 1 to 5 of 5

Thread: How to get keyevent in qt console application

  1. #1
    Join Date
    Dec 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to get keyevent in qt console application

    my qt application is a console
    how to catch the keyevent ?such as a hit key
    thank you

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to get keyevent in qt console application

    You can install an event filter on QApplication, and catch the key hit events.
    LINK
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Dec 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get keyevent in qt console application

    I just code it like this:
    QCoreApplication *app = new QCoreApplication (argc, argv);
    KeyBoardEvent kvt;
    qApp->installEventFilter(&kvt);
    --------------------------------------------------------------------------------------
    KeyBoadEvent:
    class KeyBoardEventublic QObject
    {
    public :
    KeyBoardEvent()
    {

    }
    bool eventFilter(QObject *o, QEvent *event)
    {

    qDebug()<<"sdfs";



    }
    bool event(QEvent *event)
    {

    qDebug()<<"sdfs";



    return true;

    }

    ~KeyBoardEvent()
    {

    }
    void run()
    {


    }
    };

    just print a "sdfs" one time i hit any key but nothing hanppend

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to get keyevent in qt console application

    my mistake this will not work for QCoreApplication. (as nothing will send key event to QCoreApplication)

    You need to read stdin explicitly using C / C++ calls fscanf(), cin directly from main (before entering event loop), or write a console reader thread (inherit QThread) and read the stdin in the thread and do the required.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Dec 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get keyevent in qt console application

    I have no choice I read /dev/input/event1 my keyboard device
    Thank you

Similar Threads

  1. Console application beginning
    By Raccoon29 in forum Newbie
    Replies: 6
    Last Post: 23rd March 2012, 23:42
  2. about console application
    By jirach_gag in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2012, 11:39
  3. Replies: 2
    Last Post: 21st November 2010, 18:03
  4. QT Console application QTextStream
    By Lis in forum Qt Programming
    Replies: 2
    Last Post: 6th November 2010, 05:18
  5. print something on console application
    By cemtopkaya in forum Qt Programming
    Replies: 2
    Last Post: 10th June 2010, 13:41

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
  •  
Qt is a trademark of The Qt Company.