How to handle ESC keydown on javascript popup window

前端 未结 8 664
孤独总比滥情好
孤独总比滥情好 2020-12-07 19:03

I have a javascript window.open popup, and I want the popup to close itself when the user presses the ESC key. I can\'t figure out how to hook the keydown event (and on wha

相关标签:
8条回答
  • 2020-12-07 19:08

    @Gumbo 's answer is good but often you need to unhook this behaviour so I suggest to use the one event handler:

    $(document).one('keydown', function(e) {
        // ESCAPE key pressed
        if (e.keyCode == 27) {
            window.close();
        }
    });
    

    OR

    $(document).on('keydown', function(e) {
        // ESCAPE key pressed
        if (e.keyCode == 27) {
            window.close();
        }
    });
    

    and when ready to stop the behaviour

    $(document).off('keydown');
    
    0 讨论(0)
  • 2020-12-07 19:09

    It is possible to achieve with JS Without using jQuery.

    window.onkeydown = function( event ) {
        if ( event.keyCode == 27 ) {
            console.log( 'escape pressed' );
        }
    };
    
    0 讨论(0)
  • 2020-12-07 19:12

    Try something like this:

    $(document).keydown(function(e) {
        // ESCAPE key pressed
        if (e.keyCode == 27) {
            window.close();
        }
    });
    
    0 讨论(0)
  • 2020-12-07 19:12

    You can easily achieve bind key events using Jquery.

    Here you can use .keydown()

    List of keyboard keys codes

      $(document).keydown(function(e) {        
        if (e.keyCode == 27) {
            window.close();
        }
    });
    
    0 讨论(0)
  • 2020-12-07 19:26

    event.key === "Escape"

    No more arbitrary number codes!

    document.addEventListener('keydown', function(event) {
        const key = event.key; // const {key} = event; in ES6+
        if (key === "Escape") {
            window.close();
        }
    });
    

    Mozilla Docs

    Supported Browsers

    0 讨论(0)
  • 2020-12-07 19:27

    In case if any looking for angularjs popup solution here you go

    *this is without using ui-bootstrap dependency(only recommended when there is no other way)

    $scope.openModal = function(index){
        $scope.showpopup = true;
        event.stopPropagation();//cool part
    };
    
    $scope.closeModal = function(){
        $scope.cc.modal.showpopup = false;
    };
    
    window.onclick = function() {
      if ($scope.showpopup) {
          $scope.showpopup = false;
          // You should let angular know about the update that you have made, so that it can refresh the UI
          $scope.$apply();
      }
    };
    
    //escape key functionality playing with scope variable
    document.onkeydown = function (event) {
      const key = event.key; // const {key} = event; in ES6+
      if (key === "Escape") {
        if ($scope.showpopup) {
            $scope.showpopup = false;
            // You should let angular know about the update that you have made, so that it can refresh the UI
            $scope.$apply();
        }
      }
    };
    

    References: above answers and http://blog.nkn.io/post/hiding-menu-when-clicking-outside---angularjs/

    0 讨论(0)
提交回复
热议问题