最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - When CMD key is kept pressed, keyup is not triggered for any other key - Stack Overflow

programmeradmin3浏览0评论

I am developing an application where I need to do some post-processing when the user presses CMD+LEFT on a particular text-box. I need to do this after the browser's default functionality (i.e. after it takes the caret to first position in current physical line).

The problem is keyup is not being triggered for the LEFT key (or any key for that matter) as long as the CMD key is down.

I tried this with CTRL and SHIFT keys and found that keyup gets triggered as expected for the secondary key. So if you do CTRL+LEFT and then release LEFT and then release CTRL, you get four events in total, 2 keydowns and 2 keyups. For the CMD key however, we get 2 keydowns, but only one keyup event (the one for CMD key itself when we release it in the end).

I tried this with SHIFT key and found that keyup gets triggered as expected for the secondary key. So if you do SHIFT+LEFT and then release LEFT and then release SHIFT, you get 4 events in total, 2 keydowns and 2 keyups. For the CMD key however, we get 2 keydowns, but only one keyup event (the one for CMD key itself when we release it in the end).

What could it be? Is there any way I can get keyup triggered for the LEFT key (or any key) when CMD is down?

I'm trying this with the latest Google Chrome on OSX 10.9.5. The behaviour is exactly the same on Firefox too. So this isn't a Chrome issue.

Demo: /

Essentially:

$('#mytextbox')

    // this gets correctly triggered for the meta key as well as the secondary key
    // when you press CMD and LEFT in sequence, you get two lines in the console one for 
    // the CMD key and one for the LEFT key
    .keydown(function(_e) {
        console.log('Keydown: ' + _e.keyCode);
    })

    // however, if I release the LEFT key (while keeping the CMD key down)
    // this does NOT get triggered for the LEFT key
    .keyup(function(_e) {
        console.log('Keyup: ' + _e.keyCode);
    });

I am developing an application where I need to do some post-processing when the user presses CMD+LEFT on a particular text-box. I need to do this after the browser's default functionality (i.e. after it takes the caret to first position in current physical line).

The problem is keyup is not being triggered for the LEFT key (or any key for that matter) as long as the CMD key is down.

I tried this with CTRL and SHIFT keys and found that keyup gets triggered as expected for the secondary key. So if you do CTRL+LEFT and then release LEFT and then release CTRL, you get four events in total, 2 keydowns and 2 keyups. For the CMD key however, we get 2 keydowns, but only one keyup event (the one for CMD key itself when we release it in the end).

I tried this with SHIFT key and found that keyup gets triggered as expected for the secondary key. So if you do SHIFT+LEFT and then release LEFT and then release SHIFT, you get 4 events in total, 2 keydowns and 2 keyups. For the CMD key however, we get 2 keydowns, but only one keyup event (the one for CMD key itself when we release it in the end).

What could it be? Is there any way I can get keyup triggered for the LEFT key (or any key) when CMD is down?

I'm trying this with the latest Google Chrome on OSX 10.9.5. The behaviour is exactly the same on Firefox too. So this isn't a Chrome issue.

Demo: http://jsfiddle.net/techfoobar/xu0o11nh/4/

Essentially:

$('#mytextbox')

    // this gets correctly triggered for the meta key as well as the secondary key
    // when you press CMD and LEFT in sequence, you get two lines in the console one for 
    // the CMD key and one for the LEFT key
    .keydown(function(_e) {
        console.log('Keydown: ' + _e.keyCode);
    })

    // however, if I release the LEFT key (while keeping the CMD key down)
    // this does NOT get triggered for the LEFT key
    .keyup(function(_e) {
        console.log('Keyup: ' + _e.keyCode);
    });
Share Improve this question edited Nov 22, 2019 at 10:56 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Dec 9, 2014 at 13:25 techfoobartechfoobar 66.7k14 gold badges116 silver badges138 bronze badges 4
  • In chrome I get all events. jQuery send a key in the event which is called ctrlKey where you can check if ctrl is pressed... i dodnt check which is cmd but you can easily find it out by console complete event – Xandrios93 Commented Dec 15, 2014 at 9:57
  • @Mephiztopheles - The problem is only with the CMD key (event.metaKey courtesy jQuery). – techfoobar Commented Dec 15, 2014 at 12:52
  • Either I didn't understand your problem or I couldn't simulate it. Here is my console: 70(index):34 Keydown: 17 (index):34 Keydown: 37 (index):37 Keyup: 37 (index):37 Keyup: 17 – Marco Aurélio Deleu Commented Dec 16, 2014 at 19:47
  • if you check my console, you'll see that I held CTRL and it triggered Keydown for 70 times, then I triggered down LEFT KEY, then I triggered up LEFT KEY, then I triggered up CTRL. puu.sh/dxnVq/8a9a74a2f8.png – Marco Aurélio Deleu Commented Dec 16, 2014 at 19:50
Add a comment  | 

3 Answers 3

Reset to default 7 +250

This is known behavior with the meta key, and there is unfortunately no known workaround.

For your case, you may consider implementing the default browser behavior yourself (how to do that), then implement the custom behavior you need, and finish it off with _e.preventDefault();

This is almost certainly to do with system set 'hot keys'.Apparently according to the docs this missing keyup event is expected behaviour too.

When i do cmdspace i dont even get a keydown event for the space as the spotlight window appears.

On your mention of the ctrl key: because i have 'spaces' set up when i ctrlleft or ctrlright i get no left or right keydown events fired, however ctrlup or ctrldown at least fire their keydown events.

I think it would be difficult to assume the use of a system that does not have the "default" hot keys setup.

don't think you'll need a settimeout.

i've changed the code to detect 2 keydowns. this can be refactored further to a cleaner code.

http://jsfiddle.net/7nd7hf16/1/

var cmdDown = false;
$('#foo')
.keydown(function(_e) {
    if(_e.keyCode == 91)
        cmdDown = true;

    if(cmdDown && _e.keyCode == 37)
        console.log('cmd + left'); 

    console.log('Keydown: ' + _e.keyCode);
})
.keyup(function(_e) {
    if(_e.keyCode == 91)
        cmdDown = false;

    console.log('Keyup: ' + _e.keyCode);
})
.focus();
发布评论

评论列表(0)

  1. 暂无评论