Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if control/command key is currently pressed in VS Extensions

In my extension, I would like to check in a certain function, if the control (or command) key is currently pressed. How is that possible? I couldn't find any field that exposes this information.

like image 629
Matthias Avatar asked May 20 '18 16:05

Matthias


People also ask

How do I enable Ctrl in VS Code?

go to File -> Preferences -> Keyboard shortcuts, or press Ctrl+K, then Ctrl+S, or edit %UserProfile%\AppData\Roaming\Code\User\keybindings.

How do I check my extensions in VS Code?

Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (Ctrl+Shift+X). This will show you a list of the most popular VS Code extensions on the VS Code Marketplace.

What is Ctrl D in VS Code?

Ctrl+D selects the word at the cursor, or the next occurrence of the current selection. Tip: You can also add more cursors with Ctrl+Shift+L, which will add a selection at each occurrence of the current selected text.

How do I change the shortcut key in VS Code?

All keyboard shortcuts in VS Code can be customized via the keybindings. json file. To configure keyboard shortcuts through the JSON file, open Keyboard Shortcuts editor and select the Open Keyboard Shortcuts (JSON) button on the right of the editor title bar. This will open your keybindings.


1 Answers

If you read the below article

https://code.visualstudio.com/docs/extensionAPI/patterns-and-principles

It says

Visual Studio Code has a very rich extensibility model and there are many ways to extend the tool. However, we do not provide direct access to the underlying UI DOM to extension writers. With VS Code, we’re continually trying to optimize use of the underlying web technologies to deliver an always available, highly responsive editor and we will continue to tune our use of the DOM as these technologies and our product evolve. To maintain performance and compatibility, we run extensions in their own host process and prevent direct access to the DOM. VS Code also includes a built-in set of UI components for common scenarios such as IntelliSense, so that these experiences are consistent across programming languages and extensions and extension developers do not need to build their own.

We realize that this approach may initially feel restrictive to extension developers. We’re always looking for ways to improve our extensibility model and expand the capabilities available to extensions. We look forward to hearing your feedback and ideas.

This means your extension code doesn't run the in editor window context at all. And you can't hack into the webview as the extension api doesn't provide it. So you need to open a Feature request with VScode team and ask them to expose either the last keyboard event or atleast the status of Shift, Ctrl and alt keys. Currently they just discard it and throw it away (if no editor is open) else they send it to the monaco editor before checking for any shortcut combination

Throw event away

like image 161
Tarun Lalwani Avatar answered Sep 23 '22 08:09

Tarun Lalwani