检测javascript中的组合键击
问题描述:
我正在创建一个包含问题的html页面(类似情况的拼图)。这个问题的答案是Windows 7。但我希望用户只有按下 windows键后跟7
才能解决问题。我想为此创建一个JavaScript函数。我可以通过获取击键的ascii值来捕获单个按键。但是,当我尝试联合使用它不起作用时。怎么去解决这个问题..任何想法..
I am creating a html page which contains a question ( a puzzle like situation ). The answer for the question is "Windows 7". But I want the user to get through the question only if he presses windows key followed by 7
. I want to create a JavaScript function for this. I am able to capture the individual key press by getting the ascii value of the keystroke. But when I try to do it combinedly its not working. How to go about solving this.. any ideas..
答
http://jsfiddle.net/loktar/MsNPW/5/
var keys = [];
document.onkeydown = function(evt){
var key = evt.keyCode;
keys[key] = true;
if(keys[91] && keys[55]){
console.log("win7");
}
}
document.onkeyup = function(evt){
var key = evt.keyCode;
keys[key] = false;
}