当命令面板打开时禁用VS Code命令键绑定。

huangapple go评论63阅读模式
英文:

Disable VS Code command keybinding when Command Palette is open

问题

tl;dr: 如何在命令面板打开时禁用命令绑定?

我正在开发一个创建 Web 视图的扩展。当 Web 视图处于活动状态时,我有一些命令可以通过单个字母且没有修饰键来调用:

  "keybindings": [
    {
      "command": "visual-scxml-editor.toggleEventDisplay",
      "key": "e",
      "when": "visual-scxml-editor.visualEditorActive"
    },

这工作得很好,但是如果用户打开命令面板,他们就不能再键入这些字符

根据那个链接,我认为这是一个 bug。但是,我不能等待 bug 被修复。(而且,我可以想象一个无论何时命令面板打开并且具有键盘焦点都需要处理键绑定的问题,以至于行为不会被更改的问题。)

我没有看到任何条件上下文允许我在命令面板打开时禁用此绑定。是否有任何事件、回调或其他技巧,可以监听以发现命令面板是打开还是关闭的情况?

我唯一能想到的其他解决方法是对这些命令根本不使用键绑定,而是在 Web 视图中自己处理这些键盘事件:

document.body.addEventListener('keydown', evt => {
  switch (evt.code) {
    case 'KeyE':
      this.toggleEventDisplay();
      evt.preventDefault();    
    break;
  }   
});
英文:

tl;dr: How can I disable a command keybinding while the Command Palette is open?

I'm developing an extension that makes a webview. When the webview is active, I have some commands that can be invoked by a single letter with no modifiers:

  "keybindings": [
    {
      "command": "visual-scxml-editor.toggleEventDisplay",
      "key": "e",
      "when": "visual-scxml-editor.visualEditorActive"
    },

This works well, except that if the user opens the Command Palette they can no longer type those characters.

Per that link, I feel this is a bug. However, I cannot wait for the bug to be fixed. (And, I can imagine a turtles-all-the-way-down argument that requires keybindings to be processed even when the command palette is open and has keyboard focus, such that the behavior won't ever be changed.)

I don't see a when clause context that would allow me to disable this binding when the command palette is open. Are there any events or callbacks or other tricks I can listen for to discover when the command palette is open vs. closed?

The only other workaround I can think of is to not use keybindings at all for such commands, instead processing these keyboard events myself in the webview:

document.body.addEventListener('keydown', evt => {
  switch (evt.code) {
    case 'KeyE':
      this.toggleEventDisplay();
      evt.preventDefault();    
    break;
  }   
});

答案1

得分: 0

"listFocus"上下文不适用于命令面板,但"inputFocus"上下文适用。因此,答案就如下所示:

"keybindings": [
    {
      "command": "visual-scxml-editor.toggleEventDisplay",
      "key": "e",
      "when": "visual-scxml-editor.visualEditorActive && !inputFocus"
    },
英文:

While the listFocus context does not apply to the command palette, the inputFocus context does. So, the answer is as simple as:

"keybindings": [
    {
      "command": "visual-scxml-editor.toggleEventDisplay",
      "key": "e",
      "when": "visual-scxml-editor.visualEditorActive && !inputFocus"
    },

huangapple
  • 本文由 发表于 2023年2月27日 11:56:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定