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

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

问题描述

使用 jQuery 热键,我尝试将 Alt + HAlt + 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 在某些环境中也存在问题,因为 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和野生动物园)
  • 如果用户在释放字母键之前释放 ALT 键,则不会工作
  • 如果用户在按 ALT 键之前按字母键,则它不起作用
  • 尝试使用 ACCESSKEY 属性,但这只会设置焦点.我想要单击超链接和/或触发某些元素的基于 click() 事件在一些 ALT-X 组合上.
  • 虽然这对于企业网络应用程序来说是可以接受的(因为区域设置和浏览器支持通常是较窄的焦点,并且由于用户通常受过软件培训),这可能是不可接受的网站(由于使用大量浏览器,本地化增加复杂性的担忧等)......必须考虑一下还有一些.

希望这对某人有所帮助...随时提出建设性的批评,因为这是客户在我构建 Web 应用程序时总是询问他们的功能.(他们习惯了 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天全站免登陆