`
$("#txtboxToFilter").keydown(function (e) {
// Allow: backspace, delete, tab, escape, enter and .
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
// Allow: Ctrl+A
(e.keyCode == 65 && e.ctrlKey === true) ||
// Allow: Ctrl+C
(e.keyCode == 67 && e.ctrlKey === true) ||
// Allow: Ctrl+X
(e.keyCode == 88 && e.ctrlKey === true) ||
// Allow: home, end, left, right
(e.keyCode >= 35 && e.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
});`
答案 0 :(得分:0)
如果您只想允许某些键盘快捷键,则可以禁用除允许通过之外的任何选项。您还可以决定列出要禁用的所有内容,并允许其他所有内容执行。我认为可以采用的一种简单方法是,如果同时按下Ctrl键和Alt键,则禁用它们,例如:
$("#txtboxToFilter").keydown(function (e) {
if (e.ctrlKey === true && e.altKey === true) {
return;
}
else {
//Do whatever;
if (e.key === "Tab") {
//Tab was pressed
}
else if (e.key === "H") {
//Shift H was pressed
}
else if (["Home", "End", "PageUp", "PageDown"].includes(e.key) === true) {
//Navigational Keys Were Pressed
}
else if (["ArrowUp", "ArrowDown", "ArrowRight", "ArrowLeft"].includes(e.key) === true) {
//Directional Arrows Were Pressed
}
}
});
并且我建议您使用e.key
而不是e.keyCode
,e.which
或code
,因为它受到更多的支持,并且更容易理解。只需看一下上面的代码片段,就有e.key
的示例。此外,因为使用了键名,所以不会与数字混淆。如果要在Windows上使用Windows键,则可以使用Chromebook上的搜索键e.key === "Meta"
。
希望这些额外的信息有所帮助!