# Implementing a Custom Mouse Cursor in XNA

I have tried making a custom mouse cursor, hiding the real mouse, taking the real mouse delta and clamping it to a maximum speed. Here is the problem:

• In windowed mode, the "real" mouse cursor moves off the window while the custom mouse cursor (since it's movement is being scaled) is still somewhere inside the game window. This becomes bizarre and is obviously not desired, as clicking at this point means clicking on things outside the game window. Is there any way to accomplish this in windowed mode?

• In fullscreen mode, the "real" mouse cursor is bounded to the edges of the screen. So I get to a point where there is no more mouse delta, yet my custom cursor is still somewhere in the middle of the screen and hence can't move further in that direction. If I wanted to clamp it to the edge of the screen when the real cursor is at the edge, then I would get an abrupt jump to the edge of the screen, which isn't desired either

Any help would be appreciated. I'd like to be able to limit the speed of the mouse.

• Personally, I get really annoyed when games start to play with my mouse. – John McDonald Nov 14 '11 at 18:52
• I can understand that and only turned to the "limit speed" question initially because of the juddering. However, I'm thinking I may want to have multiplayer in the future (where people are "racing" against each other to complete some task), and it would be nice to dissuade them from getting an edge by changing their mouse speed. – idlewire Nov 14 '11 at 19:06
• Then you should probably fix the rules of the game so that having a fast mouse pointer doesn't give a specific advantage. For instance, by implementing a rest time (like Kung-fu chess does) or limiting the number of actions per second. Personally I hate when a game changes my mouse speed, too, and I view such games as lagging. – sam hocevar Nov 14 '11 at 19:58
• Nah, I'm not going to redesign the flow of the game just for this. Maybe I'll just feel flattered if someone goes through the trouble to change their mouse speed just to get an advantage. – idlewire Nov 15 '11 at 4:14

Let's get the obvious stuff out of the way, the tool is there: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.mouse.setposition.aspx

In fullscreen it is easy to use, just do some controlled repositionings and the edge problem is gone. For some other games this could lead to the problem of a trapped mouse in windowed mode, but if you do it just right I think you may solve your problem without upsetting John McDonald:

Whenever the player has moved the mouse too quickly, reposition it to where the game cursor ends up, this way the player can actually still drag the mouse outside the window if so desired, and you don't get the clamping problem until you'd have to cap the cursor movement anyway.

• Yeah, you're right. I personally don't think clamping max speed should be a problem. It'll just feel like old DOS games (that totally slowed down when you moved the mouse to fast, so it feels like you're moving thru tar). If there's motion of say, 5px or more (in x, y independently) then clamp the motion to 5px, and use SetPosition to enforce it – bobobobo Nov 14 '11 at 21:29
• I actually didn't know there was a SetPosition method, so thanks for that. This helped me with my second question, and I realized I should only have asked one question (I may ask the first in a later question). So I've edited it to only reflect the part that you helped answer and marking yours as the answer. – idlewire Dec 2 '11 at 4:35

Vector2 MousePos = Mouse.GetState().Position;