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...
To stop the save dialog of the browser from opening, we must use the
keydown
event instead ofkeyup
and call the function$event.preventDefault();
. Updated code below:Then bind this method to the
(keydown)
event in your div:Updated PLUNKER DEMO
Here is an idea, how about detecting the event in you class:
Then bind this method to the
(keyup)
event in your div:Here is a plunker link: PLUNKER DEMO