【问题标题】:ACE change key binding conditionallyACE 有条件地更改键绑定
【发布时间】:2014-08-13 12:46:31
【问题描述】:

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

为此,我写了这个:

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

不幸的是,我可以返回falsetrue,结果是一样的,它总是捕获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);

【问题讨论】:

    标签: javascript command ace-editor


    【解决方案1】:

    在当前版本中,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);
    

    【讨论】:

    • 非常有用。谢谢。
    猜你喜欢
    • 2013-04-12
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多