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

AltGr invokes Ctrl+Alt shortcuts #50341

Closed
amcsi opened this issue May 23, 2018 · 12 comments
Closed

AltGr invokes Ctrl+Alt shortcuts #50341

amcsi opened this issue May 23, 2018 · 12 comments
Assignees
Labels
upstream Issue identified as 'upstream' component related (exists outside of VS Code)

Comments

@amcsi
Copy link

amcsi commented May 23, 2018

  • VSCode Version: 1.23.1
  • OS Version: Windows 10

Steps to Reproduce:

  1. Set keyboard to the Hungarian keyboard layout
  2. Make sure there is a keyboard shortcut set to Ctrl+Alt+B
  3. Press AltGr+B in the editor

Expected: a { is typed
Actual: The Ctrl+Alt+B shortcut is invoked

This issue is basically the same as #41225

Whoever closed that ticket quoted that Ctrl+Alt and AltGr should be equivalents based on this wikipedia article, but I digress.
It's true that even on Windows you can press Ctrl+Alt+<key> to get the AltGr+<key> equivalent character typed, however it should not be true the other way around.
If you read the wikipedia article, it says that the ability was there to make it possible for otherwise AltGr-only characters to be typed. It does not say though that AltGr should invoke the hotkeys that are for Ctrl+Alt. If you did, you make it impossible for people using foreign keywords to use Ctrl+Alt shortcut sequences. Windows most certainly does not emit Ctrl+Alt on press of AltGr!

For reference, in IntelliJ there is a custom option you can enable to make sure AltGr does not emit Ctrl+Alt events: https://youtrack.jetbrains.com/issue/IDEA-91975#comment=27-384169

@vscodebot vscodebot bot added editor editor-contrib Editor collection of extras labels May 23, 2018
@alexdima
Copy link
Member

@amcsi I agree with you, but this is not under our (VS Code) control and we cannot change this. We are built with Electron, which is built on top of Chromium, which follows w3c standards.

Here is where we read a keyboard event sent our way by Chromium. Observe the lack of doing anything with AltGr. We simply read the modifier flags as we receive the event :
https://github.com/Microsoft/vscode/blob/baf3b60651bbfac50d8297b07e71240232316de9/src/vs/base/browser/keyboardEvent.ts#L230-L233

When you press AltGr+B, the event coming our way contains the key code for B and the flags for altKey and ctrlKey are set to true. i.e. there is no way at our abstraction level to determine if this keypress came from AltGr+B or Ctrl+Alt+B.

@alexdima alexdima added upstream Issue identified as 'upstream' component related (exists outside of VS Code) and removed editor editor-contrib Editor collection of extras labels May 28, 2018
@amcsi
Copy link
Author

amcsi commented May 28, 2018

@alexandrudima I don't know that much about Chromium internals, but I would think the problem is with your abstraction layer.

On this website I can see that it is possible in Chrome to differentiate between if an alt key press was from the right side of the keyboard.

In the screenshot, the bottom two keyup events are me just pressing down AltGr; the keydowns higher up are left Ctrl+Alt.
True that Ctrl is always shown as pressed, but together with the knowledge that out of the Alt keys the right one is down, you could ignore that combined with the Ctrl key being pressed down for the purposes of modifier keys for shortcuts.

2018-05-28 2

@alexdima
Copy link
Member

@amcsi

I'm using an ISO keyboard with the German (Swiss) keyboard layout.

Here is me pressing AltGr+S on the physical keyboard:
image

Here is me pressing Ctrl+Alt+S on the physical keyboard:
image

Here is me pressing AltGr+S using the On-Screen Keyboard:
image

Here is me pressing Ctrl+Alt+S using the On-Screen Keyboard:
image


Our keybinding dispatching executes on keydown, specifically on the keydown at number 3 in all the screenshots above. Note how the row number 3 is identical in all cases, and how Chromium reports the modifier AltGraph in all 4 cases.

@amcsi
Copy link
Author

amcsi commented May 28, 2018

@alexandrudima I know for a fact that unfortunately AltGraph is not a reliable way to detect AltGr, evident also by this StackOverflow post: https://stackoverflow.com/questions/10657346/detect-alt-gr-alt-graph-modifier-on-key-press

What I wanted to bring to your attention though is the code column within the cyan UI Events; you can tell that an Alt is Right by it saying AltLeft... and good that you brought up the on-screen keyboard where apparently it's neither left nor right for AltGr.

This would probably require additional work to keep track of whether the left or right Alt was pressed down/released in the keydown/keyup events, because probably there's not enough information just looking at the keypress event with its .xyKey properties.

@alexdima
Copy link
Member

Currently, keypress dispatching is stateless. i.e. the event coming in from the browser is treated independent from other events (with the notable exception of chords -- but that has nothing to do with tracking modifiers). In other words, all the data and only the data of event number 3 above is used to dispatch Ctrl+Alt+S

Anyways, in my experience, tracking modifiers manually is extremely error prone. (i.e. by manual I mean looking for keydown and keyup events for the modifiers keys and trying to maintain an application level modifier state).

The reason is that surprisingly often, keydowns/keyups go missing and don't reach us.

For example, it is possible to press Ctrl when focused outside of VS Code, then bring focus (via mouse) to VS Code, and then press S and that should be handled as Ctrl+S. The same can happen if focus is moved out of VS Code when a modifier is pressed. Or, sometimes, people use remote desktoping software or other keyboard intercepting software which cause similarly, for events to be dropped.

From my limited Windows C++ programming from more than a decade ago, I believe the recommended way was to query if a key is pressed, rather than tracking that by yourself. There is even GetAsyncKeyState for avoiding unprocessed messages in the event queue. From https://msdn.microsoft.com/en-us/library/windows/desktop/gg153546(v=vs.85).aspx :

image

Anyways, the problem, IMHO, is that:

  • either the w3c spec is broken (or too limited)
  • or Chromium implements the spec incorrectly.

@amcsi
Copy link
Author

amcsi commented May 28, 2018

Alright, I see. Thanks for putting in the effort to write all this down though!

I can see that because of window focus change issues you definitely cannot rely on mousedown, and if Chromium truly makes it impossible to detect right alt specifically in the keypress event, then unless you made your own Chromium fork, this will be impossible. If that's something that you don't do here, then yes I find it unrealistic that this would ever get fixed here.

I rest my case.

@alexdima
Copy link
Member

Thank you, I really appreciated the discussion. If you have sufficient energy, please go ahead and bring it up at https://bugs.chromium.org/p/chromium/issues/list . I think being able to distinguish Ctrl+Alt+ from AltGr+ is something any number of web applications would want to do (including the Monaco Editor) and it would only improve the web ecosystem.

@kwinz
Copy link

kwinz commented Jun 21, 2018

Using US-Int keyboard layout. AltGr+S should make a German ß character. Instead I get the "run task" menu jumping at me. Very annoying. Appreciate not handling AltGr (right alt) the same as ctrl + alt

@kwinz
Copy link

kwinz commented Jun 21, 2018

https://stackoverflow.com/a/39412714/643011
In my test this code snipped differentiates reliably between AltGr and Ctrl + Alt.
Tested in Chrome 67 under Windows 10 both with physical and on-screen keyboard.
Please use this code?

@alexdima alexdima reopened this Jun 21, 2018
@amcsi
Copy link
Author

amcsi commented Jun 21, 2018

Indeed that works in my Chrome as well

@iphigenie
Copy link

just nudging on this one as I have just installed on a clean machine and having to go through the list of bindings to find all the ones which override accents is an unnecessary pain.

@alexdima
Copy link
Member

This has actually changed in Chromium, so AltGr+ no longer equivalates to Ctrl+Alt+. See #68003

@vscodebot vscodebot bot locked and limited conversation to collaborators Aug 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
upstream Issue identified as 'upstream' component related (exists outside of VS Code)
Projects
None yet
Development

No branches or pull requests

4 participants