Hook the Keydown Event in the TinyMCE Post Editor

I would like to hook the keydown event in the TinyMCE Editor on the edit post admin page. I managed to hook the HTML content editor using the following code:

jQuery('#content').keydown(function(){
    alert("keydown")
});

Here is my failed attempt at hooking the TinyMCE editor. The problem is that the editor hasn’t been init yet so the variable ed is undefined.

Read More
var ed = tinyMCE.getInstanceById('tinymce');
ed.onChange.add(function(ed, l) {
    alert("keydown");
});

Any help would be much appreciated!

Related posts

Leave a Reply

1 comment

  1. the TinyMCE Editor has its own keydown event handler and its hooked to a function on initiation so to do that you can create a tinymce plugin or use the wordpress initiation of it with tiny_mce_before_init hook like this:

    add_filter( 'tiny_mce_before_init', 'wpse24113_tiny_mce_before_init' );
    function wpse24113_tiny_mce_before_init( $initArray )
    {
        $initArray['setup'] = <<<JS
    [function(ed) {
        ed.onKeyDown.add(function(ed, e) {
            //your function goes here
            console.debug('Key down event: ' + e.keyCode);
        });
    
    }][0]
    JS;
        return $initArray;
    }