Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACE change key binding conditionally

If the user presses the down key while a custom popup is displayed, I would like this down event to be cancelled from the editor and handled manually. However, if the popup is disactivated, the 'down' key should perform as usual.

For that, I wrote this:

editor.commands.addCommand({
  name: 'nav_down.',
  bindKey: {win: 'Down', mac: 'Down'},
  exec: function(editor) {
        if(myPopupIsOpen()) {
          // Do whatever I want with the popup.
          return false;
        } else {
          // just leave the key.
          return true;
        }
  readOnly: true
});

Unfortunately, I can return false or true, the result is the same, it always capture the down event, which is annoying. How can I prevent that?

I already tried the following:

  • Add a key binding to the DOM. But after that, the interaction always happen (i.e. I cannot capture it).
  • Return false or true as suggested for common events but this does not work here.

EDIT

The solution from @a user works very well. Instead of the above command, I wrote:

var HashHandler = require("ace/keyboard/hash_handler").HashHandler;
keyboardHandler = new HashHandler();
keyboardHandler.addCommand({
  name: 'nav_down.',
  bindKey: {win: 'Down', mac: 'Down'},
  exec: function(editor) {
        if(myPopupIsOpen()) {
          // Do whatever I want with the popup.
          return true; // CHANGE HERE ! true is when it capture it.
        } else {
          // just leave the key.
          return false; // CHANGE HERE ! false is when I don't capture it.
        }
  readOnly: true
});
editor.keyBinding.addKeyboardHandler(keyboardHandler);
like image 359
Mikaël Mayer Avatar asked Aug 06 '26 23:08

Mikaël Mayer


1 Answers

In the current version ace only keeps one command for each key so your addCommand call removes default binding for down.

You can add new keyboard handler similar to what autocompletion does https://github.com/ajaxorg/ace/blob/v1.1.3/lib/ace/autocomplete.js#L221

var HashHandler = require("ace/keyboard/hash_handler").HashHandler;
keyboardHandler = new HashHandler();
keyboardHandler.addCommand(/*add your command with return false*/)
editor.keyBinding.addKeyboardHandler(keyboardHandler);
like image 79
a user Avatar answered Aug 09 '26 13:08

a user



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!