Using Hot Keys in Blazor WASM
Blazor
31 Articles
In this article, let's learn about how to setup and use Hot Keys in Blazor WASM apps.
Note: If you have not done so already, I recommend you read the article on Prevent image leech by dynamically streaming image in Blazor WASM.
Table of Contents
- Introduction
- Installation
- Configuration
- Use Cases for Hot Keys
- Demo
- Advantages of Hot Keys
- Limitations
- Summary
Introduction
In the context of front-end development, a hotkey, also known as a keyboard shortcut or shortcut key, is a combination of keys on a computer keyboard that triggers specific actions within a software application. Hotkeys are designed to enhance user productivity and provide a quicker way to perform tasks without using a mouse or navigating through menus.
Installation
To start with Hot Keys, you need to,
- Install the Toolbelt.Blazor.HotKeys2 package from NuGet.
- Register HotKeys in DI Container using builder.Services.AddHotKeys2().
Configuration
To configure hot keys inside a component, you need to,
- Implement IDisposable interface to the component.
- Inject the HotKeys service into the component.
- Create hot key context using HotKeys.CreateContext() in OnInitialized() or OnAfterRender() method.
- Add the combination with key and action to the HotKeysContext object that is returned from CreateContext() method, using Add() method.
- You can also specify the async method to the callback action argument. The method of the callback action can take an argument which is HotKeyEntryByCode or HotKeyEntryByKey object.
- Dispose the HotKeysContext object when the component is disposing, in the Dispose() method of the component.
Code Sample - Configuring Hot Keys in Blazor WASM
Use Cases for Hot Keys
The most common use cases for hot keys are,
- Saving a record while updating
- Activating search within a web site
Demo
Advantages of Hot Keys
The advantages of hot keys are as follows,
- Speed & Efficiency.
- Improved Accessibility.
- Reduced mouse dependency.
- Customization & Personilization.
Limitations
The only limitation of hot keys is that it can be used only in desktop devices where we have a key board device available to activate hot key.
Summary
In this article, we learned about what is hot keys and how to configure them in Blazor WASM apps using BlazorHotKeys package. We also learned about the advantages of using hot keys in our apps. Hotkeys empower end users by providing faster, more accessible, and customizable ways to interact with software applications, enhancing their overall efficiency and user experience. You can learn more about the Toolbelt.Blazor.HotKeys2 package in official docs.