View Full Version : How to get keyevent in qt console application
zuoshaobo
7th December 2011, 03:25
my qt application is a console
how to catch the keyevent ?such as a hit key
thank you
Santosh Reddy
7th December 2011, 04:20
You can install an event filter on QApplication, and catch the key hit events.
LINK (http://doc.qt.nokia.com/4.7-snapshot/qobject.html#installEventFilter)
zuoshaobo
7th December 2011, 04:35
I just code it like this:
QCoreApplication *app = new QCoreApplication (argc, argv);
KeyBoardEvent kvt;
qApp->installEventFilter(&kvt);
--------------------------------------------------------------------------------------
KeyBoadEvent:
class KeyBoardEvent:public 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
Santosh Reddy
7th December 2011, 06:47
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.
zuoshaobo
7th December 2011, 06:55
I have no choice I read /dev/input/event1 my keyboard device
Thank you
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.