Overriding shortcut assignments in Tinymce

為{幸葍}努か 提交于 2019-12-01 10:39:31
rob c

I actually found a way to solve this problem from a another post on StackOverflow that gives me full control of all keystrokes and lets me easily trap any shortcuts I want. This successfully overrided the CTRL-R for refresh and the CTRL-O for Open. I made a few mods to the example code but a huge thanks to hims056 for the solution. his response along with some other helpful examaples can be found at:

Overriding Browser's Keyboard Shortcuts

Below is my version of the code:

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;
}

Open to other suggestions, improvements, comments, etc.

Regards,

Rob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!