TinyMCE Insert Text Keyboard Shortcut

Demonstrates how to extend TinyMCE to insert some text by using a keyboard shortcut (the soft hyphen, in this case).

by Cy Rossignol

HTML

<script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
<h1>TinyMCE Example</h1>
<h2>Press <kbd>CTRL SHIFT -</kbd> to insert a word containing a soft hyphen.</h2>
<textarea id="editor"></textarea>
<p>
    This inserts a soft hyphen between "foo" and "bar". Use the browser's developer tools to inspect the text area's HTML markup to reveal the <code>&amp;shy;</code> HTML entity.
</p>
<p>
For more information, see <a href="https://stackoverflow.com/q/34491100/5209322">this Stack Overflow question</a>.

JavaScript

tinymce.init({
    selector: "#editor",
    init_instance_callback: function (editor) {
        editor.shortcuts.add("ctrl+shift+189", 'Insert Soft Hyphen', function () {
            editor.execCommand('mceInsertContent', false, 'foo');
            editor.execCommand('mceInsertContent', false, '\u00AD');
            editor.execCommand('mceInsertContent', false, 'bar...');
        })
    }
});