Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keydown event on Ace Editor

On this amazing editor (Ace: Code Editor), there's a method which I can get the on change event, is there a on keydown event? Or a hack I can simulate it?

like image 554
Mauricio Soares Avatar asked Jan 20 '15 11:01

Mauricio Soares


People also ask

How do I trigger Keydown event?

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.

What is the use of Keydown event?

The onkeydown attribute fires when the user is pressing a key (on the keyboard).


2 Answers

There is no keydown event, you can add keydown event listener on textarea returned by editor.textInput.getElement(), but the better way is to use editor.commands.addCommand

editor.commands.addCommand({
    name: "...",
    exec: function() {},
    bindKey: {mac: "cmd-f", win: "ctrl-f"}
})

or editor.keyBinding.addKeyboardHandler

like image 156
a user Avatar answered Oct 26 '22 09:10

a user


I can't find it in the documentation, but in this discussion, I found out about the editor.commands.on('afterExec', ...) API:

editor.commands.on('afterExec', eventData => {
    if (eventData.command.name === 'insertstring') {
        console.log('User typed a character: ' + eventData.args);
    }
});

afterExec fires on every command in the editor. Commands include actions like typed text, appearance of completion popups on ctrl+space, etc...

It's not a direct analog of keydown event, but it is the thing I was googling for when I came here, so hopefully you'll find it useful.

like image 26
Klesun Avatar answered Oct 26 '22 09:10

Klesun