Angular 2+ elegant way to intercept Command+S

前端 未结 3 1490
刺人心
刺人心 2021-02-20 00:10

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 me

相关标签:
3条回答
  • 2021-02-20 00:24

    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

    0 讨论(0)
  • 2021-02-20 00:41
    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;
          }
        }
      });
    }
    
    0 讨论(0)
  • 2021-02-20 00:42

    Global listener, non-deprecated answer:

    @HostListener('window:keydown', ['$event'])
    onKeyDown(event: KeyboardEvent) {
        if ((event.metaKey || event.ctrlKey) && event.key === 's') {
            this.save();
            event.preventDefault();
        }
    }
    
    0 讨论(0)
提交回复
热议问题