实际上,我从 StackOverflow 上的另一篇文章中找到了解决此问题的方法,该方法使我可以完全控制所有击键并轻松捕获所需的任何快捷方式。这成功地覆盖了用于刷新的 CTRL-R 和用于打开的 CTRL-O。我对示例代码做了一些修改,但非常感谢 hiss056 的解决方案。他的回答以及其他一些有用的例子可以在以下位置找到:
覆盖浏览器的键盘快捷键
以下是我的代码版本:
tinymce.init({
selector: "textarea#elm1",
theme: "modern",
statusbar: false,
width: "100%",
height: "95%",
plugins: ["print preview"],
content_css: "css/content.css",
...
setup: function(ed)
{
ed.on("keyup", function(e) {
console.debug('Key up event: ' + e.keyCode);
overrideKeyboardEvent(e);
});
ed.on("keydown", function( e) {
console.debug('Key down event: ' + e.keyCode);
overrideKeyboardEvent(e);
});
<script type="text/javascript">
document.onkeydown = overrideKeyboardEvent;
document.onkeyup = overrideKeyboardEvent;
var keyIsDown = {};
function overrideKeyboardEvent(e){
var returnVal = true;
switch(e.type){
case "keydown":
if(!keyIsDown[e.keyCode]){
keyIsDown[e.keyCode] = true;
// check if they selected ctrl-r which will refresh the screen
if (keyIsDown[17])
{
switch (e.keyCode)
{
case 82: // CTRL-R refreshes the screen! Don't want to do that!
e.stopPropagation();
e.preventDefault();
returnVal = false; // false means don't propagate
break;
case 79: // CTRL-O by default opens an open File Dialog.
e.stopPropagation();
e.preventDefault();
returnVal = false; // false means don't propagate
// call openDocument
loadDocument(false);
break;
case 68: // CTRL-D by default opens up the Bookmark Editor in Chrome. We want to start a comment!
e.stopPropagation();
e.preventDefault();
returnVal = false; // false means don't propagate
createComment();
break;
}
}
}
break;
case "keyup":
delete(keyIsDown[e.keyCode]);
// do key up stuff here
break;
}
return returnVal;
}
接受其他建议、改进、评论等。
问候,
抢