Reputation: 145
I am creating an extension in VSCode that will close all open text documents when you click the option "Yes" in the vscode.window.showInformationMessage("Want to close?", "Yes", "No)
.
Pressing the "Yes" button will work fine but I want the "Enter" key on the keyboard to do the same? How can I listen for keystrokes when I register a command? Is it possible?
Here is my code right now:
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?
}
)
);
To reiterate. How can I choose the "Yes" option when pressing the "Enter" key?
Upvotes: 0
Views: 182
Reputation: 182791
Since your InformationMessage
box is not modal, it doesn't get focus when it appears.
I'm afraid you would have to make it modal - which in your case might be acceptable - to get the behaviour you want:
const answer = await vscode.window.showInformationMessage("Want to close tabs?", { modal: true }, "Yes", "No");
Then the message box's first button is automatically given focus and Enter will trigger it.
Upvotes: 1