Home > Games Programming, My Thoughts, Programming, XNA > XNA Fuchs InputHandler

XNA Fuchs InputHandler


Introduction:

Each XNA “Game” I program I have to take input, something like the following


KeyboardState prevKeyboardState;

protected override void Update(GameTime gameTime)
{
    KeyboardState keyboardState = Keyboard.GetState();

    if (keyboardState.IsKeyDown(Keys.Left) && prevKeyboardState.IsKeyUp(Keys.Left))
    {
        // Do stuff
    }

    prevKeyboardState = keyboardState;
}

I have to create two KeyboardStates, one for this frame & the other is for the previous frame. Then in most cases I check that some button is pressed only in a frame not in its predecessor…( because even if you press a button and remove your finger quickly the, button will be considered pressed in more than one frame )

Same issue goes for the mouse.

So I thought I should create my own input handler, a friend of mine told me “Don’t you think you’re re-inventing the wheel?” well, he was right, almost :D, I’m inventing a wheel that fits my vehicles :D.

FuchsInputHandler.dll

Of course writing an input handler like this one is very easy :D, but my biggest problem was the names to give for the functions :|, can you give me a shorter name for the function IsKeyDownThisFrameButUpThePreviousFrame :D?

Just kidding, no function with that name :D, below is a list containing some of the functions & properties of the static class InputHandler

void Update(GameTime gameTime) : Needs to be called ONCE at the beginning of the Update function of your main game.

Keboard

bool IsKeyPressed(Keys key) : Returns true if the button is down this frame & was up the previous one, false otherwise.

bool IsKeyDown(Keys key) : Returns true if the button is pressed or held down, false otherwise.

Mouse

bool IsMouseLeftPressed() : Returns true if the left mouse button is down this frame & it was up the previous one, false otherwise.

bool IsMouseLeftDown() : Returns true if the left mouse button is pressed or held down, false otherwise.

And there are similar functions for both right & middle mouse buttons

bool IsMouseMiddlePressed()

bool IsMouseMiddleDown()

bool IsMouseRightPressed()

bool IsMouseRightDown()

Vector2 MousePosition : Returns the current mouse position.

Vector2 PrevMousePosition : Returns the position of the mouse in the previous frame.

MouseState MouseState : Returns the current mouse state ( in case you ever need it ).

MouseState PrevMouseState : Returns the mouse state from the previous frame.

Vector2 MouseVelocity : Returns current mouse position minus its position from the previous frame

There are also some delegates named.

onLeftMouseDown
onRightMouseDown
onMiddleMouseDown

onMouseMove

For example if you want to make a sound every mouse click you just put the code for playing the sound in a function then wire the appropriate delegate with that function.


Demo & Source Code:

I wrote a small demo to test the library:

FuchsInputHandler

FuchsInputHandler Demo

The wheel on the top left rotates with the mouse wheel, the red x is moved by clicking using the left mouse button

Below the red x there’s a counter for the number of clicks, press right mouse button to decrease the counter or press middle mouse button to reset the counter.
The cyan filled arrows will move whenever the arrow keys on the keyboard are pressed, while the red arrows will move once for each button press.

FuchsInputHandler Demo Source Code (VS2010 + XNA4)
FuchsInputHandler dll (.NET4Framework Client Profile)

Final Notes:

I wrote this class for personal use but I thought I should share it, Who knows 🙂 someone might find it useful or improve it.

To be honest I didn’t search for input handlers on the internet, maybe this was a mistake but it’s okay for me.

Advertisements
  1. 14/12/2012 at 5:54 am

    i needed to add mouse handling functionality to my new project, and remembered this project, which you told me about while back.
    thanks for making it. it works like a charm:D

    • Fuchs
      14/12/2012 at 1:04 pm

      Told you it was a good idea 😀 !

  1. No trackbacks yet.

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: