I'm using C# XNA 4.0 Refresh.

I'm working to create an InputHandler Game Component for my XNA game to handle all of the keyboard and mouse input.

I used SixOfElevens RPG Game Tutorial (Here - http://xnagpa.net/xna4/rpgtutorials/xna4rpgtutorial1.pdf) to create the InputHandler.

Currently, as shown in the guide it can handle keyboard input and has methods for performing an action when a key is down, pressed, or released.

I added mouse input into this InputHandler so that it gets the current and previous state of the mouse.

My problem though is that I can't figure out how to make working methods to perform actions when a mouse button is down, released, or pressed.

For example: With the keyboard input here is the section that handles performing and action when a key is released after being pushed.

public static bool KeyReleased(Keys key)
        return keyboardState.IsKeyUp(key) &&

I've tried every way I could think of or find to get a similar thing working with the mouse.

I've seen this posting ( http://www.techpowerup.com/forums/threads/sample-code-c-xna-mouse-control-tip.57633/ ) and while it checking the button presses its doing it right at the moment when the action is to be performed. I need a way to pass it through the InputHandler and return it like the keyboard example shown above. Otherwise I'll be stuck having my keyboard input handled neatly and my mouse inputs handled messily all over the place.

Anyone out there know of a way to implement this like I need?

  • \$\begingroup\$ To clarify, you don't want if (prevMouseState.Left == ButtonState.Pressed && currMouseState.Left == ButtonState.Released).. correct? \$\endgroup\$
    – Inisheer
    Mar 9, 2014 at 23:48
  • \$\begingroup\$ @Inisheer Yeah that's basically what I'm looking for, just using it in a slightly different way. I need a way to compare the current and previous state of the mouse so I can check if the mouse button was pressed and then released. I know how to do that by using the code you posted in your comment. The thing I don't know how to do though is to perform that in the same style as the keyboard example given in the post. I don't know how to pass it into the InputHandler and get the result via "return". I'm still new at all this, but I think that the terminology for what I'm trying to do is to use it \$\endgroup\$
    – user43194
    Mar 10, 2014 at 1:01

1 Answer 1


You don't need to pass anything into a method. Much like you did for the keyboard in SixOfEleven's tutorial you would create mousestate fields like :

    private static MouseState mouseState;
    private static MouseState lastMouseState;

you would then (after the mousestates were initialized properly) access methods specific to each button or action instead of using the enums provided like those provided for your keyboard.

For example, I have a method that checks if the right mouse button is down and returns true or false.

    public static bool RightButtonDown()
        return mouseState.RightButton == ButtonState.Pressed;

Released is:

    public static bool RightButtonReleased()
        return mouseState.RightButton == ButtonState.Released &&
            lastMouseState.RightButton == ButtonState.Pressed;

Hope that helps.

  • \$\begingroup\$ That does more than just help! That was exactly what I was looking for. I was so close to that with my early attempts before I came here, but I was messing it up. I kept thinking that I needed a variable like the (Keys key) part in the keyboard example. I kept trying to add (ButtonState button) to the mouse properties I was creating. I knew I was doing something dumb with my implementation, but sadly I couldn't figure out what. Thanks again for the help! My InputHandler can now check the keyboard keys or mouse buttons and perform an action when they are down, pressed, or released. \$\endgroup\$
    – user43201
    Mar 10, 2014 at 8:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .