在jQuery中检测单个按键事件上的多个键 [英] Detect multiple keys on single keypress event in jQuery

查看:103
本文介绍了在jQuery中检测单个按键事件上的多个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以组合使用按键混合'来触发单个事件?

Is it at all possible to combine a mixture of keypress' to fire a single event?

$(document).keyup(function(e){
    if (e.keyCode == 68 && e.keyCode == 69 && e.keyCode == 86) {
        alert('oh hai');
    }
});

我在Chrome中尝试过但事件并未触发。

I've tried it in Chrome but the event doesn't fire.

叫我疯了,但我正在写一个Chrome扩展程序,想要推 D + E + V 键一起强制它进入一个隐藏的开发者模式。

Call me crazy but I am writing a Chrome extension and want to push D+E+V keys together to force it into a hidden developer mode.

推荐答案

如果你想检测到 d e v 键全部同时关闭,你必须同时观察 keydown keyup 并保留已关闭的地图。如果他们全部失败,请解雇您的活动。

If you want to detect that the d, e, and v keys were all down at the same time, you have to watch both keydown and keyup and keep a map of the ones that are down. When they're all down, fire your event.

例如:实时副本 | 来源

var map = {68: false, 69: false, 86: false};
$(document).keydown(function(e) {
    if (e.keyCode in map) {
        map[e.keyCode] = true;
        if (map[68] && map[69] && map[86]) {
            // FIRE EVENT
        }
    }
}).keyup(function(e) {
    if (e.keyCode in map) {
        map[e.keyCode] = false;
    }
});

我假设你不关心他们被压下的顺序(因为那将是一个只要他们在某个时刻同时都处于失望状态,可靠地按下疼痛。

I assume you don't care what order they're pressed down in (as that would be a pain to reliably press) as long as they're all down at the same time at some point.

这篇关于在jQuery中检测单个按键事件上的多个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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