GameInput.Net is a .NET wrapper around Microsoft's **GameInput ** library, a modern input capture system introduced woth the Microsoft Game Development Kit.
You must have Windows 10 or 11 with the GameInput library installed. It is recommended you distribute the GameInput Redistributable with your software to ensure the user has the most recent version. GameInput.Net will look for the re-distributable before the boxed version.
This is included in the **Microsoft.GameInput ** package, though not automatically installed.
Download the package in your favorite IDE such as **Jetbrains Rider ** or **Microsoft Visual Studio ** using the NuGet package manager. You may also install via commandline dotnet tools.
dotnet add package GameInputNet
Compatible with .NET 6.0+ you can instantiate a GameInput object and access all of the native **GameInput ** through the managed wrapper.
using GameInputDotNet;
using GameInputDotNet.Interop.Enums;
using var _gameInput = GameInput.Create();
// Set focus allow capture of inputs regardless of program focus.
_gameInput.SetFocusPolicy(GameInputFocusPolicy.EnableBackgroundInput);
while (true)
{
// Slow down the loop so we don't get spammed.
Thread.Sleep(100);
GameInputReading? reading = null;
try
{
// Get the current frame's "Keyboard" inputs.
reading = _gameInput.GetCurrentReading(GameInputKind.Keyboard);
}
catch (GameInputException)
{
// Handle exceptions and unprepared reading from first frame.
continue;
}
if (reading is null) continue;
using (reading)
{
// Get the current state.
var state = reading.GetKeyboardState();
// Nothing pressed we can jump to the next loop.
if (state.Keys.Count == 0)
continue;
// Report the scan code of each key currently pressed.
Console.Write("Keys Pressed: ");
foreach (var key in state.Keys) Console.Write(key.ScanCode + " ");
Console.WriteLine();
// We can also use this to end our program when the user hits Escape.
if (state.Keys.Any(key => key.ScanCode == 1))
{
Console.WriteLine("Got ESCAPE key, ending program.");
break;
}
}
}If you have issues or recommendations please submit a ticket through the * GameInput.Net GitHub*. The repo also contains additional documentation and examples.