JavaScript 无法捕获“SHIFT+TAB”组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5707758/
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
JavaScript can't capture "SHIFT+TAB" combination
提问by Bohemian
For whatever reason I can't capture "SHIFT+TAB" combination. I am using the latest jQuery.
无论出于何种原因,我都无法捕获“SHIFT+TAB”组合。我正在使用最新的 jQuery。
Same result if I use other ajax/javascript, etc.
如果我使用其他 ajax/javascript 等,结果相同。
Here is a simple example that should work as I currently understand it...
这是一个简单的例子,应该按照我目前的理解工作......
event.which or event.KeyCode are always "undefined" only shiftKey exists in a scenario involving a "SHIFT+TAB" or backward keyboard traversal, traditionally inherent in windows based apps/web or otherwise...
event.which 或 event.KeyCode 总是“未定义”,只有 shiftKey 存在于涉及“SHIFT + TAB”或向后键盘遍历的场景中,传统上基于 Windows 的应用程序/网络或其他方式固有...
function ShiftTab()
{
debugger;
if(event.KeyCode == 9 && event.shiftKey) // neither this line nor the following work
// if (event.which == 9 && event.shiftKey) // shift + tab, traverse backwards, using keyboard
{
return true;
}
else
{
return false;
}
}
this seems to be yet another item related to tab order that no longer works as it traditionally worked in Microsoft.Net WinForm/WebForm based apps.
这似乎是另一个与选项卡顺序相关的项目,它不再像传统上在基于 Microsoft.Net WinForm/WebForm 的应用程序中那样工作。
回答by Eli
If you are using jQuery, this should be how the code is working. Make sure keyCode
is lower case. Also, jQuery normalizes keyCode
into which
:
如果您使用 jQuery,这应该是代码的工作方式。确保keyCode
是小写。此外,jQuery 规范化keyCode
为which
:
$(document).keyup(function (e) {
if (e.which === 9 && e.shiftKey) {
ShiftTab();
}
});
If you're into terse JavaScript:
如果您喜欢简洁的 JavaScript:
$(document).keyup(function (e) {
e.which === 9 && e.shiftKey && ShiftTab();
});
jQuery 1.7+ on
syntax:
jQuery 1.7+on
语法:
$(document).on('keyup', function (e) {
e.which === 9 && e.shiftKey && ShiftTab();
});
回答by Adriang
I created a function which I wired up to my button's onkeydown event. I used onkeydown, because onkeypress would not capture my tab key press
我创建了一个函数,将其连接到按钮的 onkeydown 事件。我使用了 onkeydown,因为 onkeypress 不会捕获我的 Tab 键按下
function ShiftTab(evt) {
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode; // for trans-browser compatibility
if (charCode === 9) {
if (e.shiftKey) {
$('#controlName').focus();
return false;
} else {
return true;
}
}
I took this approach to deal with two specific problems:
我采用这种方法来处理两个具体问题:
- onkeypress would not capture tab key press
- When click shift-tab, shift key press would trigger function, so I had nest the shiftkey modifier check
- onkeypress 不会捕获 Tab 键按下
- 当单击 shift-tab 时,按下 shift 键会触发功能,所以我嵌套了 shiftkey 修饰符检查
回答by mukund patel
use same code inside keypress event. the tab changes the element between keypress and keyup. here we get event.key = tab and event.shiftKey = true.
在按键事件中使用相同的代码。该选项卡更改 keypress 和 keyup 之间的元素。在这里我们得到 event.key = tab 和 event.shiftKey = true。