简体   繁体   中英

Simple key combo in jquery

I'm looking to catch simple key combos such as Ctrl + A . Here's my stab at it:

var isCtrl = false;
$(window).keydown(function (e) {
    if (e.keyCode == 17) isCtrl = true;
    if (isCtrl && e.keyCode == 65) alert('hi');
});

Is this a good and robust approach? If not, how can I improve on it?

由于您使用的是jQuery,请尝试利用该库提供的内容对.ctrlKey和.which进行击键标准化:

if (e.which == 17 && e.ctrlKey) alert('hi');

您可以使用e.ctrlKey代替isCtrl

你的第一个刺看起来不错-只是记得设置isCtrl回假的keyup (如果e.keyCode == 17再次)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM