按住CMD键时,不会触发其他任何键的启动 [英] When CMD key is kept pressed, keyup is not triggered for any other key

查看:125
本文介绍了按住CMD键时,不会触发其他任何键的启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,当用户在特定文本框中按下 CMD + LEFT 时,我需要进行一些后处理。我需要在浏览器的默认功能之后 进行此操作(即,将插入符号移至当前物理行中的第一个位置之后)。

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).

问题是快捷键未被触发的 LEFT 键(或与此相关的任何键),只要 CMD 键按下即可。

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.

我尝试了CTRL和SHIFT键,并发现按预期的方式触发了辅助键。因此,如果您先按CTRL + LEFT,然后再释放LEFT,再再释放CTRL,则总共会发生四个事件,分别是2个按键和2个按键。但是,对于CMD密钥,我们有2次keydown,但是只有一个keyup事件(最后释放时是CMD密钥本身)。

我使用 SHIFT 键进行了尝试,发现按预期触发了辅助键的输入。因此,如果您执行 SHIFT + LEFT ,然后释放 LEFT ,然后释放 SHIFT ,则总共会发生4个事件,2个按键和2个按键。但是,对于 CMD 键,我们有2次按键按下,但是只有一个keyup事件(最后释放它时,是 CMD 键本身一个)。

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).

可能是什么?当 CMD 关闭时,有什么方法可以触发 LEFT 键(或任意键)的 keyup 吗?

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

我正在尝试在OSX 10.9.5上使用最新的Google Chrome浏览器。行为也与Firefox完全相同。因此,这不是Chrome问题。

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.

演示: http://jsfiddle.net/techfoobar/xu0o11nh/4/

本质上:

$('#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);
    });


推荐答案

这是使用meta键的已知行为,很遗憾,没有已知的解决方法。

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

对于您的情况,您可以考虑自己实现默认的浏览器行为(如何执行此操作),然后实施所需的自定义行为,并使用 _e.preventDefault()完成此操作。

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

这篇关于按住CMD键时,不会触发其他任何键的启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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