jQuery 如何检测是否按下了 Ctrl+R?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2767126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect that Ctrl+R was pressed?
提问by sadmicrowave
I'm coding a function in jquery that executes if Ctrl+Ris pressed but I can't seem to find out what the left and right ctrl keycodes are... Can someone please help?
我正在 jquery 中编写一个函数,如果按下Ctrl+R则执行该函数,但我似乎无法找出左右 ctrl 键码是什么......有人可以帮忙吗?
UPDATE
更新
///this works
$(document).keydown(function(e){
if(e.keyCode==17){alert("control was pressed")};
});
Next Question-- How do I link control key press and another key press to execute a function?
下一个问题 - 如何链接控制按键和另一个按键以执行功能?
if(e.keyCode==17){llCtrlPress=1};
if(e.keyCode==97 && llCtrlPress=1){DO SOMETHING}
????????????
That seems like it would work fine but then how do I set llCtrlpress back to '0' on keyup?
这似乎可以正常工作,但是如何在键盘上将 llCtrlpress 设置回“0”?
回答by Alan Hymanson
You have to use the keydownfunction to trap Ctrlcharacters. Here is my implementation of Ctrl+A:
您必须使用keydown函数来捕获Ctrl字符。这是我对Ctrl+ 的实现A:
$(document).keydown(function(e) {
if (e.keyCode == 65 && e.ctrlKey) {
alert('ctrl A');
}
});
Ctrl-R is tougher because in most browsers, that is Reload Page, which means the javascript doesn't run, the page is refreshed.
Ctrl-R 更难,因为在大多数浏览器中,即Reload Page,这意味着 javascript 不运行,页面被刷新。
Just a note as well, the keyCodevalue are different in the keydown/keyupup functions than in the keypress functions.
还要注意的是,keydown/keyupup 函数中的keyCode值与 keypress 函数中的值不同。
EDIT:Removed ctrl variable, forgot about ctrlKey
编辑:删除了 ctrl 变量,忘记了 ctrlKey
回答by Saurav S.
This is the code I'm using to disable refresh on IE and firefox (This works well for F5, Ctrl+F5and Ctrl+R)
这是我用来在 IE 和 firefox 上禁用刷新的代码(这适用于F5, Ctrl+F5和Ctrl+ R)
<script language="javascript" type="text/javascript">
//this code handles the F5/Ctrl+F5/Ctrl+R
document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (e)
keycode = e.which;
// Mozilla firefox
if ($.browser.mozilla) {
if (keycode == 116 ||(e.ctrlKey && keycode == 82)) {
if (e.preventDefault)
{
e.preventDefault();
e.stopPropagation();
}
}
}
// IE
else if ($.browser.msie) {
if (keycode == 116 || (window.event.ctrlKey && keycode == 82)) {
window.event.returnValue = false;
window.event.keyCode = 0;
window.status = "Refresh is disabled";
}
}
}
</script>
If you don't want to use useragent to detect what type of browser it is ($.browser uses navigator.userAgent to determine the platform), you can use
如果不想使用useragent来检测浏览器是什么类型的($.browser使用navigator.userAgent来判断平台),可以使用
if('MozBoxSizing' in document.documentElement.style)
- returns true for firefox
if('MozBoxSizing' in document.documentElement.style)
- 为 Firefox 返回 true
回答by lincolnk
why aren't you using e.ctrlKey
?
你为什么不使用e.ctrlKey
?
if (e.keyCode == 65 && e.ctrlKey) {
alert('ctrl A');
}
edit: here's an appropriate function to detect your ctrl-r keypress and stop the browser from reloading.
编辑:这是一个适当的功能来检测您的 ctrl-r 按键并阻止浏览器重新加载。
function keydown(e) {
if (e.ctrlKey && e.keyCode == 82) {
// 82 = r
// TODO: your thing.
if (e.preventDefault) {
e.preventDefault();
}
else {
return false;
}
}
}
i'm a jquery newbie, i think you'd do
我是 jquery 新手,我想你会这样做
$(document).keydown(keydown);
right?
对?
回答by Jithu
回答by Gibolt
Use event.key
and modern JS!
使用event.key
和现代JS!
$(document).keypress(function(event) {
if (event.key === "r" && event.ctrlKey) {
// Do something
}
});
or without jQuery:
或不使用 jQuery:
document.addEventListener("keypress", function onEvent(event) {
if (event.key === "r" && event.ctrlKey) {
// Do something better
}
});
回答by Josh Stodola
回答by MSM
@HostListener('window:keydown', ['$event'])
keyEvent(event: KeyboardEvent) {
if (event.ctrlKey && event.keyCode == 82)
{
}
}
回答by Aravinthan
We can also use Ascii code for finding the characters as well as the characters
我们还可以使用 Ascii 代码来查找字符以及字符
$('html').keydown(function (e) {
keydownfunc(e);
});
function keydownfunc(e) {
if (e.ctrlKey && (e.key === "r" || e.key === "R")) {
alert("ctrl+R");
}
}
回答by Rahul Kumar
The Key Code for Ctrlkey is 11
.
密钥的密钥代码Ctrl是11
。
$(document).keypress(function(e) {
alert("Ctrl is pressed: " + e.ctrlKey);
});