jQuery热键:阻止IE运行自己的快捷方式,如Alt + H. [英] jQuery hotkeys: prevent IE from running its own shortcuts, like Alt+H

查看:143
本文介绍了jQuery热键:阻止IE运行自己的快捷方式,如Alt + H.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery热键,我尝试将 Alt + H Alt + C 等快捷方式绑定到某些我网站中的具体操作。除了IE之外,事件传播在所有浏览器中停止(应该如此)。首先,有可能实现这一目标吗?如果是这样,怎么做?

Using jQuery hotkeys, I try to bind shortcuts like Alt + H, Alt + C to some specific actions in my site. The event propagation stops (as it should) in all browsers but IE. First of all, is it possible to achieve this? If so, how?

这是一个代码示例:

jQuery(document).bind('keydown', 'Alt+H',
     function (event)
     {
        $this.toggleMenu( 'H' );
        if(jQuery.browser.msie) {
            event.cancelBubble = true;
        }
                    else
                    {
            event.stopPropagation();
        }
        return false;
     } );

在IE中,行 $ this.toggleMenu('H'); 被执行,但 cancelbubble 似乎没有效果(浏览器打开其帮助菜单)

In IE, the line $this.toggleMenu( 'H' ); gets executed, but then cancelbubble seems to have no effect (the browser opens its "Help" menu)

推荐答案

这总是一个奇怪的尝试(由于浏览器的内置热键等),但我很幸运使用了onKeyUp事件。

This is always a weird thing to attempt (due to browser's built-in hotkeys, etc), but I've had luck with using the onKeyUp event.

keydown的问题在于它一次只能发送一个密钥。因此,如果你点击ALT它是keydown的一个触发器,然后C是第二个触发器。 KeyPress在某些环境中也有probs,因为像ALT和CTRL这样的修饰键并不算作按键(我想......任何人都能更好地解释这个吗?)。

The problem with keydown is that it may only be sending one key at a time. So if you hit ALT it's one trigger of keydown, and then C is a second trigger. KeyPress also has probs in some environments because modifier keys like ALT and CTRL don't count as keypresses (I think.. can anybody explain this better?).

如果你使用keyup,你可以检查ALT是否被按下,同时检查密钥。仍在解决问题,但试试这个(我只在win7中验证:ie9和chrome):

If you use keyup you can check whether ALT is being held down while also checking the key. Still working out the kinks, but try this (I only verified in win7: ie9 and chrome):

$(window).keyup(function (e)
{
    // warning: only tested with EN-US keyboard / locale

    // check whether ALT pressed (and not ALT+CTRL, ALT+SHIFT, etc)
    var key = e.key || String.fromCharCode(e.keyCode);
    if (e.altKey && !e.ctrlKey && !e.shiftKey && key)
    {
        // if ALT pressed, handle the keycode shortcut
        var keyPressed = key.toUpperCase(); // normalize input
        switch (keyPressed)
        {
            case "C":
                // do stuff for ALT-C
                break;
            case "H":
                // do stuff for ALT-H 
                break;
        }
    }
});

问题/疑问:


  • 使用jQuery,但纯JS解决方案并不太复杂

  • 是否可以通过取消
    事件来阻止默认浏览器行为?

  • 它可以在Chrome和IE9以外的浏览器中使用吗? (可能是FF和
    Safari)

  • 如果用户在释放字母键之前释放ALT键,则不会
    工作

  • 如果用户在按ALT键之前按下字母键,则不起作用

  • 尝试使用ACCESSKEY属性,但只设置焦点..我想要
    点击超链接和/或在某些ALT-X组合上基于
    触发一些元素的click()事件。

  • 虽然这对于企业Web应用程序可能是可接受的(因为
    locale)和浏览器支持通常是较窄的焦点,并且由于用户
    通常在软件上受过培训),因此可能无法接受
    网站(由于使用了大量浏览器,本地化为
    )关注增加复杂性等)...将不得不考虑
    更多。

  • Uses jQuery, but pure JS solution is not too complicated
  • Is it possible to prevent default browser behavior by canceling event?
  • Will it work in browsers other than Chrome and IE9? (probably FF and Safari)
  • If user releases ALT key BEFORE releasing the letter key, it doesn't work
  • If user presses letter key BEFORE pressing ALT key, it doesn't work
  • Tried using ACCESSKEY attributes, but that only sets focus.. I wanted to click a hyperlink and/or fire some element's click() event based on some ALT-X combination.
  • While this may be acceptable for an enterprise web application (since locale and browser support is usually narrower focus, and since users are typically trained on the software), it may not be acceptable for web sites (due to large number of browsers in use, localization concerns that add to complexity, etc)... would have to think about it some more.

希望这有助于有人......随意提出建设性的批评,因为这是客户总是询问我何时建立他们的网络应用程序的功能。 (他们习惯于80年代和90年代的传统表单驱动应用程序,并且需要大量的键盘支持,这是可以理解的,特别是当他们为此付费时!)

Hope this helps somebody... feel free to bring on the constructive criticism as this is a feature clients are ALWAYS asking about when I build them web apps. (they are used to the legacy form-driven apps of the 80s and 90s and want extensive keyboard support, which is understandable, especially when they pay for it!)

这篇关于jQuery热键:阻止IE运行自己的快捷方式,如Alt + H.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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