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
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.
I did a big update yesterday. Started on the new docs too: https://apostolique.github.io/Apos.Input/
There’s is now a built-in tracking system. It makes it much easier to resolve conflicts like:
ICondition run =
new AllCondition(
new KeyboardCondition(Keys.Shift),
new Track.KeyboardCondition(Keys.Right)
);
ICondition walk = new Track.KeyboardCondition(Keys.Right);
if (run.Held()) {
// Run while the buttons are held.
}
if (walk.Held()) {
// Walk while the buttons are held.
}
Without having any conflict betwen the walk and run conditions.
For a more complex example, here is an editor I’m working on, it has two RectEdit objects: https://github.com/Apostolique/Apos.Editor/blob/d2dea9d4d8c2c57f2eb854eac946d4b86bec8f4b/Game/Layer1/GameRoot.cs#L165
Both of their UpdateInput methods are called each frame: https://github.com/Apostolique/Apos.Editor/blob/d2dea9d4d8c2c57f2eb854eac946d4b86bec8f4b/Game/Layer1/GameRoot.cs#L54
To initiate a selection drag we check a mouse button press: https://github.com/Apostolique/Apos.Editor/blob/d2dea9d4d8c2c57f2eb854eac946d4b86bec8f4b/Game/Layer1/RectEdit.cs#L35
Both will reach that code, but only one will be triggered when the mouse is pressed.
I have released version 2.1.0. This version adds static methods for the tracking system.
It is now possible to replicated the run + walk example from above:
if (KeyboardCondition.Held(Keys.LeftShift) &&
Track.KeyboardCondition.Held(Keys.Right)) {
// Run while the buttons are held.
}
if (Track.KeyboardCondition.Held(Keys.Right)) {
// Walk while the buttons are held.
}
Some more docs were written including the InputHelper class: https://apostolique.github.io/Apos.Input/api/InputHelper/