views:

1323

answers:

4

I'll try to explain my scenario as best i can;

At each application tick I query the current state of the keyboard and mouse and wrap them in individual classes and data structures. For the keyboard it's an array of my Keys enum (one item for each of the keys that are currently pressed) and for the mouse it's a class containing coordinate delta's and bools for each buttons pressed.

I also have a rudimentary state machine managed via a state manager class which maintains the stack and marshalls the states.

All I want to know is, how best to pass the input (snapshots) to the individual states my app can be in at any one time?

I would like to process as much of the input as possible away from the individual states, so as to reduce repeating logic within the states.

Or would it be better to keep the input snapshots as pure as possible and pass them to the states so they can keep they're input-specific-logic hidden?

Note
This structure is similiar to how I imagine a game would work, and although this application is not a game it does need to be processed as quickly as possible.

+2  A: 

Why are you querying the state of the keyboard and mouse with each tick? A much better and traditional solution would be to capture events fired from the keyboard and mouse. That way you only need to update the state when you HAVE to.

Nicholas Mancuso
I'm currently working along these lines so i'll keep you posted how well it turns out.
Adam Naylor
I eventually plumped with a delegate being passed to my input class that gets called only on an event.Cheers!
Adam Naylor
+1  A: 

In XNA, they do this by making the keyboard class static.

KeyboardState state = Keyboard.GetState();

you can then access the current key state with the above line of code.

Jimmy
A: 

You should fire events and capture the arguments. That would be the easiest, and most efficient way to handle this.

class YourClass
{    
    //data members ...

    public void OnKeyboardPress(object sender, EventArgs args)
    {
        //handle your logic capturing the state here
    }
}

//elsewhere
someControl.KeyPress += new KeyPressDelegate(yourClassInstance.OnKeyboardPress);
Nicholas Mancuso
+2  A: 

If you simply query your keyboard and mouse every tick, I can guarantee you'll run into problems. When you query every tick, you'll find that you miss inputs that occur quickly (within the time domain of a single tick). Imagine a situation where the user presses and releases a key/button between two ticks (it will happen more than you think) - you'll completely miss the input.

Particularly in C#, which is completely geared for input events like this, you should be using events.

Quintus