/**设置快捷键(常用判断)----------------------------------------*/
onkeyup = e => {
var k = e.keyCode || e.which || e.charCode, ck = e.ctrlKey || e.metaKey, ak = e.altKey, sk = e.shiftKey;
/**独立键*/
if (k == 13) {/*按下回车键Enter↩的时候执行*/
}
if (k == 27) {/*按下退出键Esc的时候执行*/
}
if (k == 36) {/*执行Home*/
}
if (k == 35) {/*执行End*/
}
if (k == 48 || k == 96) {/*执行0、Num0*/
}
if (k == 49 || k == 97) {/*执行1、Num1*/
}
if (k == 37 || k == 38 || k == 27 || k == 8) { /*按下 ← ↑ Esc <BACKSAPCE 的时候执行*/
}
if (k == 33 || k == 37 || k == 38 || k == 100 || k == 104 || e.keyCode === 80) {/*执行Page Up、←、↑、Num4(←)、Num8(↑)、P*/
}
if (k == 34 || k == 39 || k == 40 || k == 102 || k == 98 || e.keyCode === 78) {/*执行Page Down、→、↓、Num2(→)、Num6(↓)、N*/
}
/**组合键*/
if (ck && k == 13) {/*Ctrl+Enter*/
alert('submit');
}
if (sk && k == 13) {/*Shift+Enter*/
alert('change line');
}
if (ck && ak && sk && k == 83) {/*Ctrl+Alt+Shift+S*/
alert('导出web图片');
}
}
/**设置快捷键(简单版本)----------------------------------------*/
onkeyup = e => {
e.preventDefault();
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
/*按下Ctrl+S*/
return false;
}
if (e.key === 'Meta' && e.key === 's') {
/*按下win+S*/
return false;
}
if (e.altKey && e.key === 's') {
/*按下Alt+S*/
return false;
}
if (e.shiftKey && e.key === 's') {
/*按下Shift+S*/
return false;
}
}
键码了解下文章来源:https://www.toymoban.com/news/detail-776767.html
[JavaScript键码表]身为一个键盘侠,不搞清楚键码映射表怎么可以???_码表映射脚本_你挚爱的强哥的博客-CSDN博客keycode 0 =keycode 1 =keycode 2 =keycode 3 =keycode 4 =keycode 5 =keycode 6 =keycode 7 =keycode 8 = BackSpacekeycode 9 = Tabkeycode 10 =keycode 11 =keycode 12 = Clearkeycode 13 = Enterkeycode 14 =keycode 15 =keycode 16 = Shift_Lkeycode 17.https://blog.csdn.net/qq_37860634/article/details/120446294文章来源地址https://www.toymoban.com/news/detail-776767.html
到了这里,关于JS设置键盘快捷键、组合键的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!