Event handled in Key Handler is then being subsequently handled by Ace

42 Views Asked by At

I have a KeyHandler which defines a new behavior for alt up/down. After handling those keys, I call event.preventDefault and event.stopPropagation, and then return true.

For some reason that I can't figure out Ace is then handling the event after my handler. I tried setting the pos parameter of the keyhandler, which at 0 prevented it from working at all, and at 1 didn't change the behavior at all.

How do I properly tell Ace that a keyboard event is handled? Should I be using addCommand instead of KeyHandler? When should I use which, if so?

Update: code included.

aceEdit.keyBinding.addKeyboardHandler(::keyHandler)

private fun keyHandler(data: String, hash: String, keyString: String, keyCode: Int, event: KeyboardEvent?): Boolean {
    if (event.altKey && event.keyCode == 36) { //home
        this.aceEditor!!.moveCursorTo(0, 0)
    } else if (event.altKey && event.keyCode == 35) { // end
        this.aceEditor!!.moveCursorTo(aceEditor!!.getValue().split("\n").size - 1, 0)
    } else if (event.altKey && event.keyCode == 38) { // alt up
        moveNextOrPrev(false)
        event.preventDefault()
        event.stopPropagation()
1

There are 1 best solutions below

1
a user On

keyboardHandler is not supposed to make any changes to the editor, it is supposed to return an object with either a name of an existing command, or a command object

{ 
    command: string | {exec: function, ...}
}

This allows the editor to record macros, detect when to merge undo stack items, properly handle multiple selections, etc.

However if you can't return commands and prefer to handle key directly in keyboardHandler, you can return a special object to indicate that editor should stop calling other keyboard handlers

  return {command: "null"};

see keybinding.js#L107C1-L108C26 for details how it is implemented.

But judging from the snippet you have provided, you don't even need to implement keyboardHandler, just adding some commands or even changing keybindings of existing commands would suffice.