'TinyMCE KeyDown Events Not Working With jQuery

I am having a problem getting keydown events to work in TinyMCE. I have tried everything I can think of but no success. My set up is:

var tinyOptions={
    height: "400px",
    width: "1250px",
    script_url : 'https://cloud.tinymce.com/stable/tinymce.min.js',
    theme : "modern",
    plugins: 'print preview fullpage powerpaste searchreplace autolink   directionality advcode visualblocks visualchars fullscreen image link media template codesample table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount tinymcespellchecker a11ychecker imagetools mediaembed  linkchecker contextmenu colorpicker textpattern help',
    toolbar1: 'formatselect | bold italic strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify  | numlist bullist outdent indent  | removeformat',
    content_css: "/adex/mvc/public/css/content.css",
    forced_root_block: true
};
var editor=$("#memo").tinymce(tinyOptions);
editor.on('keyup', function(e) {
    //console.log('init event', e);
    console.log('Editor contents was modified. Contents: ' + editor.getContent());
});

I have also tried:

init_instance_callback: function (editor) {
    editor.on('keyup', function (e) {
      console.log('Element clicked:', e.target.nodeName);
    });
}

What I want to do is to allow tabs in the field. I am getting a Javascript error from TinyMCE. The error is:

TypeError: i.toLowerCase is not a function. (In 'i.toLowerCase()', 'i.toLowerCase' is undefined)

I don't know if that's causing the issue or not.



Solution 1:[1]

Here is a TinyMCE Fiddle that shows how to capture the keydown event:

http://fiddle.tinymce.com/drgaab

In the example I determine what key was pressed and if its the enter key I stop the keypress from happening. All other keys are processed as normal.

Note: Your first example failed as editor was a jQuery object and not a TinyMCE editor instance so editor.on is not valid on a jQuery object.

Solution 2:[2]

This is because forced_root_block is true, not detected p tag, if you put forced_root_block = false, it will work.

Reference:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Fromin
Solution 2 Tyler2P