Home

Awesome

<p align="center"> <img width="20%" height="20%" src="./logo.svg"> </p> <br />

Test MIT commitizen PRs styled with prettier All Contributors ngneat spectator

Shortcut like a pro!

A declarative library for handling hotkeys in Angular applications.

Web apps are getting closer and closer to be desktop-class applications. With this in mind, it makes sense to add hotkeys for those power users that are looking to navigate their favorite websites using hotkeys just as they do on their regular native apps. To help you have a better experience we developed Hotkeys.

Features

Table of Contents

Compatibility with Angular Versions

@ngneat/hotkeysAngular
3.x.x>=17.2
2.x.x>=16
1.3.x>=14
1.2.x<=13

Installation

npm install @ngneat/hotkeys

Usage

Module

Add HotkeysModule in your AppModule:

import { HotkeysModule } from '@ngneat/hotkeys';

@NgModule({
  imports: [HotkeysModule]
})
export class AppModule {}

Standalone

Add HotkeysService in the standalone components :

@Component({
  standalone: true,
  imports: [HotkeysDirective],
})
export class AppComponent {}

Now you have two ways to start adding shortcuts to your application:

Hotkeys Directive

<input hotkeys="meta.a" (hotkey)="handleHotkey($event)" />

Hotkeys take care of transforming keys from macOS to Linux and Windows and vice-versa.

Additionally, the directive accepts three more inputs:

For example:

<!-- prettier-ignore -->
<input hotkeys="meta.n" 
      hotkeysGroup="File" 
      hotkeysDescription="New Document" 
      (hotkey)="handleHotkey($event)"

Example sequence hotkey:

<input hotkeys="g>i" hotkeysGroup="Navigate" hotkeysDescription="Go to Inbox" (hotkey)="handleHotkey($event)"

Hotkeys Service

This is a global service that can be injected anywhere:

import { HotkeysService } from '@ngneat/hotkeys';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private hotkeys: HotkeysService) {}

  ngOnInit() {
    this.hotkeys.addShortcut({ keys: 'meta.a' }).subscribe(e => console.log('Hotkey', e));
    this.hotkeys.addSequenceShortcut({ keys: 'g>i' }).subscribe(e => console.log('Hotkey', e));
  }
}

Options

There are additional properties we can provide:

interface Options {
  // The group name
  group: string;
  // hotkey target element (defaults to `document`)
  element: HTMLElement;
  // The type of event (defaults to `keydown`)
  trigger: 'keydown' | 'keyup';
  // Allow input, textarea and select as key event sources (defaults to []).
  // It can be 'INPUT', 'TEXTAREA', 'SELECT' or 'CONTENTEDITABLE'.
  allowIn: AllowInElement[];
  // hotkey description
  description: string;
  // Included in the shortcut list to be display in the help dialog (defaults to `true`)
  showInHelpMenu: boolean;
  // Whether to prevent the default behavior of the event. (defaults to `true`)
  preventDefault: boolean;
}

onShortcut

Listen to any registered hotkey. For example:

const unsubscribe = this.hotkeys.onShortcut((event, key, target) => console.log('callback', key));

// When you're done listening, unsubscribe
unsubscribe();

registerHelpModal

Display a help dialog listing all visible hotkeys:

import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { HotkeysHelpComponent, HotkeysService } from '@ngneat/hotkeys';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  constructor(private hotkeys: HotkeysService, private dialog: NgbModal) {}

  ngAfterViewInit() {
    this.hotkeys.registerHelpModal(() => {
      const ref = this.modalService.open(HotkeysHelpComponent, { size: 'lg' });
      ref.componentInstance.title = 'Custom Shortcuts Title';
      ref.componentInstance.dismiss.subscribe(() => ref.close());
    });
  }
}

It accepts a second input that allows defining the hotkey that should open the dialog. The default shortcut is Shift + ?. Here's how HotkeysHelpComponent looks like:

<p align="center">
 <img width="50%" height="50%" src="./help_screenshot.png">
</p>

You can also provide a custom component. To help you with that, the service exposes the getShortcuts method.

removeShortcuts

Remove previously registered shortcuts.

// Remove a single shortcut
this.hotkeys.removeShortcuts('meta.a');
// Remove several shortcuts
this.hotkeys.removeShortcuts(['meta.1', 'meta.2']);

setSequenceDebounce

Set the number of milliseconds to debounce a sequence of keys

this.hotkeys.setSequenceDebounce(500);

Hotkeys Shortcut Pipe

The hotkeysShortcut formats the shortcuts when presenting them in a custom help screen:

<div class="help-dialog-shortcut-key">
  <kbd [innerHTML]="hotkey.keys | hotkeysShortcut"></kbd>
</div>

The pipe accepts and additional parameter the way key combinations are separated. By default, the separator is +. In the following example, a - is used as separator.

<div class="help-dialog-shortcut-key">
  <kbd [innerHTML]="hotkey.keys | hotkeysShortcut: '-'"></kbd>
</div>

Allowing hotkeys in form elements

By default, the library prevents hotkey callbacks from firing when their event originates from an input, select, or textarea element or any elements that are contenteditable. To enable hotkeys in these elements, specify them in the allowIn parameter:

import { HotkeysService } from '@ngneat/hotkeys';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private hotkeys: HotkeysService) {}

  ngOnInit() {
    this.hotkeys
      .addShortcut({ keys: 'meta.a', allowIn: ['INPUT', 'SELECT', 'TEXTAREA', 'CONTENTEDITABLE'] })
      .subscribe(e => console.log('Hotkey', e));
  }
}

It's possible to enable them in the template as well:

<!-- prettier-ignore -->
<input hotkeys="meta.n" 
      hotkeysGroup="File" 
      hotkeysDescription="New Document" 
      hotkeysOptions="{allowIn: ['INPUT','SELECT', 'TEXTAREA', 'CONTENTEDITABLE']}" 
      (hotkey)="handleHotkey($event)"

That's all for now! Make sure to check out the playground inside the src folder.

FAQ

Can I define duplicated hotkeys?

No. It's not possible to define a hotkey multiple times. Each hotkey has a description and a group, so it doesn't make sense assigning a hotkey to different actions.

Why am I not receiving any event?

If you've added a hotkey to a particular element of your DOM, make sure it's focusable. Otherwise, hotkeys cannot capture any keyboard event.

How to ...

Listening to the same shortcut in different places.

You can always use onShortcut. This method allows listening to all registered hotkeys without affecting the original definition.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> <!-- prettier-ignore-start --> <!-- markdownlint-disable --> <table> <tr> <td align="center"><a href="http://carlosvilacha.com"><img src="https://avatars3.githubusercontent.com/u/1565222?v=4" width="100px;" alt=""/><br /><sub><b>Carlos Vilacha</b></sub></a><br /><a href="https://github.com/@ngneat/hotkeys/commits?author=flatstadt" title="Code">💻</a> <a href="#content-flatstadt" title="Content">🖋</a> <a href="https://github.com/@ngneat/hotkeys/commits?author=flatstadt" title="Documentation">📖</a></td> <td align="center"><a href="https://www.netbasal.com"><img src="https://avatars1.githubusercontent.com/u/6745730?v=4" width="100px;" alt=""/><br /><sub><b>Netanel Basal</b></sub></a><br /><a href="#blog-NetanelBasal" title="Blogposts">📝</a> <a href="https://github.com/@ngneat/hotkeys/commits?author=NetanelBasal" title="Code">💻</a> <a href="https://github.com/@ngneat/hotkeys/commits?author=NetanelBasal" title="Documentation">📖</a> <a href="#ideas-NetanelBasal" title="Ideas, Planning, & Feedback">🤔</a></td> <td align="center"><a href="https://github.com/alvaromartmart"><img src="https://avatars1.githubusercontent.com/u/18287793?s=120&v=4" width="100px;" alt=""/><br /><sub><b>Álvaro Martínez</b></sub></a><br /><a href="https://github.com/@ngneat/hotkeys/commits?author=alvaromartmart" title="Code">💻</a> <a href="https://github.com/@ngneat/hotkeys/commits?author=alvaromartmart" title="Documentation">📖</a></td> </tr> </table> <!-- markdownlint-enable --> <!-- prettier-ignore-end --> <!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the all-contributors specification. Contributions of any kind welcome!