简体   繁体   English

在YUI中防止键监听器中的默认行为

[英]Prevent Default Behavior in Key Listeners in YUI

I have a web page where I'd like to remap Ctrl+N to a different behavior. 我有一个网页,我想将Ctrl + N重新映射到不同的行为。 I followed YUI's example of register Key Listeners and my function is called but Firefox still creates a new browser window. 我按照YUI的注册Key Listeners示例调用了我的函数,但Firefox仍然创建了一个新的浏览器窗口。 Things seem to work fine on IE7. 事情似乎在IE7上运行良好。 How do I stop the new window from showing up? 如何阻止新窗口显示?

Example: 例:

var kl2 = new YAHOO.util.KeyListener(document, { ctrl:true, keys:78 },
             {fn:function(event) {
                     YAHOO.util.Event.stopEvent(event); // Doesn't help
                     alert('Click');}});
kl2.enable();

It is possible to remove default behavior. 可以删除默认行为。 Google Docs overrides Ctrl+S to save your document instead of bringing up Firefox's save dialog. Google文档会覆盖Ctrl + S以保存文档,而不是显示Firefox的保存对话框。 I tried the example above with Ctrl+S but Firefox's save dialog still pops up. 我用Ctrl + S尝试了上面的例子,但Firefox的保存对话框仍然弹出。 Since Google can stop the save dialog from coming up I'm sure there's a way to prevent most default keyboard shortcuts. 由于谷歌可以阻止保存对话框出现,我确信有一种方法可以阻止大多数默认键盘快捷键。

The trick is the 'fn' function is whack. 诀窍是'fn'功能很糟糕。

Experimentally, you can see that the function type for fn takes two parameters. 在实验上,您可以看到fn的函数类型有两个参数。 The first param actually contains the TYPE of event. 第一个参数实际上包含事件类型。 The second one contains... and this is screwy: an array containing the codepoint at index 0 and the actual event object at index 1. 第二个包含......这很复杂:一个数组包含索引0处的代码点和索引1处的实际事件对象。

So changing your code around a bit, it should look like this: 所以改变你的代码,它应该是这样的:

function callback(type, args)
{
    var event = args[1]; // the actual event object
    alert('Click');

    // like stopEvent, but the event still propogates to other YUI handlers
    YAHOO.util.Event.preventDefault(event);
}
var kl2 = new YAHOO.util.KeyListener(document, { ctrl:true, keys:78 }, {fn:callback});
kl2.enable();

Also, for the love of lisp, don't use raw code points in your code. 此外,为了热爱lisp,请不要在代码中使用原始代码点。 Use 'N'.charCodeAt(0) instead of "78". 使用'N'.charCodeAt(0)代替“78”。 Or wrap it up as a function 或者将其作为一个函数包装起来

function ord(char)
{
    return char.charCodeAt(0);
}

I'm just guessing here but I don't think it can be done. 我只是猜测这里,但我不认为可以做到。

If it's possible it definitely shouldn't be. 如果可能的话绝对不应该。 Generic keyboard shortcuts are something you should not mess with. 通用键盘快捷键是你不应该搞乱的东西。 What's next? 下一步是什么? Hook the window close button to open a new window... 挂钩窗口关闭按钮打开一个新窗口...

Using YUI's event util, you could try and use the stopEvent method: 使用YUI的event util,您可以尝试使用stopEvent方法:

However, because most users are used to those keypresses doing a particular thing in the browser (new window in your example), I always avoid clashes, which in effect means I don't use any of the meta or control keys. 但是,因为大多数用户习惯于在浏览器中执行特定操作的那些按键(在您的示例中为新窗口),所以我总是避免冲突,这实际上意味着我不使用任何元或控制键。

I simply use letters, on their own, which is fine until you have text entry boxes, so this bit of advice might be less useful to you. 我只是单独使用字母,这样就可以了,直到你有文字输入框,所以这些建议可能对你没用。

Although overriding default browser shortcuts is not trivial, in some cases it is worth to do this since it gives a more professional look of the application. 尽管覆盖默认的浏览器快捷方式并非易事,但在某些情况下,这样做是值得的,因为它提供了更专业的应用程序外观。 Take a look at this script: 看看这个脚本:

http://www.openjs.com/scripts/events/keyboard_shortcuts/index.php#disable_in_input http://www.openjs.com/scripts/events/keyboard_shortcuts/index.php#disable_in_input

It turns out to work fine for me.. 结果对我来说工作正常..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM