tags:

views:

36

answers:

1

I'm writing an XNA application which requires input directed to only one input event handler, which is the object that is currently locked, i.e. game, menu, console, etc.

What is the best way to do this? I've thought about creating an IInputReceiver interface (with methods such as HandleInput) that attaches into a central input manager which only sends input events to the locked input receiver.

Just wondering about what other people do with input handling.

+1  A: 

The easiest way - and the way that XNA itself handles things - is making input global (using static classes). In your Update function for your Game/Menu/Console, simply query the input state.

You probably shouldn't be calling Update on more than one of these objects at the same time. If you have to, for some reason, I recommend having a separate InputUpdate function on each that reads the input state.

If you need "key-went-up/down" handling - which in XNA is generally done by storing the last state and the current state - I find it is also easiest to store these globally, and "pump" it at the start of your Game.Update function.

This may not sound particularly fancy - but this is input for a game. It doesn't have to be fancy!

I don't really understand what you mean by "lockable" - but I feel I should point out that XNA input (reads) must be done on the main thread.

Andrew Russell