14

Trying to implement a shortcut key combination for Command+S to save a form.

I've read this - https://angular.io/guide/user-input, but it does not say anything about meta or command.

Tried surrounding the form with:

<div
  (keyup.command.s)="save()"
  (keyup.command.u)="save()"
  (keyup.control.u)="save()"
  (keyup.control.s)="save()"
  (keyup.meta.u)="save()"
>

Of those, only control.u and control.s worked.

With all the power and cross-browser capabilities of Angular 2+, I was hoping that this is somehow handled in an elegant way, using (keyup...).

And for sure many Angular Devs use Macs :).

I've also read How does one capture a Mac's command key via JavaScript? and http://unixpapa.com/js/key.html but still hoping for Angular elegant solution instead of fighting with browser-specific stuff...

KarolDepka
  • 8,318
  • 10
  • 45
  • 58

3 Answers3

17

Global listener, non-deprecated answer:

@HostListener('window:keydown', ['$event'])
onKeyDown(event: KeyboardEvent) {
    if ((event.metaKey || event.ctrlKey) && event.key === 's') {
        this.save();
        event.preventDefault();
    }
}
Chris Fremgen
  • 4,649
  • 1
  • 26
  • 26
8

UPDATE

To stop the save dialog of the browser from opening, we must use the keydown event instead of keyup and call the function $event.preventDefault();. Updated code below:

  onKeyDown($event): void {
    // Detect platform
    if(navigator.platform.match('Mac')){
        this.handleMacKeyEvents($event);
    }
    else {
        this.handleWindowsKeyEvents($event); 
    }
  }

  handleMacKeyEvents($event) {
    // MetaKey documentation
    // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/metaKey
    let charCode = String.fromCharCode($event.which).toLowerCase();
    if ($event.metaKey && charCode === 's') {
        // Action on Cmd + S
        $event.preventDefault();
    } 
  }

  handleWindowsKeyEvents($event) {
    let charCode = String.fromCharCode($event.which).toLowerCase();
    if ($event.ctrlKey && charCode === 's') {
        // Action on Ctrl + S
        $event.preventDefault();
    } 
  }

Then bind this method to the (keydown) event in your div:

<div (keydown)="onKeyDown($event)" tabindex="0">
</div>

Updated PLUNKER DEMO


ORIGINAL ANSWER

Here is an idea, how about detecting the event in you class:

  onKeyUp($event): void {
    // Detect platform
    if(navigator.platform.match('Mac')){
        this.handleMacKeyEvents($event);
    }
    else {
        this.handleWindowsKeyEvents($event); 
    }
  }

  handleMacKeyEvents($event) {
    // MetaKey documentation
    // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/metaKey
    let charCode = String.fromCharCode($event.which).toLowerCase();
    if ($event.metaKey && charCode === 's') {
        // Action on Cmd + S
        $event.preventDefault();
    } 
  }

  handleWindowsKeyEvents($event) {
    let charCode = String.fromCharCode($event.which).toLowerCase();
    if ($event.ctrlKey && charCode === 's') {
        // Action on Ctrl + S
        $event.preventDefault();
    } 
  }

Then bind this method to the (keyup) event in your div:

<div (keyup)="onKeyUp($event)" tabindex="0">
</div>

Here is a plunker link: PLUNKER DEMO

FAISAL
  • 33,618
  • 10
  • 97
  • 105
  • 1
    There is an Boolean for "meta" keys, which is the Command key on mac, it's `event.metaKey`. – Adam LeBlanc Aug 08 '17 at 13:05
  • 1
    @KarolDepka in mac you have `metaKey`. See the updated answer. – FAISAL Aug 08 '17 at 13:06
  • Tried `$event.metaKey`. Behaves weirdly. I have not yet managed to capture any useful key combination with `metaKey`... As if it is not a modifier key (which I've read somewhere as well...) – KarolDepka Aug 08 '17 at 13:08
  • @AdamLeBlanc: have you managed to capture actual meta+letter key combinations using `metaKey`? – KarolDepka Aug 08 '17 at 13:09
  • 1
    here is a useful article on key combinations: https://www.bennadel.com/blog/3088-native-key-combination-event-binding-support-in-angular-2-beta-17.htm – FAISAL Aug 08 '17 at 13:10
  • @Faisal: for me, your updated example (with `$event.metaKey`) does not work in neither Chrome nor Firefox nor Safari. For Cmd+S it brings the "Save webpage" dialog. Did you try to run it? – KarolDepka Aug 08 '17 at 13:13
  • 1
    div by default is not focusable. I test with a input element. I have created a plunk for this, here is the link: https://plnkr.co/edit/xtaTjKzPwIDXpc77ZcUj?p=preview [I am trying to figure out how to stop the save dialog from opening] – FAISAL Aug 08 '17 at 13:24
  • @Faisal: so far the only thing that was able to capture command+s (key DOWN) - http://bennadel.github.io/JavaScript-Demos/demos/key-events-plugin-angular2/ - source https://github.com/bennadel/JavaScript-Demos/blob/master/demos/key-events-plugin-angular2/index.htm – KarolDepka Aug 08 '17 at 13:29
  • @Faisal: for preventing save dialog, maybe: `$event.preventDefault();` – KarolDepka Aug 08 '17 at 13:29
  • @Faisal - interestingly, for me your Plunkr does not seem to detect Cmd+S... Are you on a Mac? – KarolDepka Aug 08 '17 at 13:31
  • 1
    I am on windows – FAISAL Aug 08 '17 at 13:36
  • 1
    @KarolDepka I have updated the plunker to detect the platform – FAISAL Aug 08 '17 at 13:44
  • 1
    @Faisal I accepted this answer, though the remaining question is how to get rid of the browser's page-save dialog hijacking the shortcut key, which we hope to solve soon ;-). – KarolDepka Aug 09 '17 at 22:54
  • For global shortcuts, one can use the `window:` prefix, for example: `
    `
    – KarolDepka Aug 11 '17 at 10:57
1
  1. The methods preventDefault() and stopPropagation() and so on did not work for me (using current Chrome), so i had to go multiple keys: ctrl+shift+s
  2. When defining the event handler in my component, it only worked for this subsite and only if some form inputs were in focus, so i had to add the listener at another place.
  3. I compressed the multiple functions, since i do not care about which environment the hotkey gets called, i want to save my object.
ngOnInit() {   
  document.body.addEventListener("keydown", event => {
    let charCode = String.fromCharCode(event.which).toLowerCase();
    switch (((navigator.platform.match('Mac') && event.metaKey) || event.ctrlKey) && event.shiftKey && charCode === 's') {
      case true: {
        this.saveArticle(); //whatever your usecase is
        break;
      }
    }
  });
}
connectedMind
  • 421
  • 4
  • 17