Skip to main content
VS Code’s keybinding system provides a flexible, context-aware way to bind keyboard shortcuts to commands. The platform handles cross-platform differences, chord sequences, and context-based activation.

Overview

The keybinding system consists of:
  • KeybindingsRegistry: Stores all default and extension-provided keybindings
  • IKeybindingService: Runtime service that resolves keybindings and dispatches keyboard events
  • ResolvedKeybinding: Platform-specific representation of keybindings

KeybindingsRegistry

The registry stores all built-in and extension-provided keybindings (but not user-defined ones):
Access the global registry:

Registering Keybindings

Basic Keybinding Rule

Keybinding Rule Interface

Keybinding Weights

Weights determine priority when multiple keybindings conflict:
Higher weights take precedence. User-defined keybindings always win.

Key Codes and Modifiers

Key Modifiers

Key Codes

Combining Keys

Combine modifiers and keys using the bitwise OR operator:

Platform-Specific Keybindings

Define different keybindings per platform:

Chord Keybindings

Chord keybindings are two-key sequences (like Ctrl+K Ctrl+C):
The user must press both sequences in order. VS Code enters “chord mode” after the first key.

Context-Aware Keybindings

Use when clauses to activate keybindings only in specific contexts:

IKeybindingService

The keybinding service resolves and dispatches keybindings at runtime:

Using the Keybinding Service

Looking Up Keybindings

Appending Keybindings to Labels

Dispatching Keyboard Events

Registering with Action2

The recommended way to register keybindings is through Action2:

Real-World Examples

Best Practices

Use KeyMod.CtrlCmd: Always use KeyMod.CtrlCmd instead of KeyMod.WinCtrl for better cross-platform behavior. It automatically maps to Cmd on Mac and Ctrl elsewhere.
Avoid conflicts: Check existing keybindings before registering new ones. Use appropriate weights and context expressions to avoid conflicts.
Provide alternatives: Consider providing secondary keybindings for discoverability and user preference.
Context awareness: Always use when clauses to scope keybindings to appropriate contexts. This prevents unintended activation.
Document chord sequences: Chord keybindings are powerful but less discoverable. Document them clearly in your extension’s README.

Testing Keybindings

See Also