Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've made myself a camera for a DirectX 10 game, and the keyboard control works fine. However, I can't see to get the mouse to work. My biggest problem is I can't find out how to hide the mouse on the display to allow a crosshair (I don't need a visual one) and make movement like it would be in any other common 3D game. I looked at this tutorial, but it doesn't seem to work (The mouse part, that is). Everything I try just seems to result in the camera spinning very fast...

I'm using C++ and DirectInput.

share|improve this question
up vote 4 down vote accepted

You should not use DirectInput. It has been completely deprecated in favor of RAWINPUT.

The answer that uses WM_MOUSEMOVE is not the same as RAWINPUT and I expect it to not be fast enough.

An example using RAWINPUT in a Win32 app is available on my blog

share|improve this answer
Thank you! I was worried about using DirectInput when I had to add some lib files. – smoth190 Oct 28 '11 at 19:23

Edit: Hide/Show mouse using ShowCursor -

Keep resetting the mouse position to the center of the screen if you are working in a relative movement mode and track the movement.

This is pretty much Win32 related not having anything to do with Direct3D. I'm personally using it along side Direct Input which takes care of joysticks / gamepads.

Here is part of the code I'm using

    case WM_MOUSEMOVE:
        //Most other messages are only sent to windows with the current focus
        //but mouse move is sent whenever the mose is hovering over, so we need
        //to ignore it if we don't have focus
        if (!IsWindowActive)

        //X and Y can be relative or absolute
        if (!MouseAbsolute)
            if (IgnoreNextMouseMove)
                IgnoreNextMouseMove = false;
                return 0;

            POINT temp;
            // WM_MOUSEMOVE is 2 shorts, so I think it is safe to assume all mouse cooridnates are words
            int16 MouseX = (WORD)temp.x; 
            int16 MouseY = (WORD)temp.y; 

            //Add new changes to any existing
            //InputDriverInteral resets these to 0 when the axis is read
            uint16 cX = wX + wLength/2;
            uint16 cY = wY + wHeight/2;

            // This is a float array
            Axis[IA_MOUSE_X] += (MouseX - cX) / (float)wLength;
            Axis[IA_MOUSE_Y] += (MouseY - cY) / (float)wHeight;

            // This is a struct array with a float and a bool
            m_AxisMap[IA_MOUSE_X].Value = pDriver->Axis[IA_MOUSE_X];
            m_AxisMap[IA_MOUSE_X].Changed = true;
            m_AxisMap[IA_MOUSE_Y].Value = pDriver->Axis[IA_MOUSE_Y];
            m_AxisMap[IA_MOUSE_Y].Changed = true;

            // Set cursor position will send WM_MOUSEMOVE so we need to ignore that message
            IgnoreNextMouseMove = true;
            SetCursorPos(cX, cY);

            int16 MouseX = LOWORD(lParam); 
            int16 MouseY = HIWORD(lParam); 

            //Get our absolute position within the window
            Axis[IA_MOUSE_X] = (float)MouseX / wLength;

            //Do the same as above but for y (note that windows is top left but we want from bottom left)
            //Get our absolute position within the window
            Axis[IA_MOUSE_Y] = (float)MouseY / wHeight;

            // Flag these axis as changed
            m_AxisMap[IA_MOUSE_X].Value = pDriver->Axis[IA_MOUSE_X];
            m_AxisMap[IA_MOUSE_X].Changed = true;
            m_AxisMap[IA_MOUSE_Y].Value = pDriver->Axis[IA_MOUSE_Y];
            m_AxisMap[IA_MOUSE_Y].Changed = true;

        //Send an event notification

It works pretty well and the only issues I've had is if the user moves the mouse at an insane rate. The windows mouse position is limited to the size of the user's desktop so it cannot capture movement beyond that.

If you want to see the complete code for better context take a look:

share|improve this answer
I was originally using DirectInput to gather mouse input, should I be using Win32 instead? – smoth190 Oct 28 '11 at 0:54
@smoth190 I would recommend it. DirectInput is just a wrapper for Win32 and it makes it somewhat difficult to support absolute mouse controls when your application is running in Windowed mode (World of Warcraft in windowed mode is a great example of what I'm trying to do with my code). – NtscCobalt Oct 28 '11 at 0:59
You should use rawinput – bobobobo Oct 28 '11 at 2:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.