CKEditor keystroke interception

Is it possible to intercept the CKEditor key press (tab key) and replace the default behavior? I want the tab key to insert a div with a margin.

+2


a source to share


3 answers


this.editorInstance.on( 'tab', function(evt){

    evt.editor.insertHtml('span style="margin-left: 40px;">&nbsp;</span>');

    evt.cancel();
    return false;
})

      



+2


a source


I am using version 4.4.7. At least here you can change the behavior of pressing the TAB key simply by editing config.js

. With these TAB and SHIFT + TAB pointers:



config.keystrokes =
[
    [ 09, 'indent' ],
    [ CKEDITOR.SHIFT + 09, 'outdent' ]
];

      

+1


a source


I solved it in a slightly different way. Instead of inserting a fixed width range, I wanted the tabs to line up across all lines. So I insert a tab character (& # 0 9) with pre formatting. I also had difficulties with insertHtml () and had to use a combination of createFromHtml () and insertElement ().

Here's my solution:

// my editor id is 'summary'
CKEDITOR.replace('summary', { ... });

var editor = CKEDITOR.instances.summary; 
editor.on('key', function(ev) {
    if (ev.data.keyCode == 9) { // TAB
        var tabHtml = '<span style="white-space:pre">&#09;</span>';
        var tabElement = CKEDITOR.dom.element.createFromHtml(tabHtml, editor.document);
        editor.insertElement(tabElement);
        ev.cancel();
    }
});

      

0


a source







All Articles