本文介绍了如果用户按下javascript中的两个键怎么办的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我编写了此脚本,以便您可以在网站上使用键盘快捷键,而我想知道如何执行多个键(即,不只是执行左箭头"键,而是"ctrl +左箭头" .这是我当前的语法:
So I wrote this script so that you can have keyboard shortcuts on your website, and I was wondering how to do multiple keys (ie instead of doing just the "left arrow" key, it would be the "ctrl + left arrow". Here's my current syntax:
var arrow = {
left: 37,
up: 38,
right: 39,
down: 40
};
function DoSomething() {}
$(document).ready(function() { // requires jQuery
$("body").keydown(function(event) {
if(event.keyCode == arrow.left) {
DoSomething();
}
}
}
但是我想做的是这样的:
But what I would like to do is something like this:
var arrow = {
left: 37,
up: 38,
right: 39,
down: 40
},
ctrl = 17;
function DoSomething() {}
$(document).ready(function() { // requires jQuery
$("body").keydown(function(event) {
if(event.keyCode == ctrl && arrow.left) {
DoSomething();
}
}
}
推荐答案
jQuery中提供的事件对象告诉您是否按下了ctrl
键.
The event object provided in jQuery tells you if the ctrl
key is being pressed.
$(document).on("keydown", function (event) {
if (event.ctrlKey && event.which === arrow.left) {
console.log("You pressed left, and control.");
}
});
演示: http://jsfiddle.net/zcMXR/
这篇关于如果用户按下javascript中的两个键怎么办的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!