vscode icon indicating copy to clipboard operation
vscode copied to clipboard

How to detect shift shift key in VSCode?

Open kasecato opened this issue 10 years ago • 90 comments

  • 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);
}

kasecato avatar Apr 14 '16 22:04 kasecato

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).

alexdima avatar Apr 28 '16 11:04 alexdima

@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 avatar Sep 12 '16 09:09 jpoon

@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.

alexdima avatar Sep 12 '16 10:09 alexdima

(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:

  1. I have VSCodeVim installed which registers the type command
  2. I develop a new extension that attempts to also register the type command
  3. When I run my new extension, VSCode spits out an error saying that the type ID is already spoken for.

This seems to imply that only one extension at a time can register the type command?

jpoon avatar Sep 12 '16 18:09 jpoon

@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:

  • keydown
  • keypress
  • input
  • compositionstart
  • compositionupdate
  • compositionend
  • keyup
  • 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 a will generate a keydown, a keypress, an input and a keyup
  • in the US keyboard layout, pressing ctrl+a will generate a couple keydowns and a couple keyups
  • the rule of thumb mostly is that a keypress and a input is sent if the key combination produces a visible character... mostly... you see sometimes even Escape produces a keypress even if it doesn't produce any visible input.
  • in the Japanese/Hiragana IME, typing sennsei will generate many keydowns and keyups, a compositionstart, followed by multiple compositionupdates, sometime a few input events and finally a compositionend
  • the rule of thumb mostly is that you get visible produced characters in compositionupdate and in input events, meanwhile in keydown and keyup events you get key codes.
  • Key codes basically consist of a number, for example key code 36 is sent when pressing the Home key.
  • 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 the keydown events, meaning they all work with key codes. So when you bind ctrl+] it really means ctrl+VK_OEM_6, in other words ctrl+0xDD. When the browser sends my way a keydown with the e.keyCode === 0xDD and the e.ctrlKey === true I will invoke your command.
  • type, compositionstart and compositionend - 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.

alexdima avatar Sep 13 '16 09:09 alexdima

Indeed, would be nice if we could define this key binding in keybindings.json:

{ "key": "shift shift", "command": "workbench.action.quickOpen" }

hastebrot avatar Sep 26 '16 14:09 hastebrot

Any progress on this?

deecewan avatar Jun 04 '17 07:06 deecewan

@hastebrot it does not work on vscode 1.17.1

huy-lv avatar Oct 17 '17 09:10 huy-lv

Would like to see this.

MrChriZ avatar Oct 31 '17 14:10 MrChriZ

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?

jedlikowski avatar Dec 19 '17 10:12 jedlikowski

I'll just add my ditto to this.

gdbjohnson avatar Jan 21 '18 20:01 gdbjohnson

Any news on this ? (I'll wait relentlessly for this feature)

Varkal avatar Mar 14 '18 16:03 Varkal

I would love this as well!

MrBlaise avatar Mar 19 '18 08:03 MrBlaise

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).

image

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.

image

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.

FlorisWarmenhoven avatar Jul 11 '18 09:07 FlorisWarmenhoven

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

leanderr avatar Jul 26 '18 13:07 leanderr

I proposed something similar in #29407 which was closed. Did not see this issue. My +1.

gulshan avatar Oct 04 '18 03:10 gulshan

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. 😄

loopmode avatar Mar 01 '19 11:03 loopmode

Is there gonna be any update on this?

mpivchev avatar Mar 22 '19 22:03 mpivchev

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
                        }
                    }
                ]
            }
        }
    ]
}

perseus-fyi avatar Apr 16 '19 15:04 perseus-fyi

It would be great if this was supported on linux, ive missed search being double shift since moving from phpstorm.

leeIco3 avatar Apr 25 '19 07:04 leeIco3