admin管理员组文章数量:1426020
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
2 Answers
Reset to default 2attention 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 () { [...] });
本文标签: javascriptHow to fire an event in jquery when alttab or windowsd is pressedStack Overflow
版权声明:本文标题:javascript - How to fire an event in jquery when alt+tab or windows+d is pressed? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745460464a2659301.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论