Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

695
Vistas
What is Angular2 way of creating global keyboard shortcuts (a.k.a. hotkeys)?

What would be proper way of creating global keyboard shortcuts (a.k.a. hotkeys) in Angular2 application?

Let's say good starting point would be to get working: "?" for cheat-sheet and "Alt+s" for submitting form.

Should I map "?" somehow to main component and then develop attribute directive that would be applied to those components which should respond on particular hotkeys, but then - how do I prevent input fields from responding to "?".

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use this syntax in your template

<div (window:keydown)="doSomething($event)">click me<div>

to call this method in your component

doSomething($event) {
  // read keyCode or other properties 
  // from event and execute a command
} 

To listen on the host component itself

@Component({
  selector: 'app-component',
  host: { '(window:keydown)': 'doSomething($event)' },
})
class AppComponent { 
  doSomething($event) {
    ...
  }
}

or by this equivalent syntax

@Component({
  selector: 'app-component',
})
class AppComponent { 
  @HostListener('window:keydown', ['$event'])
  doSomething($event) {
    ...
  }
}
over 4 years ago · Santiago Trujillo Denunciar

0

You can use a library I created called angular2-hotkeys

It lets you create hotkeys like this:

constructor(private _hotkeysService: HotkeysService) {
  this._hotkeysService.add(new Hotkey('meta+shift+g', (event: KeyboardEvent) => {
    console.log('Typed hotkey');
    return false; // Prevent bubbling
}));  }
over 4 years ago · Santiago Trujillo Denunciar

0

A simple and elegant solution will look like as following:-

Create a global service, which will store the bindings of shortcuts with clickable elements. Note:- Use NgOnDestroy, for removing bindings on component is destroyed.

Now create a directive, which will take keycodes as input like this.

<button [angularHotKey]="[17,78]">New Document</button>
<!-- CTRL = 17 & n = 78 -->

Now in your root most component, listen keypresses and use them as indices for locating Elements in Global Service for HotKeys. Now on getting the reference do something like this.

I assume you have formatted recorded keycodes in this format = 1-23-32-... , dont forget to sort them in ascending/descending format, both while adding it to keypair array in service and while checking

if(this.keypair[combo].length)this.keypair[combo].click();

this.keypair[combo] contain reference of element on which angularHotKey directive was added.

Further Notes: 1.) In angularHotKey directive while adding new keypair and element/clickable reference please check if there don't exist pairing with same combo, if yes throw a exception, it will be helpful while debugging, and prevent you from doing silly mistakes, and in Angular 2 component class,in ngOnDestroy method define a logic of removing all shortcuts paired with it's child elements.

Visit http://keycode.info/ for getting keycodes for all types of keys present on your keyboard.

JavaScript multiple keys pressed at once A answer with every little detail about how to deal with combo key presses

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda