Important: Please read the Qt Code of Conduct -

QEventDispatcherWin32 does not handle WM_INPUT messages properly - BUG?

  • Hi,

    I need to integrate "3D Connexion Space Navigator ": into my Qt Win32 application. 3D Connexion recommends that developers implement the 3D mouse integration with using Raw Input API. To use the Raw input API, you first need to register the input device and assign a window handle of the window that should receive WM_INPUT messages. Because I would like to treat 3D mouse events in the same way like QMouseEvent I need to assign the internal window of the QEventDispatcherWin32. This internal message window receives all Win32 messages and forwards the messages to the event dispatcher object where an application can install an event filter to handle the messages. Because Qt does not provide any way to get the handle of the internal core window, I wrote my own function to get this handle:

    @HWND QSpaceNavigator::getNativeAppWindow()
    HWND tempHwnd = 0;
    // Grab the first window handle that Windows finds:
    tempHwnd = FindWindow(0, 0);
    HINSTANCE hInstance = qWinAppInst();
    // Check if no parent for this window
    if (GetParent(tempHwnd) == 0)
    // check if window application instance is our Qt application
    HINSTANCE AppInstance = (HINSTANCE) GetWindowLong(tempHwnd,
    if (AppInstance == hInstance)
    #ifdef GWLP_USERDATA
    QAbstractEventDispatcher *q = (QAbstractEventDispatcher *)GetWindowLongPtr(
    QAbstractEventDispatcher *q = (QAbstractEventDispatcher *)GetWindowLong(
    tempHwnd, GWL_USERDATA);
    if (q == QAbstractEventDispatcher::instance())
    return tempHwnd;
    tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT);
    while (tempHwnd != 0);
    return tempHwnd;

    The function getNativeAppWindow() works fine. I checked this by printing the name of the window and shows QEventDispatcherWin32_Internal_Widget.... Now I used this window handle to register it with my raw input device:

    @bool QSpaceNavigator::initializeRawInput()
    static RAWINPUTDEVICE SpaceNavigatorDevice =
    { 0x01, 0x08, RIDEV_INPUTSINK, 0x00 };

    const uint32_t NumberOfDevices = 1;
    const uint32_t DeviceStructSize = sizeof(SpaceNavigatorDevice);

    SpaceNavigatorDevice.hwndTarget = getNativeAppWindow();
    std::cout << "EventDispatcher HWND: " << SpaceNavigatorDevice.hwndTarget;

    if(RegisterRawInputDevices(&SpaceNavigatorDevice, NumberOfDevices,
    std::cout << "Raw input device registered successfully!" << std::endl;
    return true;
    return false;

    Now I used the function:

    @QAbstractEventDispatcher::setEventFilter ( EventFilter filter )@

    to install my own event filter that receives and handles WM_INPUT messages:

    @bool spaceNavigatorEventFilter(void* msg, long* result)
    std::cout << "Message received " << static_cast<PMSG>(message)->message << std::endl;
    std::cout << "Window: " << static_cast<PMSG>(message)->hwnd << std::endl;
    return QSpaceNavigator::instance()->processSpaceNavigatorEvent(static_cast<PMSG>(msg));

    So normally this should work properly and spaceNavigatorEventFilter() function should receive all events and then handles WM_INPUT events of space navigator device.

    I created a small test application based on QMainWindow to check if this filter works. In the main window I placed two QSpinBoxes. When I start the application and move my mouse over the main window, I can see a lot of mouse messages on my console printed by std::cout lines in my event filter. That shows that the event filter works. When I move my 3D space navigator nothing happens - no messages arrive in my event filter. Now, if I move my normal mouse again, a burst of messages from 3D space navigator arrive in the message filter - that means only if I move my mouse, the WM_INPUT messages that are sent from the 3D space navigator previously arrive in my event filter.

    If I click into one of the two QSpinBoxes, that means one spin box gets the input focus, then I can move the space navigator and I can see that WM_INPUT messages from the 3D device arrive in my event filter. If I print the hwnd field of the WM_INPUT message, I can see that the window handle is the window handle of the QEventDispatcherWin32 internal window. That shows, that the raw input device is properly registered with the internal event dispatcher window.

    My question that may only get answered from a troll that knows the Qt event dispatching in detail: Why do normal mouse messages arrive in my event filter all the time I move my mouse and why do WM_INPUT messages of the windows raw API arrive only in my event filter if a widget has the input focus in the active window? This is quite strange and makes it impossible to use RAW input API with Qt because it would force the user to first click into a widget that can receive input focus before he can use the 3D space navigator.

    Sorry for the long post but I spent two days now trying to integrate 3D space navigator in my Qt application and did not find a working solution yet. Is there any best practice how to integrate Win32 raw input API devices into Qt?

  • As far as I know there is an 'spnav' library that handles this (my project searches for spnav.h) and the open source app suite 'koffice' uses this to handle the navigator so its known to work.

    See for example code.

  • Thank you for this hint. Unfortunately the "": driver is no option for us for two reasons:

    1. It is not in production quality.

    2. It requires "": which is also in beta state and requires .NET Framework 2.0. We would like to avoid any dependency to .NET Framework.

    I will try to work around the issue by creating a second message pump in a different native Win32 thread that has its own message window and its own windows procedure. But this still does not answer the question why QEventDispatcherWin32 does not handle WM_INPUT messages properly.

  • I have been having the same problem trying to get the 3D Connexion mouse working and I think I have discovered where the problem is in QEventDispatcherWin32.

    I am using Qt 4.6.2 with VisualStudio 2005 on Windows XP. The problem I was having was that the WM_INPUT messages were not being received by window/event filter the until some other event such as a mouse or timer event occurred. I could move the 3D mouse and nothing would happen then move the normal mouse and all the 3D mouse events would arrive.

    I traced the problem to
    @MsgWaitForMultipleObjectsEX(nCount, pHandles, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE);@

    In the processEvents function of QEventDispatcherWin32. This was blocking and not waking on WM_INPUT events.

    The windows documentation for this notes that QS_ALLINPUT, which includes QS_INPUT, "does not include QS_RAWINPUT in Windows2000." Therefore I think the problem is that the binary Qt installer is built to work on all windows platforms, so during the compile _WIN32_WINNT is defined to something less that WinXP (0x501) so that raw-input messages are not handled.

    I re-compiled Qt passing -D _WIN32_WINNT=0x501 to configure and now the WM_INPUT messages are being passed to the event filter when the 3D mouse is moved.

    So the options seem to be

    • Recompile Qt with _WIN32_WINNT=0x501 so that XP (and later) functions are enabled
    • Use a timer to unblock the event loop at regular intervals so that the messages get processed. I am not sure yet what effect this will have on the smoothness of the mouse operation.

    I also found that just using the winId() function of the main window for the HWND to use for registering the device also worked fine.

  • Hi David,

    hey thank you very much for this solution. We use a self-compiled Qt installation here so it should be no problem to recompile it with _WIN32_WINNT=0×501.

    We worked around the problem by creating a native Win32 DLL (without Qt) that creates an internal win32 message window in its own thread that receives the messages from Space Navigator device. Then we wrote a Qt wrapper for this DLL that translates the messages into Qt events an created a QTDx device.

    But your solution makes it much easier to use Space Navigator from Qt - thank you :O)


  • Hi,

    can anybody of you post an example code which shows how to implement the spacenavigator in qt?

    Thank you very much

  • I wrote an slightly updated version of my comments above and added some sample code of a Qt class to get the data from the mouse on Windows at:


    With Qt 4.7.1 it seems to work without having to re-complile the Qt.

  • @David Dibben: Thank you very much for the sample code. I tried to compile it with the qt eclipse integration but it stops during compiling the file "Mouse3DInput.cpp" in line around 345 "pri = NEXTRAWINPUTBLOCK(pri);" complaining that "NEXTRAWINPUTBLOCK was not declared in this scope".

    I added the line in

    @LIBS += -lUser32 -LC:\programs\Microsoft SDKs\Windows\v6.0A\Lib@

    added user32.lib and the paths to the lib und include directories of Microsofts SDKs.

    Is there anything missing?

    [EDIT: code formatting, Volker]

  • NEXTRAWINPUTBLOCK is a windows macro, defined in winuser.h which should be included with windows.h
    See -

    Which compiler are you using? I am guessing that it is mingw. I don't have experience using g++ on Windows - I compiled the sample using MS VC++ 2008 Express.

    The windows headers should be available with mingw as far as I know, but the windows headers are full of conditional statements so it is possible that something has to be defined for the macros to show up correctly.

  • Yes I use mingw and did the following changes in your sourcecode:

    I added this in Mouse3DInput.cpp

    @#define RAWINPUT_ALIGN(x) (((x) + sizeof(DWORD) - 1) & ~(sizeof(DWORD) - 1))
    #define NEXTRAWINPUTBLOCK(ptr) ((PRAWINPUT)RAWINPUT_ALIGN((ULONG_PTR)((PBYTE)(ptr) + (ptr)->header.dwSize)))@

    and this

    @#include "C:\Qt\2010.05\mingw\include\windows.h"
    #include "C:\Qt\2010.05\mingw\include\winuser.h"@

    Then I found a difference between Microsoft's WinUser.h and mingw's winuser.h (see following):
    typedef struct tagRAWHID {
    DWORD dwSizeHid;
    DWORD dwCount;
    BYTE bRawData;
    I changed it to @bRawData[1]@ like in Microsoft's WinUser.h. I don't know why it works with the range of one, because in the sourcecode you'll find the command
    @pRawInput->data.hid.bRawData[1] == 0x01@
    so it must be at least 2, right?

    Additionally I added
    CONFIG += console

    DEFINES += _WIN32_WINNT="0x0501"
    DEFINES += _WIN32_WINDOWS="0x0501"

    INCLUDEPATH += "C:\Qt\2010.05\mingw\include" \

    LIBS += -luser32 -L"C:\Qt\2010.05\mingw\lib"

    to, but I am not shure if I need all of that.

    After that I was able to compile it. At the moment I am not sure if the translation works correctly, because the data appears too fast in the textboxes, so I will change the program that it saves the data in a textfile.

  • Hi,

    I recorded some data which my program (original written by David Dibben) received from the spacenavigator. I made translational movements at first - followed by rotations. There is only noise on the x-axis and no reaction on y- and z-axis. But you can see the translational movements in the last three plots (plots of the rotations). Does anybody has any idea what's going wrong?


    The horizontal axis shows the number of received events - it's not the time!

  • Hi,

    I switched to Visual Studio and your code worked out of the box.

    Thank you for the sample code, David Dibben!

Log in to reply