问题描述
我实施了ace编辑器后出现了这个问题...
This question came up after i implemented the ace editor...
这是该链接:
我有ace编辑器,并且for循环:
I have the ace editor, and thx to the for loop:
for (key in editor.keyBinding.$defaultHandler.commandKeyBinding) {
if (key !== "ctrl-d")
delete editor.keyBinding.$defaultHandler.commandKeyBinding[key];
}
我有我自己的keyBindings,而ace编辑器也有自己的,我删除了,除了一个,其余所有都是,而那个是CTRL + D来删除一行...
I have my own keyBindings, and the ace editor has its own, witch i deleted, all but one, and that one is the CTRL+D to remove a line...
但是,我的浏览器在书签上已经有ctrl-d的内容了,现在我需要防止出现这种情况,有什么主意吗?
but, my browser has already the ctrl-d stuff on the bookmark, and i need now to prevent that, any ideas?
推荐答案
我在ace-editor中进行了测试,默认功能似乎会自行阻止默认键盘快捷键.但是为了回答您的问题,您可以使用ctrl + d
和command + d
的事件侦听器,而不是使用e.preventDefault()
和e.stopPropagation()
的事件侦听器...现在了解如何与ace-editor一起使用它:
I tested in ace-editor and default function seem to block default keyboard shortcuts on its own. but for the sake of answering your question, you can use an event listener for ctrl + d
and command + d
than use e.preventDefault()
and e.stopPropagation()
... now to how would you use it with ace-editor:
document.addEventListener("keydown", function(e) {
if (e.key.toLowerCase() == "d" && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
editor.execCommand("removeline");
e.preventDefault();
e.stopPropagation();
}
}, false);
- 注意我如何使用
e.key.toLowerCase() == "d"
而不是e.keyCode == 68
...这是因为 KeyboardEvent.keyCode 现在被标记为已弃用,因此我使用了 KeyboardEvent.key .使用.toLowerCase()
,即使在切换Caps Lock时也可以使用快捷键. - 使用
e.key
而不是e.keyCode
的一个缺点是e.key
仅适用于英语/拉丁语键盘输入,因此键必须按字面意义为"d",而e.keyCode
则可以与其他语言输入(如阿拉伯语)一起使用例如(ctrl +ي)...,以便您决定使用哪个. - notice how I used
e.key.toLowerCase() == "d"
instead ofe.keyCode == 68
... this is because KeyboardEvent.keyCode is now marked as deprecated so I used KeyboardEvent.key instead. the.toLowerCase()
is used so the shortcut work even when Caps Lock is toggled. - one downside of using
e.key
instead ofe.keyCode
is thate.key
will only work with English/Latin keyboard input so the key must be literally "d" whilee.keyCode
will work with other languages input like Arabic for example (ctrl + ي)... so you decide which one to use.
如果您已经删除了所需命令的默认绑定/命令,您可以添加它而无需键绑定,如下所示:
if you already removed the default binding/command for the wanted command mentioned here you can add it without its key binding like this:
editor.commands.addCommand({
name: "removeline",
exec: function(editor) { editor.removeLines(); },
scrollIntoView: "cursor",
multiSelectAction: "forEachLine"
});
- 此命令的存在是必需的,因此
editor.execCommand("removeline")
将起作用 - it's neccsarry for this command to exist so
editor.execCommand("removeline")
will work
这篇关于如何防止浏览器覆盖我的ace编辑器的keyBindings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!