如何检测到Ctrl + R被按下? [英] How to detect that Ctrl+R was pressed?
问题描述
我正在用jquery编写一个函数,如果按下 Ctrl + R ,该函数将执行,但我似乎无法找出左右ctrl键码是什么...有人可以帮忙吗?
I'm coding a function in jquery that executes if Ctrl+R is pressed but I can't seem to find out what the left and right ctrl keycodes are... Can someone please help?
更新
///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}
????????????
似乎可以正常工作,但是如何在键盘输入中将llCtrlpress设置回'0'?
That seems like it would work fine but then how do I set llCtrlpress back to '0' on keyup?
推荐答案
您必须使用 keydown 函数来捕获 Ctrl 字符.这是我的 Ctrl + A 的实现:
You have to use the keydown function to trap Ctrl characters. Here is my implementation of Ctrl+A:
$(document).keydown(function(e) {
if (e.keyCode == 65 && e.ctrlKey) {
alert('ctrl A');
}
});
Ctrl-R更加强大,因为在大多数浏览器中,即重新加载页面,这意味着javascript无法运行,页面会被刷新.
Ctrl-R is tougher because in most browsers, that is Reload Page, which means the javascript doesn't run, the page is refreshed.
也请注意,keydown/keyup功能中的 keyCode 值与keypress功能中的不同.
Just a note as well, the keyCode value are different in the keydown/keyupup functions than in the keypress functions.
编辑:删除了ctrl变量,忘记了ctrlKey
Removed ctrl variable, forgot about ctrlKey
这篇关于如何检测到Ctrl + R被按下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!