Skip to content

hmlendea/nucixna.input

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Donate Latest Release Build Status License: GPL v3

NuciXNA.Input

Input management for NuciXNA (MonoGame/XNA), with event-driven and polling-based APIs for keyboard and mouse input.

Features

  • 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)

Installation

Get it from NuGet

.NET CLI

dotnet add package NuciXNA.Input

Package Manager

Install-Package NuciXNA.Input

Requirements

  • .NET: net10.0
  • MonoGame DesktopGL (or compatible MonoGame runtime)

Quick Start

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
	}
}

Event API

InputManager exposes the following events:

  • KeyboardKeyPressed
  • KeyboardKeyReleased
  • KeyboardKeyHeldDown
  • MouseButtonPressed
  • MouseButtonReleased
  • MouseButtonHeldDown
  • MouseMoved

Event args include rich state:

  • KeyboardKeyEventArgs: Key, KeyState
  • MouseButtonEventArgs: Button, ButtonState, Location
  • MouseEventArgs: Location, PreviousLocation

Polling API

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.Idle
  • ButtonState.Pressed
  • ButtonState.Released
  • ButtonState.HeldDown

Mouse buttons:

  • MouseButton.Left
  • MouseButton.Right
  • MouseButton.Middle
  • MouseButton.Back
  • MouseButton.Forward

Development

Build

dotnet build NuciXNA.Input.csproj

Run

dotnet run --project NuciXNA.Input.csproj

Test

dotnet test NuciXNA.Input.sln

Contributing

Contributions 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

License

Licensed under the GNU General Public License v3.0 or later. See LICENSE for details.

Contributors

Languages