Hello Qt experts,
I have created an add-on for a game (I use Visual Studio 2008 C++ and Qt 4.6.2.). It works fine!
Now I want to receive events from keystrokes when in-game (non-windows mode?!). The solution I have made works, but only when the game is not running
I think it has something to do with the events not being sent to my window any more, so I would like to dig deeper into the Windows event-mechanism.
The code I made is this:
Class-Header:
#ifndef FACEAPP_H
#define FACEAPP_H
#include <QApplication>
{
Q_OBJECT
public:
FaceApp
( int &argc,
char **argv
) : QApplication( argc, argv
) {} ~FaceApp() {}
void SetupEventFilter( MyClass *window );
protected:
bool winEventFilter( MSG * msg, long * result );
};
#endif // FACEAPP_H
#ifndef FACEAPP_H
#define FACEAPP_H
#include <QApplication>
class FaceApp : public QApplication
{
Q_OBJECT
public:
FaceApp( int &argc, char **argv ) : QApplication( argc, argv ) {}
~FaceApp() {}
void SetupEventFilter( MyClass *window );
protected:
bool winEventFilter( MSG * msg, long * result );
};
#endif // FACEAPP_H
To copy to clipboard, switch view to plain text mode
Implementation:
#include "FaceApp.h"
#include "windows.h"
#include <QDebug>
//
// Override the Application MessageFilter, to receive messages from the game(s)
//
bool FaceApp::winEventFilter( MSG * msg, long * result )
{
int msgType = msg->message; // test line
if (msgType == WM_HOTKEY) {
switch ( msg->wParam ) {
case 777:
qDebug() << "FaceApp::winEventFilter says: HOME pressed";
break;
case 778:
qDebug() << "FaceApp::winEventFilter says: END pressed";
break;
default:
qDebug() << "FaceApp::winEventFilter says: unknown HotKey pressed";
break;
}
}
return( false );
}
//
// Setup the EventFilter
//
void FaceApp::SetupEventFilter( MyClass *window ) {
if ( RegisterHotKey( window->winId(), 777, MOD_WIN, VK_HOME ) ) {
qDebug() << "FaceApp::SetupEventFilter says: RegisterHotKey HOME =" << VK_HOME;
}
if ( RegisterHotKey( window->winId(), 778, MOD_WIN, VK_END ) ) {
qDebug() << "FaceApp::SetupEventFilter says: RegisterHotKey END =" << VK_END;
}
if (evtdis != NULL) {
qDebug() << "FaceApp::SetupEventFilter says: EventDispatcher found!";
}
}
#include "FaceApp.h"
#include "windows.h"
#include <QDebug>
//
// Override the Application MessageFilter, to receive messages from the game(s)
//
bool FaceApp::winEventFilter( MSG * msg, long * result )
{
int msgType = msg->message; // test line
if (msgType == WM_HOTKEY) {
switch ( msg->wParam ) {
case 777:
qDebug() << "FaceApp::winEventFilter says: HOME pressed";
break;
case 778:
qDebug() << "FaceApp::winEventFilter says: END pressed";
break;
default:
qDebug() << "FaceApp::winEventFilter says: unknown HotKey pressed";
break;
}
}
return( false );
}
//
// Setup the EventFilter
//
void FaceApp::SetupEventFilter( MyClass *window ) {
if ( RegisterHotKey( window->winId(), 777, MOD_WIN, VK_HOME ) ) {
qDebug() << "FaceApp::SetupEventFilter says: RegisterHotKey HOME =" << VK_HOME;
}
if ( RegisterHotKey( window->winId(), 778, MOD_WIN, VK_END ) ) {
qDebug() << "FaceApp::SetupEventFilter says: RegisterHotKey END =" << VK_END;
}
QAbstractEventDispatcher *evtdis = QAbstractEventDispatcher::instance();
if (evtdis != NULL) {
qDebug() << "FaceApp::SetupEventFilter says: EventDispatcher found!";
}
}
To copy to clipboard, switch view to plain text mode
Main:
int main(int argc, char *argv[])
{
FaceApp a(argc, argv);
//
// Create the Main Window and DeskTop and Exec!
//
MyClass w;
a.SetupEventFilter(&w);
w.move(desktop.screenGeometry().width()/2-w.width()/2, 10);
w.show();
qApp->exec();
return 0;
}
int main(int argc, char *argv[])
{
FaceApp a(argc, argv);
//
// Create the Main Window and DeskTop and Exec!
//
MyClass w;
a.SetupEventFilter(&w);
QDesktopWidget desktop;
w.move(desktop.screenGeometry().width()/2-w.width()/2, 10);
w.show();
qApp->exec();
return 0;
}
To copy to clipboard, switch view to plain text mode
When I run my program, the HotKey events do arrive fine. If I minimize my main-window, they still do. After starting another program, like Outlook, the HotKeys still make it through. However, when I start the game and press the HotKeys, nothing arrives any more.
The Help of winEventFilter suggests I use the QAbstractEventDispatcher to receive system wide keystrokes. As you can see I have tried it, but don't know how to proceed. Calling the instance(), I can. But the what? Where do I put the code for receiving the events?
Any help is appreciated!
Bookmarks