Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Keycode with CKeditor

I wanted to get the keyCode on keydown event in ckeditor. My code looks somewhat like this:

    editor.document.on( 'keydown', function(event) {
   var keycode= event.data.keyCode & ~(CKEDITOR.SHIFT | CKEDITOR.CTRL |CKEDITOR.ALT);
  alert('Key:'+keycode);
  
});

But it always alerts 0. Can't figure why is it so.?

Also I wanted to use event.preventDefault() from jQuery, but I can't figure out how to include jQuery library in my plugin.js file. Any suggestions?

like image 635
Wasim Wani Avatar asked Feb 21 '23 02:02

Wasim Wani


1 Answers

This is the correct code if you want to have fun:

CKEDITOR.instances.editor1.on( 'key', function (evt) {
    var kc = evt.data.keyCode,
        csa = ~(CKEDITOR.CTRL | CKEDITOR.SHIFT | CKEDITOR.ALT);

    console.log(kc, kc & csa);
    // kc & csa is what you need
});

Or this if you want to have shorter code:

CKEDITOR.instances.editor1.document.on( 'keydown', function (evt) {
    console.log(evt.data.getKeystroke(), evt.data.getKey());
    // getKey() is exactly what you want
});

Regarding preventing default - you don't need jQuery for this. CKEditor is so huge tool it has built-in DOM library. You can find preventDefault method in the evt.data from the second example.

like image 80
Reinmar Avatar answered Feb 28 '23 07:02

Reinmar