Apos.Input - Input library for MonoGame

I’ve been working on an input library for MonoGame. It makes it easier to setup key rebinding and complex hotkeys. Currently supports mouse, keyboard and gamepad buttons.

Available on GitHub and Nuget:

I’m also writing API documentation for it here: https://apostolique.github.io/Apos.Input/

It’s pretty easy to use:

// Create a condition to toggle fullscreen.
// It should work on either Alt keys with Enter.
var toggleFullScreen = new AllCondition(
    new AnyCondition(
        new KeyboardCondition(Keys.LeftAlt),
        new KeyboardCondition(Keys.RightAlt)
    ),
    new KeyboardCondition(Keys.Enter)
);

// To check if toggleFullscreen is triggered:
if (toggleFullscreen.Pressed()) {
    //Do the fullscreen change.
}

You can create a jump condition using:

var jump = new AnyCondition(
    new KeyboardCondition(Keys.Space),
    new MouseCondition(MouseButton.LeftButton),
    new GamePadCondition(GamePadButton.A, 0)
);

Edit: Update code with latest API changes.

Looks really easy to use :slight_smile:
You should try and implement touch-control as if it was mouse. That way you have to write code once and it’ll be usable on touch-devices as well!

I’m adding touch control soon too. You can already access them in my Helper class: https://github.com/Apostolique/Apos.Input/blob/master/Source/InputHelper.cs#L20

But I want something better so it’s easier to define useful actions on the touch control.

I added a way to handle text input from the user. This way it’s easier to check for text input wherever it’s convenient in the game.

Accessible from InputHelper.TextEvents.

This is useful for coding text boxes.

I added a new ActionTrigger class. This is useful for building extra complex sets of inputs. I can recode my toggle fullscreen from above like this (note I added keybind for gamepad 0):

//Create an action to toggle fullscreen.
//It should work on both Alt keys and Enter.
var fullscreenKeyboard = new ActionKeyboardComposite();
fullscreenKeyboard.AddSet(Keys.Enter).AddNeed(Keys.LeftAlt);
fullscreenKeyboard.AddSet(Keys.Enter).AddNeed(Keys.RightAlt);

var fullscreenGamepad = new ActionGamepadComposite();
fullscreenGamepad.AddSet(s => s[0].Buttons.Back);

var toggleFullscreen = new ActionTrigger();
toggleFullscreen.AddAction(fullscreenKeyboard.Pressed);
toggleFullscreen.AddAction(fullscreenGamepad.Pressed);

//To check if toggleFullscreen is triggered:
if (toggleFullscreen.Triggered()) {
    //Do the fullscreen change.
}

If you want, you can also add in game actions to an ActionTrigger object. For example, you could create a function that returns true when a character in your game steps over a tile and build some sort of input over that.

I have refreshed the whole library’s API. The code is now much simpler. I also finished writing the API documentation. The example above can be rewritten like this:

//Create a condition to toggle fullscreen.
var fullscreenComposite = new ConditionComposite();
//Enter with left alt.
fullscreenComposite.AddSet(Keys.Enter).AddNeed(Keys.LeftAlt);
//Enter with right alt.
fullscreenComposite.AddSet(Keys.Enter).AddNeed(Keys.RightAlt);
//Gamepad's back button.
fullscreenComposite.AddSet(s => s[0].Buttons.Back);

var toggleFullscreen = new ConditionTrigger();
//If either of the above are pressed.
toggleFullscreen.AddCondition(fullscreenComposite.Pressed);

//To check if toggleFullscreen is triggered:
if (toggleFullscreen.Triggered()) {
    //Do the fullscreen change.
}

API Documentation: https://apostolique.github.io/Apos.Input/#File:Condition.cs

I finally took the time to clean up this library.

ConditionComposite becomes AnyCondition .
ConditionSet becomes AllCondition .

They now hold their values in an array instead of a list. Methods to add conditions have been removed.

The preferred way to initialize a condition tree is through a series of constructors:

// Create a condition to toggle fullscreen.
// It should work on either Alt keys with Enter.
var toggleFullScreen = new AllCondition(
    new AnyCondition(
        new KeyboardCondition(Keys.LeftAlt),
        new KeyboardCondition(Keys.RightAlt)
    ),
    new KeyboardCondition(Keys.Enter)
);

// To check if toggleFullscreen is triggered:
if (toggleFullscreen.Pressed()) {
    //Do the fullscreen change.
}

This refactor makes it easier to create complex trees. In the previous iteration, ConditionComposite was a container for ConditionSet . ConditionSet itself was a container for ICondition . Now, the tree doesn’t have this limitation.

I also removed the not needed keys from ConditionSet . I believe such a feature should be provided by a different abstraction layer.

You can mix keyboard, mouse, gamepad using:

var jump = new AnyCondition(
    new KeyboardCondition(Keys.Space),
    new MouseCondition(MouseButton.LeftButton),
    new GamePadCondition(GamePadButton.A, 0)
);

The next step for this library will be to provide examples on how to build a custom keybind menu.