$(document).keyup(function(e) {
if (e.shiftKey && e.keyCode == 65 && e.keyCode == 83) {
url = "https://stackoverflow.com/";
window.location.replace(url);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
答案 0 :(得分:-3)
我做了一个日志功能,以便您可以查看正在使用的键码。并使用Enter键进行设置。
document.body.addEventListener('keyup', logKey);
function logKey(e) {
console.log(`KeyCode: ${e.code}`)
if (e.code === 'KeyS' || e.code === 'Enter') {
let url = "https://stackoverflow.com/";
window.location.replace(url);
}
}
答案 1 :(得分:-4)
正如@putvande所说,一次只能检测一次按键,我建议您要检测多个按键组合,就像这样:
let shiftkey_press = false;
let A_press = false;
let S_press = false;
document.onkeydown = function(e) {
if (e.shiftKey ) {
shiftkey_press = true;
}
if (e.keyCode == 65 ) {
A_press = true;
}
if (e.keyCode == 83 ) {
S_press = true;
}
console.log("shiftkey_press:", shiftkey_press)
console.log("A_press:", A_press)
console.log("S_press:", S_press)
if (shiftkey_press && A_press && S_press ){
url = "https://stackoverflow.com/";
// window.location.replace(url);
alert(url);
}
}
是的,我知道。。。有点讨厌,但是我认为它可以解决问题,请让我知道它是否有效!
一个有效的示例:https://jsfiddle.net/b7an8L12/
我只是将您的window.location.replace替换为alert() “请务必打开控制台以查看发生了什么!” :-)