首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >摩纳哥不会在某些情况下在编辑器上使用键盘击键。

摩纳哥不会在某些情况下在编辑器上使用键盘击键。
EN

Stack Overflow用户
提问于 2022-09-04 19:06:58
回答 1查看 87关注 0票数 1

我使用摩纳哥编辑器,我想定义,当用户点击Enter key,它将只适用于特定的条件。

这是我的代码:

代码语言:javascript
运行
复制
  renderEditor(el: HTMLElement) {
    this.editor = monaco.editor.create(el, {
       ...
       ...
    });
    this.editor.onKeyDown(this.keyDown);
  }

  keyDown(event: IKeyboardEvent): void {
    if (event.code == 'ArrowDown') {
      const shouldInsertNewLine = checkIfEnterShouldApplyOnEditorOrNot();
      if(! shouldInsertNewLine){
          // ?? code here to cancel the 'ArrowDown' hit ??
          // (other action happens instead)
      }
    }
  }

现在,如果checkIfShouldMoveCursorOnEditorOrNot返回false,那么arrowDown键不应该应用于编辑器(光标不应该移动到下一行)。

我怎样才能做到这一点?

我试过使用event.preventDefault(),但它不起作用。

EN

回答 1

Stack Overflow用户

发布于 2022-09-05 18:06:32

可以使用editor.addCommand

https://github.com/Microsoft/monaco-editor/issues/940在这里找到了答案,

参见示例:https://microsoft.github.io/monaco-editor/playground.html#interacting-with-the-editor-adding-a-command-to-an-editor-instance

代码语言:javascript
运行
复制
  renderEditor(el: HTMLElement) {
    this.editor = monaco.editor.create(el, {..});
    this.editor.onKeyDown(this.keyDown);

    this.shouldInsertNewLine = this.editor.createContextKey('shouldInsertNewLine', true);
    this.editor.addCommand(monaco.KeyCode.Enter, this.shouldInsertNewLine.reset, '!shouldInsertNewLine');
  }

  keyDown(event: IKeyboardEvent): void {
    if (event.code == 'ArrowDown') {
      const shouldInsertNewLine = checkIfEnterShouldApplyOnEditorOrNot();

      this.shouldInsertNewLine?.set(shouldInsertNewLine);

    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73602087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档