Input management for NuciXNA (MonoGame/XNA), with event-driven and polling-based APIs for keyboard and mouse input.
- Keyboard input events: pressed, released, held-down
- Mouse input events: button pressed/released/held-down and movement
- Polling helpers for "all" and "any" key/button checks
- Frame-based state tracking (
Pressed,Released,HeldDown,Idle) - Lightweight singleton API (
InputManager.Instance)
dotnet add package NuciXNA.InputInstall-Package NuciXNA.Input- .NET:
net10.0 - MonoGame DesktopGL (or compatible MonoGame runtime)
Call Update once per frame (typically from your Game.Update) and subscribe to events.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using NuciXNA.Input;
public class Game1 : Game
{
protected override void Initialize()
{
InputManager.Instance.KeyboardKeyPressed += OnKeyboardKeyPressed;
InputManager.Instance.MouseButtonPressed += OnMouseButtonPressed;
InputManager.Instance.MouseMoved += OnMouseMoved;
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
InputManager.Instance.Update(Window);
if (InputManager.Instance.IsKeyDown(Keys.LeftControl, Keys.S))
{
// Handle Ctrl+S
}
if (InputManager.Instance.IsAnyMouseButtonDown())
{
// At least one mouse button is currently down
}
base.Update(gameTime);
}
private static void OnKeyboardKeyPressed(object sender, KeyboardKeyEventArgs e)
{
if (e.Key == Keys.Escape)
{
// Handle Escape
}
}
private static void OnMouseButtonPressed(object sender, MouseButtonEventArgs e)
{
// e.Button, e.ButtonState, e.Location
}
private static void OnMouseMoved(object sender, MouseEventArgs e)
{
// e.Location, e.PreviousLocation
}
}InputManager exposes the following events:
KeyboardKeyPressedKeyboardKeyReleasedKeyboardKeyHeldDownMouseButtonPressedMouseButtonReleasedMouseButtonHeldDownMouseMoved
Event args include rich state:
KeyboardKeyEventArgs:Key,KeyStateMouseButtonEventArgs:Button,ButtonState,LocationMouseEventArgs:Location,PreviousLocation
Common polling methods:
IsKeyDown(params Keys[] keys)IsAnyKeyDown()IsAnyKeyDown(params Keys[] keys)IsMouseButtonDown(params MouseButton[] buttons)IsAnyMouseButtonDown()IsAnyMouseButtonDown(params MouseButton[] buttons)
Button state model:
ButtonState.IdleButtonState.PressedButtonState.ReleasedButtonState.HeldDown
Mouse buttons:
MouseButton.LeftMouseButton.RightMouseButton.MiddleMouseButton.BackMouseButton.Forward
dotnet build NuciXNA.Input.csprojdotnet run --project NuciXNA.Input.csprojdotnet test NuciXNA.Input.slnContributions are welcome.
When contributing:
- keep the project cross-platform
- preserve the existing public API unless a breaking change is intentional
- keep the changes focused and consistent with the current coding style
- update the documentation when the behavior changes
- include tests for any new behavior
Licensed under the GNU General Public License v3.0 or later. See LICENSE for details.
