Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trigger keyboard events in CodeMirror?

I'm using a different textfield as a proxy for CodeMirror. I want to use features such as closebrackets.js which are activated via keyboard events such as keydown, keypress, and keyup. I've tried several different approaches to trigger these events, none of which have caused CodeMirror to receive anything:

kc = 219
e = $.Event 'keydown', { which: kc }
$( myCodeMirror.getInputField() ).trigger e

Doesn't work. No events are fired.

cmIF = $( myCodeMirror.getInputField() )

textArea = $('<textarea></textArea>')
$('body').append textArea
textArea.keydown (e) ->
    cmIF.focus()
    return

kc = 219
e = $.Event 'keydown', { which: kc }
textArea.trigger e

Trying to forward events from a different text area. Doesn't work. CM doesn't events don't get triggered.

$( myCodeMirror.getWrapperElement() ).children().each (index) ->
    $(this).trigger e
    return

Trying to trigger the event on every child of CMs wrapper. Doesn't work. No CM events fired.

What am I doing wrong here? How can I trigger keyboard events on a CodeMirror instance?

like image 509
zakdances Avatar asked Oct 15 '13 22:10

zakdances


People also ask

How do I add an event on my keyboard?

To record a keypress event in JavaScript, use the code below: // Add event listener on keypress document. addEventListener('keypress', (event) => { var name = event. key; var code = event.

Which event is generated when keyboard key is pressed?

The keydown event is fired when a key is pressed. Unlike the deprecated keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.

How do I use CodeMirror in textarea?

This could be used to, for example, replace a textarea with a real editor: var myCodeMirror = CodeMirror(function(elt) { myTextArea. parentNode. replaceChild(elt, myTextArea); }, {value: myTextArea.

How do I setup a code mirror?

Download CodeMirror files. Download jQuery file. Inside the codemirror project folder create subfolders and name them js, css and plugin. The js folder will hold all the javascript files.


2 Answers

I am not sure if I understand you 100% but currently I define keyboard events when I am defining my configuration options for the codemirror instance.

var cmInstance = CodeMirror(target, {
    value: myTextArea.value,
    //other options here perhaps

    //defining some keyboard shortcuts
    extraKeys: {
        "Ctrl-J": "toMatchingTag",
        "Ctrl-S": function(cm) {
            saveCode(cm); //function called when 'ctrl+s' is used when instance is in focus
        },
        "F11": function(cm) {
            toggleFullscreen(cm,true); //function called for full screen mode 
        },
        "Esc": function(cm) {
            toggleFullscreen(cm,false); //function to escape full screen mode
        }
    }
});

Keep in mind these functions will only fire when the codemirror instance is in focus. Then you can do whatever you like in your functions perhaps even add new listeners to see what sort of event occurred(?).

I hope this helps.

like image 158
Jeffrey Jenkinson Avatar answered Oct 21 '22 01:10

Jeffrey Jenkinson


codemirror comes with an undocumented function triggerOnKeyDown with which you can trigger a key down on codemirror:

const ev = {
  type: 'keydown',
  keyCode: 40 // the keycode for the down arrow key, use any keycode here
}
cm.triggerOnKeyDown(ev)
like image 26
danday74 Avatar answered Oct 21 '22 01:10

danday74