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

问题描述

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

I am developing an application where I need to do some post-processing when the user presses + 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 key (or any key for that matter) as long as the key is down.

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

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

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

What could it be? Is there any way I can get keyup triggered for the key (or any key) when 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.

演示:

本质上:

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


推荐答案

这是,很遗憾,没有已知的解决方法。

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键时,不会触发其他任何键的启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:25