How to build a directive that listens for specific keyboard shortcuts

0 votes
can someone help me with this by including relevant code or example, How to build a directive that listens for specific keyboard shortcuts?
2 days ago in Angular by Nidhi
• 14,600 points
39 views

1 answer to this question.

0 votes

You can use the @HostListener decorator to handle keydown events at the window level and match key combinations.

Keyboard Shortcut Directive

1. Create the Directive

import { Directive, HostListener, Input, Output, EventEmitter } from '@angular/core';

@Directive({

  selector: '[appShortcut]'

})

export class ShortcutDirective {

  @Input('appShortcut') keys: string = ''; // e.g., 'ctrl+shift+s'

  @Output() shortcutTriggered = new EventEmitter<void>();

  @HostListener('window:keydown', ['$event'])

  handleKeydown(event: KeyboardEvent) {

    const keyCombo = this.keys.toLowerCase().split('+');

    const ctrl = keyCombo.includes('ctrl') ? event.ctrlKey : true;

    const shift = keyCombo.includes('shift') ? event.shiftKey : true;

    const alt = keyCombo.includes('alt') ? event.altKey : true;

    const key = keyCombo.find(k => !['ctrl', 'shift', 'alt'].includes(k)) === event.key.toLowerCase();

    if (ctrl && shift && alt && key) {

      event.preventDefault();

      this.shortcutTriggered.emit();

    }

  }

}

2. Use in Template

<!-- Example: Ctrl + Shift + S triggers this shortcut -->

<div [appShortcut]="'ctrl+shift+s'" (shortcutTriggered)="saveDocument()">

  Press Ctrl + Shift + S to save.

</div>

answered 2 days ago by anonymous

Related Questions In Angular

0 votes
1 answer

How to implement a directive that adds tooltips to form controls dynamically?

Custom Tooltip Directive 1. Create the Directive import { ...READ MORE

answered 2 days ago in Angular by anonymous
18 views
0 votes
1 answer

How to develop a directive that modifies element styles based on form validation status?

Here's a concise directive that automatically applies ...READ MORE

answered 2 days ago in Angular by anonymous
18 views
0 votes
1 answer

How to create a custom directive that integrates with external charting libraries?

Compact Directive for External Chart Integration import { ...READ MORE

answered 2 days ago in Angular by anonymous
21 views
0 votes
1 answer

How to set a default base URL for all API calls in Angular?

In Angular, you can set a default ...READ MORE

answered Feb 26 in Angular by Kavya
97 views
0 votes
0 answers

How do Observables improve API call handling in Angular?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
106 views
0 votes
0 answers

What’s the difference between Observables and Promises?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
76 views
0 votes
0 answers

What type of operation do RxJS operators allow for observables?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
85 views
0 votes
0 answers

When to use switchMap vs concatMap?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
111 views
0 votes
1 answer

How to implement a directive that manages focus on dynamically added elements?

To implement a directive that manages focus ...READ MORE

answered 2 days ago in Angular by anonymous
30 views
0 votes
1 answer

How to send multipart/form-data for a file upload in Angular?

In Angular, you can send files using ...READ MORE

answered Feb 24 in Angular by Navya
146 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP