在 vscode 扩展中显示信息消息时将键分配给默认按钮选项?

Assign key to default button option when shown information message in vscode extension?

我正在 VSCode 中创建一个扩展程序,当您在 vscode.window.showInformationMessage("Want to close?", "Yes", "No) 中单击“是”选项时,它将关闭所有打开的文本文档。

按“是”按钮可以正常工作,但我希望键盘上的“Enter”键也能正常工作?注册命令时如何监听击键?可能吗?

这是我现在的代码:

  context.subscriptions.push(
    vscode.commands.registerCommand(
      "close-tabs",
      async (inputValue?: string) => {
        const { activeTextEditor } = vscode.window;

        if (!activeTextEditor) {
          vscode.window.showInformationMessage("No active text editor");
          return;
        }

        const answer = await vscode.window.showInformationMessage("Want to close tabs?", "Yes", "No");

        if (answer === "Yes") {
          vscode.commands.executeCommand("openEditors.closeAll");
        }
        // ON_ENTER: I was thinking an if case here that checks if enter has been pressed and also executes the command above? 
      }
    )
  );

重申一下。如何在按“Enter”键时选择“是”选项?

由于您的 InformationMessage 框不是模态框,因此它出现时不会获得焦点。

恐怕您必须将其设置为模态 - 在您的情况下这可能是可以接受的 - 以获得您想要的行为:

const answer = await vscode.window.showInformationMessage("Want to close tabs?", { modal: true }, "Yes", "No");

然后消息框的第一个按钮自动获得焦点,Enter 将触发它。