my qt application is a console
how to catch the keyevent ?such as a hit key
thank you
Printable View
my qt application is a console
how to catch the keyevent ?such as a hit key
thank you
You can install an event filter on QApplication, and catch the key hit events.
LINK
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
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.
I have no choice I read /dev/input/event1 my keyboard device
Thank you