All Projects β†’ ngneat β†’ Hotkeys

ngneat / Hotkeys

Licence: mit
πŸ€– A declarative library for handling hotkeys in Angular applications

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Hotkeys

react-shortcut
Convenient React component that detects if the given key combination is pressed, and triggers a callback
Stars: ✭ 16 (-89.87%)
Mutual labels:  hotkeys, shortcuts
Icanhazshortcut
simple shortcut manager for macOS
Stars: ✭ 204 (+29.11%)
Mutual labels:  shortcuts, hotkeys
Keymage
Yet Another JS Keybinding library
Stars: ✭ 325 (+105.7%)
Mutual labels:  shortcuts, hotkeys
keybind
ClojureScript key bindings (shortcut) library
Stars: ✭ 85 (-46.2%)
Mutual labels:  hotkeys, shortcuts
hotkey
⌨️ cross-platform hotkey package
Stars: ✭ 82 (-48.1%)
Mutual labels:  hotkeys, shortcuts
react-keyboard-shortcuts
A declarative library for handling hotkeys based on explicit priority in React applications
Stars: ✭ 23 (-85.44%)
Mutual labels:  hotkeys, shortcuts
Hotkeys
➷ A robust Javascript library for capturing keyboard input. It has no dependencies.
Stars: ✭ 5,165 (+3168.99%)
Mutual labels:  shortcuts, hotkeys
Spotify Shortcuts Ios12
This repository contains a list of all available Shortcuts to control Spotify as well as an update mechanism.
Stars: ✭ 34 (-78.48%)
Mutual labels:  shortcuts
Cropper
Point and shoot screen captures
Stars: ✭ 100 (-36.71%)
Mutual labels:  hotkeys
Reakeys
⌨️ React Hotkeys Hook
Stars: ✭ 28 (-82.28%)
Mutual labels:  hotkeys
Gata
Bookmarks made better
Stars: ✭ 17 (-89.24%)
Mutual labels:  shortcuts
Shortcuts
Super performant and feature rich shortcuts management library.
Stars: ✭ 40 (-74.68%)
Mutual labels:  shortcuts
Autohotkey
βš™οΈ My Autohotkey productivity suite that includes shortcuts, hotstrings, hotkeys, apps/utilities, AutoCorrect
Stars: ✭ 113 (-28.48%)
Mutual labels:  hotkeys
Better Chrome Native Video
Add keyboard support to Chrome's native HTML5 video player.
Stars: ✭ 31 (-80.38%)
Mutual labels:  shortcuts
Sketch Plugin Monster
A Sketch plugin for managing all plugin shortcuts.
Stars: ✭ 143 (-9.49%)
Mutual labels:  shortcuts
Hd To Tc
This tool will convert Age of Empires II HD to The Conquerors and create a seperate installation folder. You can also auto configure Voobly to start playing multiplayer instantly.
Stars: ✭ 13 (-91.77%)
Mutual labels:  hotkeys
Win Vind
Simple Vim Key Binder for Windows. You can operate Windows with keybindings like Vim.
Stars: ✭ 151 (-4.43%)
Mutual labels:  hotkeys
Jumpy
The fastest way to jump around files and across visible panes in Atom
Stars: ✭ 116 (-26.58%)
Mutual labels:  hotkeys
Aoe2tools
A lightweight program that lets you easily play your Steam version of Age of Empires 2 HD in Voobly format. In addition to Mega Utilities
Stars: ✭ 82 (-48.1%)
Mutual labels:  hotkeys
Xcactionbar
"Alfred for Xcode" plugin
Stars: ✭ 1,217 (+670.25%)
Mutual labels:  shortcuts


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

  • βœ… Support Element Scope
  • βœ… Support Global Listeners
  • βœ… Platform Agnostic
  • βœ… Hotkeys Cheatsheet

Table of Contents

Installation

npm install @ngneat/hotkeys

Usage

Add HotkeysModule in your AppModule:

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

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

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:

  • hotkeysGroup - define the group name.
  • hotkeysDescription - add a description.
  • hotkeysOptions - See Options

For example:

<input hotkeys="meta.n" 
      hotkeysGroup="File" 
      hotkeysDescription="New Document" 
      (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));
  }
}

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' or 'SELECT'.
  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 { MatDialog } from '@angular/material/dialog';
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: MatDialog) {}

  ngAfterViewInit() {
    this.hotkeys.registerHelpModal(() => {
      const ref = this.dialog.open(HotkeysHelpComponent, { width: '500px' });
      ref.componentInstance.dimiss.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:

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']);

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. 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'] })
      .subscribe(e => console.log('Hotkey', e));
  }
}

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

<input hotkeys="meta.n" 
      hotkeysGroup="File" 
      hotkeysDescription="New Document" 
      hotkeysOptions="{allowIn: ['INPUT','SELECT', 'TEXTAREA']}" 
      (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):


Carlos Vilacha

πŸ’» πŸ–‹ πŸ“–

Netanel Basal

πŸ“ πŸ’» πŸ“– πŸ€”

Álvaro Martínez

πŸ’» πŸ“–

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].