How to detect shift shift key in VSCode?
- VSCode Version: 1.0.0
- OS Version: OS X El Capitan 10.11.4
Steps to Reproduce:
I'm trying to bind quick open (ctrl+p) command to shift shift (shift x2) keys like IntelliJ IDEA in VS Code Extension (TypeScript).
But vscode.commands.registerCommand('type', (args) => args.text) can't detect shfit key.
How can I detect shift shift key?
// VSCode v0.10.12 or higher (not working <= v0.10.11)
'use strict';
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand('type', (args) => {
vscode.window.showInformationMessage(args.text);
vscode.commands.executeCommand('default:type', {
text: args.text
});
});
context.subscriptions.push(disposable);
}
That is a cool idea. Currently, the keybinding dispatching ignores only modifier keys (does not try to dispatch them unless they are accompanied by a non-modifier key).
@alexandrudima With the Vim extension, users can remap their vim keybindings. We're discovering that some key combinations don't get dispatched to the extension: https://github.com/VSCodeVim/Vim/issues/757. Particularly, Ctrl (ie. Ctrl+a). The only modifiers that seem to be dispatched are Alt and Shift.
Verified this by building a new extension with @k--kato's sample code above with VSCode 1.5.1
Related question: Can you have more than one extension register the type command?
@jpoon the keybindings.json use key codes. ctrl-a is correctly dispatched to extensions. e.g. here I have an extension that registers ctrl-t and it works correctly -- https://github.com/alexandrudima/vscode-lcov/blob/master/package.json#L109.
This issue is regarding registering a keybinding in the form of shift shift where only modifier keys are pressed.
Registering type is a take all or nothing. It is possible to unregister it via disposing the registration.
(this likely belongs in a separate issue, let me know if you want me to move it)
With registering type, the alt and shift modifiers are dispatched. However, if you want to bind to the ctrl modifier, you need to make a separate entry in the package.json like you suggested above. Why does this need to be a separate action? It'd be great if ctrl+key also goes through the type register command.
As for the more than one extension question, what I'm seeing is:
- I have VSCodeVim installed which registers the
typecommand - I develop a new extension that attempts to also register the
typecommand - When I run my new extension, VSCode spits out an error saying that the
typeID is already spoken for.
This seems to imply that only one extension at a time can register the type command?
@jpoon This has a very simple reason. It is driven by our platform (web browser). Pressing a key combination can end up generating the following DOM events:
keydownkeypressinputcompositionstartcompositionupdatecompositionendkeyup- maybe more, every couple of years, a few new ones pop up, or new properties are added to them.
It all depends on the keyboard layout you have installed and the key combinations you press. For example:
- in the US keyboard layout, pressing
awill generate akeydown, akeypress, aninputand akeyup - in the US keyboard layout, pressing
ctrl+awill generate a couplekeydowns and a couplekeyups - the rule of thumb mostly is that a
keypressand ainputis sent if the key combination produces a visible character... mostly... you see sometimes evenEscapeproduces akeypresseven if it doesn't produce any visible input. - in the Japanese/Hiragana IME, typing
sennseiwill generate manykeydowns andkeyups, acompositionstart, followed by multiplecompositionupdates, sometime a fewinputevents and finally acompositionend - the rule of thumb mostly is that you get visible produced characters in
compositionupdateand ininputevents, meanwhile inkeydownandkeyupevents you get key codes. - Key codes basically consist of a number, for example key code
36is sent when pressing theHomekey. - As far as I could tell, the numbers sort of follow the values defined at https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx but of course they vary across browsers and Operating Systems
- the key codes are sort-of keyboard layout independ, except when they're not, because w3c thought it would be something too difficult for websites to deal with, so they dropped in this beauty -- http://www.w3.org/TR/DOM-Level-3-Events/#h-optionally-fixed-virtual-key-codes -- which for exampel Chromium has decided to implement, but only on macs!
The relevant code is at:
- https://github.com/Microsoft/vscode/blob/master/src/vs/base/browser/keyboardEvent.ts#L12
- https://github.com/Microsoft/vscode/blob/master/src/vs/base/common/keyCodes.ts#L22
- https://github.com/Microsoft/vscode/blob/master/src/vs/platform/keybinding/browser/keybindingServiceImpl.ts#L39
- https://github.com/Microsoft/vscode/blob/master/src/vs/editor/common/controller/textAreaHandler.ts#L130
- https://github.com/Microsoft/vscode/blob/master/src/vs/editor/browser/controller/keyboardHandler.ts#L73
- some native stuff to make UI labels as close as possible to what the keyboard has likely printed on the physical key:
- https://github.com/Microsoft/vscode/blob/master/src/vs/workbench/services/keybinding/electron-browser/nativeKeymap.ts#L212
- https://github.com/Microsoft/node-native-keymap
I am certain there must be things I've missed in how this works, so I would love PRs improving things, while keeping in mind that everything must work in Windows, Linux, OSX, on Chrome, on IE9-11, on Edge and on Firefox.
I bubble this up in two conceptual different ways:
keybindings.json: These are all interpreted on thekeydownevents, meaning they all work with key codes. So when you bindctrl+]it really meansctrl+VK_OEM_6, in other wordsctrl+0xDD. When the browser sends my way akeydownwith thee.keyCode===0xDDand thee.ctrlKey === trueI will invoke your command.type,compositionstartandcompositionend- I have added these to our API for VIM emulation. They were not part of the API, but they are the only means to get visible input. If you are using these for something else than VIM, please don't. If necessary, I can throw in the registration of the command and check which extension registers these and have a whitelist. Most likely what you want needs to be a feature request that should be implemented through some other (possibly new) API.
Indeed, would be nice if we could define this key binding in keybindings.json:
{ "key": "shift shift", "command": "workbench.action.quickOpen" }
Any progress on this?
@hastebrot it does not work on vscode 1.17.1
Would like to see this.
Looks like Google Chrome now sends a keydown event when just the Shift key is pressed, with code 13. Should this be possible to implement now?
I'll just add my ditto to this.
Any news on this ? (I'll wait relentlessly for this feature)
I would love this as well!
I'm currently using a workaround, taking advantage of BetterTouchTools.
You can mimic this behaviour by adding a new shortcut, in BTT and selecting the 'Key Sequence' (inside the keyboard tab).

Then press 'Record New Key Sequence' and press shift > shift. This is what it will look like. I didn't touch any of the settings.

Save it, bind it to a key combination inside of VScode (I believe it's CMD + P by default). Et voilá! You have your desired functionality.
Of course, it would still be nicer if this was natively supported in VSCode without having to use third party software. Figured I'd share my solution anyway.
Workaround for Windows:
- Install Autohotkey
- Paste this in an Autohotkey Script
- Double Shift will result in CTRL+P being fired.
lastShift := 0 $Shift:: if ((A_TickCount - lastShift) <= 250) Send ^p else Send {Shift} lastShift := A_TickCount return
I proposed something similar in #29407 which was closed. Did not see this issue. My +1.
I came here from https://github.com/Microsoft/vscode/issues/68832 ("Double Tap Commands"), which was closed as duplicate of this.
Not sure whether it fits into this issue, but I would suggest a somewhat different new feature: "Multi Tap Commands".
I'd love to be able to bind this in keybindings.json:
{ "key": "ctrl+2*right", "command": "workbench.action.splitEditorRight" }
I guess 2*shift is just a special case of that.
Implementation could be straight-forward. Avoiding performance issues shouldn't be too hard. Not sure about the syntax change to support some modifier like /[0-9]\*/ or multi-platform support, but I guess it can all be robustly implemented.
I guess there should be a new setting, e.g. "Multi Tap Detection Timeout" with a sensible default value and possibly some restrictions, like a maximum allowed value of 500ms, or that it's only available with modifier keys (to avoid false detections when just typing..).
Then.. Some kind of global manager would have to detect multiple keydown events with the same keyCode within the timeout duration.
I guess there would be many useful possibilities. I personally just want to split my editor into a second view using the arrow keys, but why not deploy your project to the staging server using ctrl+shift+4*up?
Lot's of guessing all in all.. it might also just be a can of worms. 😄
Is there gonna be any update on this?
Another workaround for OSX using Karabiner.
Add this code to rules section in $HOME/.config/karabiner/karabiner.json
Code
{
"description": "double left shift to cmd+shift+p in VSCode",
"manipulators": [
{
"type": "basic",
"from": {
"key_code": "left_shift",
"modifiers": {
"optional": [
"any"
]
}
},
"to": [
{
"key_code": "p",
"modifiers": [
"left_command",
"left_shift"
]
}
],
"conditions": [
{
"type": "variable_if",
"name": "left_shift pressed",
"value": 1
},
{
"type": "frontmost_application_if",
"bundle_identifiers": [
"^com\\.microsoft\\.VSCode$"
]
}
]
},
{
"type": "basic",
"from": {
"key_code": "left_shift",
"modifiers": {
"optional": [
"any"
]
}
},
"to": [
{
"set_variable": {
"name": "left_shift pressed",
"value": 1
}
},
{
"key_code": "left_shift"
}
],
"to_delayed_action": {
"to_if_invoked": [
{
"set_variable": {
"name": "left_shift pressed",
"value": 0
}
}
],
"to_if_canceled": [
{
"set_variable": {
"name": "left_shift pressed",
"value": 0
}
}
]
}
}
]
}
It would be great if this was supported on linux, ive missed search being double shift since moving from phpstorm.