I have a web page where I'd like to remap Ctrl+N to a different behavior. I followed YUI's example of register Key Listeners and my function is called but Firefox still creates a new browser window. Things seem to work fine on IE7. How do I stop the new window from showing up?
我有一个网页,我想将Ctrl + N重新映射到不同的行为。我按照YUI的注册Key Listeners示例调用了我的函数,但Firefox仍然创建了一个新的浏览器窗口。事情似乎在IE7上运行良好。如何阻止新窗口显示?
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. I tried the example above with Ctrl+S but Firefox's save dialog still pops up. Since Google can stop the save dialog from coming up I'm sure there's a way to prevent most default keyboard shortcuts.
可以删除默认行为。 Google文档会覆盖Ctrl + S以保存文档,而不是显示Firefox的保存对话框。我用Ctrl + S尝试了上面的例子,但Firefox的保存对话框仍然弹出。由于谷歌可以阻止保存对话框出现,我确信有一种方法可以阻止大多数默认键盘快捷键。
4 个解决方案
#1
7
The trick is the 'fn' function is whack.
诀窍是'fn'功能很糟糕。
Experimentally, you can see that the function type for fn takes two parameters. 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.
在实验上,您可以看到fn的函数类型有两个参数。第一个参数实际上包含事件类型。第二个包含......这很复杂:一个数组包含索引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. Use 'N'.charCodeAt(0) instead of "78". Or wrap it up as a function
此外,为了热爱lisp,请不要在代码中使用原始代码点。使用'N'.charCodeAt(0)代替“78”。或者将其作为一个函数包装起来
function ord(char)
{
return char.charCodeAt(0);
}
#2
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...
如果它可能肯定不应该。通用键盘快捷键是你不应该搞乱的东西。下一步是什么?挂钩窗口关闭按钮打开一个新窗口...
#3
0
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 *es, 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.
我只是单独使用字母,直到你有文本输入框,这很好,所以这些建议可能对你没用。
#4
0
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
It turns out to work fine for me..
结果对我来说工作正常..
#1
7
The trick is the 'fn' function is whack.
诀窍是'fn'功能很糟糕。
Experimentally, you can see that the function type for fn takes two parameters. 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.
在实验上,您可以看到fn的函数类型有两个参数。第一个参数实际上包含事件类型。第二个包含......这很复杂:一个数组包含索引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. Use 'N'.charCodeAt(0) instead of "78". Or wrap it up as a function
此外,为了热爱lisp,请不要在代码中使用原始代码点。使用'N'.charCodeAt(0)代替“78”。或者将其作为一个函数包装起来
function ord(char)
{
return char.charCodeAt(0);
}
#2
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...
如果它可能肯定不应该。通用键盘快捷键是你不应该搞乱的东西。下一步是什么?挂钩窗口关闭按钮打开一个新窗口...
#3
0
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 *es, 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.
我只是单独使用字母,直到你有文本输入框,这很好,所以这些建议可能对你没用。
#4
0
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
It turns out to work fine for me..
结果对我来说工作正常..