When CMD key is kept pressed, keyup is not triggered for any other key

我的梦境 提交于 2020-11-26 07:01:09

问题


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);
    });

回答1:


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();




回答2:


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.




回答3:


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();


来源:https://stackoverflow.com/questions/27380018/when-cmd-key-is-kept-pressed-keyup-is-not-triggered-for-any-other-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!