3

German keyboards have a Key called "Alt Gr". Its seems like windows treats it like pressing alt+ctrl.

But I have to find out (in c#) if the user pressed the key together with left ctrl.

Is there a way to distinguish between

  • User presses: "Alt Gr"
  • User presses: "Alt Gr" and "left ctrl" at the same time

System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.Key.LeftCtrl) is true in both cases.

System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.Key.RightAlt) is also true in both cases.

System.Windows.Input.Keyboard.Modifiers also returns "Alt | Control" in both cases.

Nick is tired
  • 6,860
  • 20
  • 39
  • 51
Generator
  • 41
  • 2
  • Alt Gr ist a very often used key on german keyboards. Its used to write "@", "€", "\", "~" and several other characters – Generator Mar 17 '17 at 17:40
  • @NickA out of interest, is the extended bit on the scancode the same for both? – stuartd Mar 17 '17 at 17:58
  • @NickA there's an extended bit on scancodes, both right and left alt are `0x38` but left has `0` as the extended bit, and right has `0xE0`, which is how you can tell them apart. There's lots of [legacy code in scancodes](https://blogs.msdn.microsoft.com/oldnewthing/20080211-00/?p=23503/)! – stuartd Mar 17 '17 at 18:19
  • @NickA cool I'll check it out – stuartd Mar 17 '17 at 18:23

1 Answers1

2

Due to my interest in this question I did a little bit of research using GLFW and C++:

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    if (action == GLFW_PRESS) {
        std::cout << std::hex << scancode << std::endl;
    }
}

Using this code I can see the following scancodes:

Left Alt:   0x0038
Right Alt:  0x0138
Left Ctrl:  0x001d
Right Ctrl: 0x011d

As you can see the modifier keys on the right hand side of the keyboard have an extended bit(thanks stuartd) and so using GLFW it is possible to distinguish between both alts and both controls.

However, using the .NET Framework in C# as you are I used the following callback:

private void func(object sender, KeyEventArgs e) {
    MessageBox.Show(e.KeyValue.ToString());
    MessageBox.Show(e.Alt.ToString());
    MessageBox.Show(e.Control.ToString());
    MessageBox.Show(e.Modifiers.ToString());
}

I also got the results of:

Left Alt:   18    True    False   Alt
Right Alt:  17    False   True    Control
Left Ctrl:  17    False   True    Control
Right Ctrl: 17    False   True    Control

Because of this I would be inclined to say that this is a limitation of the .NET Framework, at least in the version that I'm using(4.5.2) and that the answer to your question in this case is, no you cannot distinguish between Left Ctrl + Right Alt and Right Alt. I suggest you look at this answer and use the Win32 API instead.

Note also that Right Alt is Alt Gr.

Community
  • 1
  • 1
Nick is tired
  • 6,860
  • 20
  • 39
  • 51