复制代码 代码如下:
$(document).ready(function(){
var $down = $(#down);
var $press = $(#press);
var $up = $(#up);
$(document).keydown(function(event){
$down.append(string.fromcharcode(event.keycode) + );
if (event.ctrlkey) {
alert(ctrl);
}
}).keyup(function(event){
$up.append(string.fromcharcode(event.keycode) + );
}).keypress(function(event){
$press.append(string.fromcharcode(event.keycode) + );
});
});
方法是触发down时,把keycode push到数组里,并删除重复元素;触发up时,用$.grep从数组中删除该keycode。
在任意时刻,这个数组里都保存了当前所按的按键,并且顺序是根据按键顺序排列的。
用jquery判断当前所按的按键
方法就是用一个外部的数组保存当前按键。
在触发keydown时,把keycode push到数组里,并删除重复元素;触发keyup时,用$.grep从数组中删除该keycode。
实现代码如下:
复制代码 代码如下:
当前按键:
