4

I am coding in a vbs page and I need to know when AltGr is pressed. I must use the IE browser only (I am using version 1909).

Here is a sample of code reproducing the issue:

sub document_onkeydown()
     MsgBox "key=" & window.event.keyCode
     MsgBox "ctrl=" & window.event.ctrlKey
     MsgBox "alt=" & window.event.altKey
     MsgBox "altLeft=" & window.event.altLeft
end sub

The document_onkeydown sub is called each time I press a key.

My problem is that it is impossible to differentiate AltGr key from Ctrl key.

If I press Ctrl:

 window.event.keyCode = 17
 window.event.ctrlKey = true
 window.event.altKey = false
 window.event.altLeft = false

If I press AltGr:

window.event.keyCode = 17
window.event.ctrlKey = true
window.event.altKey = false
window.event.altLeft = false

As you can see, the values are the same. How to differentiate between the two keys pressed?

I could see that window.event.altRight is not recognized so I can't use that.

I am using a FRA CAFR (french Canada) keyboard.

Dada
  • 6,313
  • 7
  • 24
  • 43

1 Answers1

1

TL;DR
This issue is caused by a combination of your Operating System (OS) and the Application you are using. IE is simply treating AltGr as Ctrl (or Ctrl + Alt) and there is nothing you can do to change that. IE and many other applications are developed that way.

Full Answer
The key your computer detects as being pressed is a combination of your OS and Application. It sounds like your using some version of Windows as your OS?

Windows will properly detect AltGr as long as its a supported keyboard and configured correctly. Its a small possibility, but you might want to check your settings to make sure your not treating your FRA CAFR (french Canada) keyboard as an English keyboard or something else.

Most likely your running into one or more of the following issues:

  1. Windows has a quirk where it often detects AltGr only when the Right Alt (AltGr) is pressed.
  2. Its up to the application to report keystrokes to you. Applications developers often have a bad habit of treating AltGr as Ctrl or Ctrl + Alt. Since you have to use IE, I suspect the version of IE your using (and possible all IE versions/ Edge) treat AltGr as Ctrl.

Sources
On SuperUser this is a similar question and answer: In a windows keyboard, is right ALT treated as AltGr? I copied the main point below:

But multi-platform apps (e.g. Eclipse IDE or some Firefox plugins) ignore this difference so AltGr+E launches Ctrl+Alt+E so you have no way to type the €. But this is rather a bug which hopefully developers fix, although developers using only US keyboard layout typically don't care and resist fixing this (as seen in Eclipse bug tracker).

Not treating the AltGr key correctly is actually a big issue that many application developers overlook or decide not to address. Take a look at this issue opened with VScode for example: AltGr invokes Ctrl+Alt shortcuts #50341 VSCode is one of the most popular code editors on the market and supported by Microsoft, yet they still cant fix this issue.

If you follow the issue thread AltGr is being treated as Ctrl + Alt by Electron possibly in part because of Chromium. The user is using one application, VSCode, but the key press is being passed through 2 to 3 different abstraction layers (applications to keep things simple) and any one of them can choose to ignore AltGr and treat it as Ctrl.

Workaround
If you can and are willing to use additional software SuperUser has a question and answer that can help you: Can I make Ctrl+Alt NOT act like AltGr on Windows?

The answer is to much to copy or summarize here, but in case of link rot the solution is to use AutoHotKey to (attempt to) force the correct key through to the application.

Blizzardengle
  • 992
  • 1
  • 17
  • 30