InputStateManager as nuget PCL

Soooo. Here we are again…
Greetz!

Last week I wanted to take a look at Metaballs and therefore I had to deal with inputs (keyboard in this case)… again…
So I wrote the same code I have had written over and over again and then I thought: Stop! Write a library (despite your family wanting to see you more often, you know how it is :slight_smile:) so that you can simply import it in your future showcase-projects via nuget whenever you want to deal with inputs in general.

So here you are. I present: The InputStateManager.
It’s a portable class library (PCL) so you shouldn’t have any difficulty including it in your games.
There is a nuget-package of course called ‘InputStateManager’.
Instructions are in the repo along with code-examples like these here:

if (input.Mouse.Is.Press(Mouse.Button.LEFT)) {...}
  // ...which is equivalent to:
  if(input.Mouse.Is.Down(Mouse.Button.LEFT) && 
     input.Mouse.Was.Up(Mouse.Button.LEFT))) {...}
// Or:
if (input.Mouse.Is.Release(Mouse.Button.LEFT)) {...}
  // ...which is equivalent to:
  if(input.Mouse.Is.Up(Mouse.Button.LEFT) && 
     input.Mouse.Was.Down(Mouse.Button.LEFT))) {...}

var positionDelta = input.Mouse.Is.PositionDelta;
var scrollWheelDelta = input.Mouse.Is.ScrollWheelDelta;
// Or, if you want the values of the last update:
if (input.Mouse.Was.Up(Mouse.Button.MIDDLE)) {...}
...

if (input.Key.Is.Press(Keys.Escape)) {...}
// Query for any SHIFT-key pressed:
if (input.Key.Is.Down(Keys.A) && input.Key.Is.ShiftDown) {...}
// Same for CTRL and ALT:
if (input.Key.Is.CtrlRelease() || input.Key.Is.AltPress()) {...}
// And NumLock and CapsLock:
if (input.Key.Is.NumLockRelease() || input.Key.Is.CapsLockPress()) {..}
...

// Details on GamePad and TouchPanel are in the repo...

As always tell me what you think, make PRs and star the repo if you like it.
I’ve already used it in three smaller projects and I already like it :smile:
Thank you.

4 Likes

Funny, I almost had the same code for handling inputs. I’m just wondering why you’re being inconsistent between shift keys (it’s a property) and other keys (using them as method parameter).

We could / should maybe share our code to improve ourselves?

Yup. That would be great. Share a Gist on Github for example. Let’s discuss that on Github-issues for example.

And… I know… I wrote this because I got tired of re-writing the same piece of code over and over again… and then I tried to tidy it up a bit.

The inconsistency is because it’s inconsistent in the original API and I tried to use as much as possible from there.
The keys-enum doesn’t have a value for CTRL (both of them) or SHIFT (doesn’t matter if left or right one)…
But as always I’m open for discussion. Maybe just open an issue on github and we’ll discuss it there.
Everyone’s invited too. :slight_smile:

Here you go:

You’re missing some classes with this code (mainly BufferedState<T>) which is a circular array with two entries, but you’ll get the grasp of it I think.

Nice. Thx. I’ll take a look at it this weekend.
Btw. Feel free to contribute to the Github repo. If you’re not sure how, just pm.
cu

That code actually looks very nice. The keyboard-part seems to go into the same direction as my text-input project here:


Maybe you’re interested to look into it. It mainly works with the TextInput event and honors special characters (all of them), etc… The textbox then emulates all the hotkeys for cutting, pasting, aso…

I’m not quite sure if the input-class should emulate a key-repetition. I always felt that this is a different use-case. But that’s only me.

Well, my keyboard class tries to handle everything based related to the keyboard, and as text inputs requires typematic settings to kick in, I’ve put the code in the keyboard component. But you’re maybe right, this should be in a separate class…