当用户在 Chrome 上按下键盘时,如何检测“删除”和“.”?

发布于 2024-11-24 11:53:37 字数 1036 浏览 2 评论 0原文

当我按下 . 时,它会触发三个事件:keydownkeypresskeyup

keydown
  which: 190 == ¾
  keyCode: 190 == ¾

keypress
  which: 46 == .
  keyCode: 46 == .

keyup
  which: 190 == ¾
  keyCode: 190 == ¾

当我按下delete时,它会触发两个事件:keydownkeyup

keydown
  which: 46 == .
  keyCode: 46 == .

keyup
  which: 46 == .
  keyCode: 46 == .

我想按 . 并能够获取相应的字符(46 == .)。但在 keydownkeyup 上,我得到 190,即 3/4。在keypress 上,我得到了正确的值,但是当我按delete 时,不会触发此事件。

当我按 delete 时,我在 keydownkeyup 上得到代码 46。但代码 46.,而不是 delete

如果是按下 . 键或 delete 键,如何创建一个事件来捕获这两个键并区分它们的区别?

要测试的页面: http://jsfiddle.net/Eg9F3/

When I press . it fires the three events, keydown, keypress and keyup.

keydown
  which: 190 == ¾
  keyCode: 190 == ¾

keypress
  which: 46 == .
  keyCode: 46 == .

keyup
  which: 190 == ¾
  keyCode: 190 == ¾

When I press delete it fires two events, keydown and keyup.

keydown
  which: 46 == .
  keyCode: 46 == .

keyup
  which: 46 == .
  keyCode: 46 == .

I want to press . and be able to get the corresponding character (46 == .). But on keydown and keyup I get 190 which is ¾. On the keypress I get the correct value, but this event is not fired when I press delete.

When I press delete I get the code 46 on keydown and keyup. But the code 46 is a . and not delete.

How can I make an event to capture both keys and tell the difference, if it was a . pressed or delete key?

Page to test: http://jsfiddle.net/Eg9F3/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情栀口红 2024-12-01 11:53:37

我认为最好的解决方案是映射您想要的键(demo),然后使用 e. which 交叉引用按下的键。有一个关于跨浏览器 keyCode 值的 很好的参考,它可以工作在这种情况下,因为 jQuery 标准化了 e.which 值。

var keys = {
  46  : 'del',
  190 : '.'
};

$("textarea").bind('keyup', function(e){
  $('#r').append( 'keyup (' + e.which + '): ' + (keys[e.which] || String.fromCharCode(e.which)) );
});

这与 jQuery UI 使用 - 查看文件顶部的键码交叉引用?这也是我在 keycaster 插件中使用的方法。

I think the best solution would be to map the keys you want (demo), then use e.which to cross-reference what key was pressed. There is a good reference of cross-browser keyCode values, which work in this case because jQuery normalizes the e.which value.

var keys = {
  46  : 'del',
  190 : '.'
};

$("textarea").bind('keyup', function(e){
  $('#r').append( 'keyup (' + e.which + '): ' + (keys[e.which] || String.fromCharCode(e.which)) );
});

This is similar to the method that jQuery UI uses - see the keycodes cross-reference at the top of the file? And it is also the method I use in the keycaster plugin.

︶葆Ⅱㄣ 2024-12-01 11:53:37

事实上,这很奇怪,但却是逻辑。

函数 String.fromCharCode 正在处理真正的字符代码,而不是 KB 操作(左移、删除...)

为什么不简单地按 keyCode 进行过滤?

In fact it's strange but it is logic.

The function String.fromCharCode is working with real char code, not with KB actions (left, delete...)

Why don't filter by keyCode simply ?

诗笺 2024-12-01 11:53:37

我强制执行与 Firefox 相同的行为,jsFiddle 上的示例

$("textarea").keydown(function(e) {
    // if the key pressed was found on the list of specialChars
    if (specialChars[e.keyCode])
    {
        // triggers the fake event
        $("textarea").trigger("chromekeypress", e);

        // temporary avoid keypress from firing
        $("textarea").unbind("keypress");
        setTimeout(function(){ $("textarea").bind("keypress", handleKey); },10);
    }
});

$("textarea").keypress(handleKey); // main event

$("textarea").bind("chromekeypress", chromeKeyPress); // chrome workaround

function chromeKeyPress(i,e){
    e.type="chromekeypress";
    e.which = 0; // copy Firefox behavior, which == 0 means a special key pressed
    handleKey(e); // fires the main event
}
function handleKey(e) {

    // which is going to be 0 if it is a special key
    // and keycode will have the code of the special key
    // or
    // which will have the value of the key

    $("#r").html($("#r").html() + "\n" +
        e.type + "\n" +
        "  which: " + e.which + " == " + String.fromCharCode(e.which) + "\n" +
        "  keyCode: " + e.keyCode + " == " + String.fromCharCode(e.keyCode) + "\n" +
     "");
}

I've forced the same behavior as Firefox, example on jsFiddle

$("textarea").keydown(function(e) {
    // if the key pressed was found on the list of specialChars
    if (specialChars[e.keyCode])
    {
        // triggers the fake event
        $("textarea").trigger("chromekeypress", e);

        // temporary avoid keypress from firing
        $("textarea").unbind("keypress");
        setTimeout(function(){ $("textarea").bind("keypress", handleKey); },10);
    }
});

$("textarea").keypress(handleKey); // main event

$("textarea").bind("chromekeypress", chromeKeyPress); // chrome workaround

function chromeKeyPress(i,e){
    e.type="chromekeypress";
    e.which = 0; // copy Firefox behavior, which == 0 means a special key pressed
    handleKey(e); // fires the main event
}
function handleKey(e) {

    // which is going to be 0 if it is a special key
    // and keycode will have the code of the special key
    // or
    // which will have the value of the key

    $("#r").html($("#r").html() + "\n" +
        e.type + "\n" +
        "  which: " + e.which + " == " + String.fromCharCode(e.which) + "\n" +
        "  keyCode: " + e.keyCode + " == " + String.fromCharCode(e.keyCode) + "\n" +
     "");
}
回忆那么伤 2024-12-01 11:53:37

“keypress”处理程序中“which”的值与“keydown”或“keyup”处理程序中“which”的含义不同。其中,它是键盘上按键的键码,而不是字符序数。在“keypress”中,它是字符,但您不会得到 Del 的“keypress”(正如您所注意到的)。

因此,“keypress”处理程序(“.”)中的 46 与 Del 的“keydown”和“keyup”事件中的 46 不同。

您可能应该使用“keydown”或“keyup”并检查键码。

The value of "which" in a "keypress" handler does not have the same meaning as that of "which" in a "keydown" or "keyup" handler. In those, it's the keycode for the key on the keyboard, and not a character ordinal. In "keypress" it is the character, but you don't get a "keypress" for Del (as you've noticed).

Thus, that's a different 46 in the "keypress" handler (for ".") than in the "keydown" and "keyup" events for Del.

You probably should use either "keydown" or "keyup" and check for the keycode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文