编程语言
首页 > 编程语言> > javascript-ACE更改条件绑定键

javascript-ACE更改条件绑定键

作者:互联网

如果在显示自定义弹出窗口时用户按下向下键,则希望此向下事件从编辑器中取消并手动处理.
但是,如果禁用了弹出窗口,则“向下”键应照常执行.

为此,我这样写:

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
});

不幸的是,我可以返回false或true,结果是相同的,它总是捕获down事件,这很烦人.我该如何预防?

我已经尝试了以下方法:

>将密钥绑定添加到DOM.但是之后,互动总是会发生(即我无法捕捉到).
>返回false或true为suggested for common events,但这在这里不起作用.

编辑

@a用户的解决方案效果很好.
代替上面的命令,我写道:

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);

解决方法:

在当前版本中,ace仅对每个键保留一个命令,因此您的addCommand调用将删除向下的默认绑定.

您可以添加新的键盘处理程序,类似于自动补全功能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);

标签:ace-editor,command,javascript
来源: https://codeday.me/bug/20191029/1959827.html