Toolbelt.Blazor.HotKeys 10.0.1

Configuration-centric keyboard shortcuts for your Blazor apps.

Install-Package Toolbelt.Blazor.HotKeys -Version 10.0.1
dotnet add package Toolbelt.Blazor.HotKeys --version 10.0.1
<PackageReference Include="Toolbelt.Blazor.HotKeys" Version="10.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolbelt.Blazor.HotKeys --version 10.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Toolbelt.Blazor.HotKeys, 10.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Toolbelt.Blazor.HotKeys as a Cake Addin
#addin nuget:?package=Toolbelt.Blazor.HotKeys&version=10.0.1

// Install Toolbelt.Blazor.HotKeys as a Cake Tool
#tool nuget:?package=Toolbelt.Blazor.HotKeys&version=10.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Blazor HotKeys NuGet Package

Summary

This is a class library that provides configuration-centric keyboard shortcuts for your Blazor apps.

movie

You can declare associations of keyboard shortcut and callback action, like this code:

// The method "OnSelectAll" will be invoked 
//  when the user typed Ctrl+A key combination.
this.HotKeysContext = this.HotKeys.CreateContext()
  .Add(ModKeys.Ctrl, Keys.A, OnSelectAll)
  .Add(...)
  ...;

This library was created inspired by "angular-hotkeys".

Supported Blazor versions

"Blazor HotKeys" ver.9.x or later supports both Blazor WebAssembly and Blazor Server.

Supported Blazor versions are as below.

  • v.3.1
    • including previews and release candidates.
  • v.3.2
    • including previews and release candidates.
  • v.5.0
    • including previews and release candidates.

How to install and use?

1. Installation and Registration

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

> dotnet add package Toolbelt.Blazor.HotKeys

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

If the Blazor version of the project is ver.3.1 preview 4 or earlyer, you should add the code into ConfigureService method in the Startup class of your Blazor application.

using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this line, and...
...
public class Startup
{
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddHotKeys(); // <- Add this line.
    ...

If the Blazor version of the project is ver.3.2 preview 1 or later, you should add the code into Main method in the Program class of your Blazor application.

using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this line, and...
...
public class Program
{
  public static async Task Main(string[] args)
  {
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    ...
    builder.Services.AddHotKeys(); // <!- Add this line.
    ...

2. Usage in your Blazor component (.razor)

Step.1 Implement IDisposable interface to the component.

@implements IDisposable @* <- Add this at top of the component.  *@
...

@code {
  ...
  public void Dispose() // <- Add "Dispose" method.
  {
  }
}

Step.2 Open the Toolbelt.Blazor.HotKeys namespace, and inject the HotKeys service into the component.

@implements IDisposable
@using Toolbelt.Blazor.HotKeys @* <- Add this, and ... *@
@inject HotKeys HotKeys @* <- And add this. *@
...

Step.3 Invoke CreateContext() method of the HotKeys service instance to create and activate hot keys entries at startup of the component such as OnInitialized() method.

You can add the combination with key and action to the HotKeysContext object that is returned from CreateContext() method, using Add() method.

Please remember that you have to keep the HotKeys Context object in the component field.

@code {

  HotKeysContext HotKeysContext;

  protected override void OnInitialized()
  {
    this.HotKeysContext = this.HotKeys.CreateContext()
      .Add(ModKeys.Ctrl|ModKeys.Shift, Keys.A, FooBar, "do foo bar.")
      .Add(...)
      ...;
  }

  void FooBar() // <- This will be invoked when Ctrl+Shift+A typed.
  {
    ...
  }
}

Note.1: You can also specify the async method to the callback action argument.

Note.2: The method of the callback action can take an argument which is HotKeyEntry object.

Step.4 Destroy the HotKeysContext when the component is disposing, in the Dispose() method of the component.

@code {
  ...
  public void Dispose()
  {
    this.HotKeysContext.Dispose(); // <- Add this.
  }
}

The complete source code (.razor) of this component is bellow.

@page "/"
@implements IDisposable
@using Toolbelt.Blazor.HotKeys
@inject HotKeys HotKeys

@code {

  HotKeysContext HotKeysContext;

  protected override void OnInitialized()
  {
    this.HotKeysContext = this.HotKeys.CreateContext()
      .Add(ModKeys.Ctrl|ModKeys.Shift, Keys.A, FooBar, "do foo bar.");
  }

  void FooBar()
  {
    // Do something here.
  }

  public void Dispose()
  {
    this.HotKeysContext.Dispose();
  }
}

Limitations

No "Cheat Sheet"

Unlike "angular-hotkeys", this library doesn't provide "cheat sheet" feature, at this time.

Instead, the HotKeysContext object provides Keys property, so you can implement your own "Cheat Sheet" UI, like this code:

<ul>
    @foreach (var key in this.HotKeysContext.Keys)
    {
        <li>@key</li>
    }
</ul>

The rendering result:

- Shift+Ctrl+A: do foo bar.
- ...

Release Note

Release notes

License

Mozilla Public License Version 2.0

Blazor HotKeys NuGet Package

Summary

This is a class library that provides configuration-centric keyboard shortcuts for your Blazor apps.

movie

You can declare associations of keyboard shortcut and callback action, like this code:

// The method "OnSelectAll" will be invoked 
//  when the user typed Ctrl+A key combination.
this.HotKeysContext = this.HotKeys.CreateContext()
  .Add(ModKeys.Ctrl, Keys.A, OnSelectAll)
  .Add(...)
  ...;

This library was created inspired by "angular-hotkeys".

Supported Blazor versions

"Blazor HotKeys" ver.9.x or later supports both Blazor WebAssembly and Blazor Server.

Supported Blazor versions are as below.

  • v.3.1
    • including previews and release candidates.
  • v.3.2
    • including previews and release candidates.
  • v.5.0
    • including previews and release candidates.

How to install and use?

1. Installation and Registration

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

> dotnet add package Toolbelt.Blazor.HotKeys

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

If the Blazor version of the project is ver.3.1 preview 4 or earlyer, you should add the code into ConfigureService method in the Startup class of your Blazor application.

using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this line, and...
...
public class Startup
{
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddHotKeys(); // <- Add this line.
    ...

If the Blazor version of the project is ver.3.2 preview 1 or later, you should add the code into Main method in the Program class of your Blazor application.

using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this line, and...
...
public class Program
{
  public static async Task Main(string[] args)
  {
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    ...
    builder.Services.AddHotKeys(); // <!- Add this line.
    ...

2. Usage in your Blazor component (.razor)

Step.1 Implement IDisposable interface to the component.

@implements IDisposable @* <- Add this at top of the component.  *@
...

@code {
  ...
  public void Dispose() // <- Add "Dispose" method.
  {
  }
}

Step.2 Open the Toolbelt.Blazor.HotKeys namespace, and inject the HotKeys service into the component.

@implements IDisposable
@using Toolbelt.Blazor.HotKeys @* <- Add this, and ... *@
@inject HotKeys HotKeys @* <- And add this. *@
...

Step.3 Invoke CreateContext() method of the HotKeys service instance to create and activate hot keys entries at startup of the component such as OnInitialized() method.

You can add the combination with key and action to the HotKeysContext object that is returned from CreateContext() method, using Add() method.

Please remember that you have to keep the HotKeys Context object in the component field.

@code {

  HotKeysContext HotKeysContext;

  protected override void OnInitialized()
  {
    this.HotKeysContext = this.HotKeys.CreateContext()
      .Add(ModKeys.Ctrl|ModKeys.Shift, Keys.A, FooBar, "do foo bar.")
      .Add(...)
      ...;
  }

  void FooBar() // <- This will be invoked when Ctrl+Shift+A typed.
  {
    ...
  }
}

Note.1: You can also specify the async method to the callback action argument.

Note.2: The method of the callback action can take an argument which is HotKeyEntry object.

Step.4 Destroy the HotKeysContext when the component is disposing, in the Dispose() method of the component.

@code {
  ...
  public void Dispose()
  {
    this.HotKeysContext.Dispose(); // <- Add this.
  }
}

The complete source code (.razor) of this component is bellow.

@page "/"
@implements IDisposable
@using Toolbelt.Blazor.HotKeys
@inject HotKeys HotKeys

@code {

  HotKeysContext HotKeysContext;

  protected override void OnInitialized()
  {
    this.HotKeysContext = this.HotKeys.CreateContext()
      .Add(ModKeys.Ctrl|ModKeys.Shift, Keys.A, FooBar, "do foo bar.");
  }

  void FooBar()
  {
    // Do something here.
  }

  public void Dispose()
  {
    this.HotKeysContext.Dispose();
  }
}

Limitations

No "Cheat Sheet"

Unlike "angular-hotkeys", this library doesn't provide "cheat sheet" feature, at this time.

Instead, the HotKeysContext object provides Keys property, so you can implement your own "Cheat Sheet" UI, like this code:

<ul>
    @foreach (var key in this.HotKeysContext.Keys)
    {
        <li>@key</li>
    }
</ul>

The rendering result:

- Shift+Ctrl+A: do foo bar.
- ...

Release Note

Release notes

License

Mozilla Public License Version 2.0

Release Notes

v.10.0.1
- Fix: A browser's autofill feature causes the unhandled exception.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
10.0.1 2,856 2/14/2021
10.0.0 1,078 1/29/2021
9.0.1 472 12/12/2020
9.0.0 205 12/11/2020
8.3.0 620 8/6/2020
8.2.1 228 7/28/2020
8.2.0 200 7/19/2020
8.1.0 218 5/31/2020
8.0.0 367 1/10/2020
7.0.0 306 9/5/2019
6.0.0.1 176 8/17/2019
5.0.0 185 6/16/2019
4.0.0 251 5/2/2019
3.0.0 269 3/9/2019
2.0.0 337 2/10/2019
1.1.0 412 10/3/2018
1.0.0 408 9/21/2018