Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom keybinding example #3848

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions website/src/website/data/playground-samples/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
id: "interacting-with-the-editor-adding-an-action-to-an-editor-instance",
path: "interacting-with-the-editor/adding-an-action-to-an-editor-instance",
},
{
chapter: "Interacting with the editor",
name: "Adding a keybinding to an existing command",
id: "interacting-with-the-editor-adding-an-keybinding-to-an-existing-command",
path: "interacting-with-the-editor/adding-an-keybinding-to-an-existing-command",
},
{
chapter: "Interacting with the editor",
name: "Revealing a position",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="container" style="height: 100%"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Explanation:
// First, we need to know the ID of the command.
// If you know the default key bindings, you can get the ID by following these steps.
// 1. open Visual Studio Code and navigate to "Preferences => Keyboard Shortcuts".
// 2. enter a shortcut key (e.g. Tab, Escape, Enter, etc.) in the search text input at the top.
// 3. Once the commands are filtered, you will need to find which command is the target command by ID and name.

// In this sample, the cursor can be moved with the Ctrl key and HJKL.
// To move the cursor to the left, we usually use the LeftArrow key, so we type `LeftArrow` in the search form.
// You will see a number of commands, but you will find the most likely one, `cursorLeft`.
// Note that the `When` column says `textInputFocus`.
// In a similar fashion, you will find the commands `cursorDown`, `cursorUp`, and `cursorRight`.

monaco.editor.addKeybindingRules([
{
keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyH,
command: "cursorLeft", // ID
when: "textInputFocus", // When
},
{
keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyJ,
command: "cursorDown",
when: "textInputFocus",
},
{
keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyK,
command: "cursorUp",
when: "textInputFocus",
},
{
keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyL,
command: "cursorRight",
when: "textInputFocus",
},
]);

monaco.editor.create(document.getElementById("container"), {
value: [
"",
"class Example {",
"\tprivate m:number;",
"",
"\tpublic met(): string {",
'\t\treturn "Hello world!";',
"\t}",
"}",
].join("\n"),
language: "typescript",
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Adding a Keybinding to an Existing Command"
}