javascript - How to fire an event in jquery when alt+tab or windows+d is pressed? - Stack Overflow

I want to fire an event when when alt+tab or windows+d is pressed. Following is my code to give alert w

I want to fire an event when when alt+tab or windows+d is pressed. Following is my code to give alert when mouse pointer is away from the browser window. But even if user presses alt+tab or Windows+D then also this event should get occur. Can anyone help me oin this regard, please? Folowing is my code for your reference:

<!DOCTYPE html>
<html>
  <head>
    <script src=".10.2/jquery.min.js"></script>
    <script src=".10.3/jquery-ui.js"></script>
    <link rel="stylesheet" href=".10.3/themes/smoothness/jquery-ui.css">
    <script>
      var timer;
      $(document).ready(function () {
        $(document).mouseleave(function () {
          //alert("Mouse is away");
          customAlert("your mouse is away");
        });
      });

      function customAlert(customText) {
        $("#popUp").html(customText);
        timer = setInterval(customAlert2, 5000);

        $("#popUp").dialog({
          dialogClass: "no-close",
          buttons: [{
                      text: "OK", click: function () {
                        $(this).dialog("close");
                        clearInterval(timer);
                      }
                    }]
        });
      }

      function customAlert2() {
        location.reload();
        $("#popUp2").dialog({
        dialogClass: "no-close",
        buttons: [{
                  text: "OK", click: function () {
                    $(this).dialog("close");
                  }
                }]
        });
      }


    </script>
  </head>
  <body>
    <h1>My first Javascript program</h1>
    <p>Hello World!</p>
    <div id="popUp" title="First alert message"></div>
    <div id="popUp2" title="Second alert message">Time is over</div>
  </body>
</html>

I want to fire an event when when alt+tab or windows+d is pressed. Following is my code to give alert when mouse pointer is away from the browser window. But even if user presses alt+tab or Windows+D then also this event should get occur. Can anyone help me oin this regard, please? Folowing is my code for your reference:

<!DOCTYPE html>
<html>
  <head>
    <script src="http://ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="http://code.jquery./ui/1.10.3/jquery-ui.js"></script>
    <link rel="stylesheet" href="http://code.jquery./ui/1.10.3/themes/smoothness/jquery-ui.css">
    <script>
      var timer;
      $(document).ready(function () {
        $(document).mouseleave(function () {
          //alert("Mouse is away");
          customAlert("your mouse is away");
        });
      });

      function customAlert(customText) {
        $("#popUp").html(customText);
        timer = setInterval(customAlert2, 5000);

        $("#popUp").dialog({
          dialogClass: "no-close",
          buttons: [{
                      text: "OK", click: function () {
                        $(this).dialog("close");
                        clearInterval(timer);
                      }
                    }]
        });
      }

      function customAlert2() {
        location.reload();
        $("#popUp2").dialog({
        dialogClass: "no-close",
        buttons: [{
                  text: "OK", click: function () {
                    $(this).dialog("close");
                  }
                }]
        });
      }


    </script>
  </head>
  <body>
    <h1>My first Javascript program</h1>
    <p>Hello World!</p>
    <div id="popUp" title="First alert message"></div>
    <div id="popUp2" title="Second alert message">Time is over</div>
  </body>
</html>
Share Improve this question asked Dec 3, 2013 at 5:07 PHPLoverPHPLover 13k53 gold badges172 silver badges324 bronze badges 3
  • JavaScript can only 'see' what is happening within the browser. – user2417483 Commented Dec 3, 2013 at 5:09
  • @PHPLover, did you find your answer? – Majid Golshadi Commented Dec 31, 2013 at 15:18
  • I think you can be interested in HTML5 Visibility API davidwalsh.name/page-visibility – gregmatys Commented Oct 1, 2017 at 18:20
Add a ment  | 

2 Answers 2

Reset to default 2

attention if you want to handle any keys that they registered by OS (for example: Alt+Tab ) you CAN NOT do this by Jquery.

you need to assign your event to unregistered keys to fire your Event with Jquery.

you can try some code like blow to handle what you want handly

var keys = {};

 $(document).keydown(function (e) {
  keys[e.which] = true;
 });

 $(document).keyup(function (e) {
   delete keys[e.which];
});

if( (keys[91] && keys[68]) || (keys[18] && keys[9]) ) /*windows+d OR alt+tab*/
{ /* your code */}


or

use jwerty lib to do it. example code:

jwerty.key('ctrl+shift+P', function () { 
   // your code
});

and support mbinations:

jwerty.key('⌃+⇧+P/⌘+⇧+P', function () { 
   // your code
});


and There's a simple javaScript library Mousetrap for handling keyboard shortcuts. Take a look at the example:

Mousetrap.bind('h', function() {
    // your code
});

OR

It also supports binations:

Mousetrap.bind(['ctrl+h', 'ctrl+l'], function(e) {
    // your code
}

i hope it's useful for you.

jwerty is a great plugin that allows you create functions for specific key binations.

e.g.:

jwerty.key('ctrl+shift+P', function () { [...] });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745460464a4628685.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信