jQuery .keypress()可以同时检测多个密钥吗? [英] Can jQuery .keypress() detect more than one key at the same time?

查看:115
本文介绍了jQuery .keypress()可以同时检测多个密钥吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery是否有办法检测到同时按下了多个键?

Is there a way for jQuery to detect that more than one key was pressed at the same time?

是否有任何替代方法允许按下两个键同一时间被检测到?

Is there any alternative that allows for pressing two keys at the same time to be detected?

推荐答案

为了检测被按下的多个键,请使用 keydown keyup events。

In order to detect multiple keys being held down, use the keydown and keyup events.

var keys = {};

$(document).keydown(function (e) {
    keys[e.which] = true;
});

$(document).keyup(function (e) {
    delete keys[e.which];
});

我在这里整理了一个演示: http://jsfiddle.net/gFcuU/ 。这很有趣,虽然我注意到我的键盘最多只能检测到6个键。

I've put together a demo here: http://jsfiddle.net/gFcuU/. It's kind of fun, though I noticed my keyboard is only able to detect at most 6 keys.

这篇关于jQuery .keypress()可以同时检测多个密钥吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆