Frage

I'm using CodeMirror in a mobile project (using PhoneGap) and a particular CodeMirror function will initiate when the " > " key is pressed.

In my application i have a quick button that allows the user to insert a " > " without having to find it on their softkeyboard. The problem is that CodeMirror detects the keystroke and not the character code >.

So when the user presses the button - the code does not initiate because it never detected the keystroke.

jQuery:

$('.html-inp').click(function() { 
    var txt = $(this).text().toString();
    editor.replaceSelection(txt, focus);
    editor.focus();
});

HTML:

<a href='#' data-role='button' id='a1' class='html-inp'>&#62;</a>

Does anyone know how I can trick the app into thinking the key was pressed and not just inserting a character code?

War es hilfreich?

Lösung

Edit: Tim Down is right, I seem to have overlooked the character code in light of the key code. The > character has a charCode of 62, which is also what's sent in the jQuery event object's which, keyCode, and charCode properties. I've made appropriate changes below:

Try the following HTML:

<a href='#' data-role='button' id='a1' class='html-inp' data-char='62'></a>

And the following jQuery:

$('.html-inp').click(function() {
    var e = jQuery.Event('keypress'),
        char = $(this).data('char');
    e.shiftKey = true;
    e.which = char;
    e.charCode = char;
    e.keyCode = char;
    editor.replaceSelection(String.fromCharCode(char), focus);
    editor.trigger(e);
});

This should have the effect of inserting the bracket into the editor, and also triggering the keypress event on the editor so that the rest of your code can act accordingly.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top