Toolbelt.Blazor.Gamepad 9.0.0

dotnet add package Toolbelt.Blazor.Gamepad --version 9.0.0                
NuGet\Install-Package Toolbelt.Blazor.Gamepad -Version 9.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Toolbelt.Blazor.Gamepad" Version="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolbelt.Blazor.Gamepad --version 9.0.0                
#r "nuget: Toolbelt.Blazor.Gamepad, 9.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Toolbelt.Blazor.Gamepad as a Cake Addin
#addin nuget:?package=Toolbelt.Blazor.Gamepad&version=9.0.0

// Install Toolbelt.Blazor.Gamepad as a Cake Tool
#tool nuget:?package=Toolbelt.Blazor.Gamepad&version=9.0.0                

Blazor Gamepad NuGet Package

Summary

This is a class library that provides gamepad API access for your Blazor apps.

demo movie

Requirements

"Blazor Gamepad" ver.9.x or later supports Blazor versions below.

  • v.6.0, 7.0, 8.0 or later

Note:
If you are using Blazor version 5.0 or earlier, please use "Blazor Gamepad" ver.8.x or earlier.

Both "Blazor WebAssembly App" and "Blazor Server App" are supoorted.

How to install and use?

1. Installation and Registration

Step.1 Install the library via NuGet package, like this.

> dotnet add package Toolbelt.Blazor.Gamepad

Step.2 Register "GamepadList" service into the DI container.

// Program.cs
...
using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this line, and...
...
var builder = WebAssemblyHostBuilder.CreateDefault(args);
...
builder.Services.AddGamepadList(); // <- Add this line.
...

2. Usage in your Blazor component (.razor)

Step.1 Inject the GamepadList service into the component.

@inject Toolbelt.Blazor.Gamepad.GamepadList GamepadList @* <- Add this. *@
...

Step.2 Invoke GetGamepadsAsync() async method to retreive gamepad list, and find a active gamepad object.

After you find it, you can reference gamepad axes and buttons.

Note: GetGamepadsAsync() returns empty list until any gamepad devices are activated. To activate the gamepad, you should do any actions on the gamepad device while the browser's document has focus.

Sample .razor code is here:

@page "/"
@using Toolbelt.Blazor.Gamepad
@using System.Timers
@implements IDisposable
@inject GamepadList GamePadList

@if (this.Gamepad != null) {
  <p>Axes</p>
  <ul>
    @foreach (var ax in _gamepad.Axes) {
      <li>@ax.ToString("#,0.0")</li>
    }
  </ul>

  <p>Buttons</p>
  <ul>
    @foreach (var button in _gamepad.Buttons) {
      <li>@button.Pressed (@button.Value)</li>
    }
  </ul>
}

@code {

  private Gamepad? _gamepad;

  private readonly System.Timers.Timer _timer = new Timer(200) { Enabled = true };

  protected override void OnInitialized() {
    _timer.Elapsed += timer_Elapsed;
  }

  private async void timer_Elapsed(object sender, EventArgs args) {
    var gamepads = await GamePadList.GetGamepadsAsync();
    _gamepad = gamepads.FirstOrDefault();
    if (_gamepad != null) 
      await this.InvokeAsync(() => this.StateHasChanged());
  }

  public void Dispose() {
    _timer.Elapsed -= timer_Elapsed;
    _timer.Dispose();
  }
}

Configuration options

The calling of AddGamepadList() injects the reference of the helper JavaScript file (.js) - which are bundled with this package - into your page automatically.

If you don't want this behavior, you can disable the automatic injections. To do that, please call AddGamepadList() with configuration action like this:

builder.Services.AddGamepadList(options =>
{
  // If you don't want automatic injection of js file, add below;
  options.DisableClientScriptAutoInjection = true;
});

You can inject the helper JavaScript file manually. The URL of that JavaScript file is below:

  • _content/Toolbelt.Blazor.Gamepad/script.min.js

Release Notes

The release notes is here.

License

Mozilla Public License Version 2.0

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.0 750 12/13/2023
8.0.0.1 1,364 9/20/2021
8.0.0 386 9/14/2021
7.0.0.3 1,250 1/13/2020
6.0.0 305 9/6/2019
5.0.0 321 6/16/2019
4.0.0 594 5/2/2019
3.0.0 574 3/9/2019
2.0.0 642 2/10/2019
1.0.0 768 10/12/2018

v.9.0.0
- Breaking change: Drop support for .NET 5 or before.
- Improve: Available for IL trimming.