'Catch Alt + other key shortcut

I have to catch user's input to send a shortcut to my WPF application.
I found on internet that I have to do something like this:
Catch when a key is pressed:

void keyDown(object sender, KeyEventArgs e)
{
    if (Keyboard.Modifiers.HasFlag(Modifiers.Shift))
        KeyPressed.SetShift(true);
    if (Key.Shift != e.Key && Key.LeftAlt != e.Key && ....) 
        KeyPressed.SetKey(e.Key);
}

where KeyPressed is a class with static boolean variables to catch if ⇧Shift, Alt or Ctrl and another key are pressed (with Alt and Ctrl instead of ⇧Shift in the if clause). The second if is to catch a key different from Alt, ⇧Shift, Control for the shortcut. For example, for the shortcut Alt+C we have:

  1. KeyPressed.Shift = false;
  2. KeyPressed.Alt = true;
  3. KeyPressed.Ctrl = false;
  4. KeyPressed.key = Key;

Where the last element is of type System.Window.Input.Key.
Catch when a key is released:

void keyUp(object sender, KeyEventArgs e)
{ 
    if (KeyPressed.getShift()) 
        this.textField.Text += "+Shift";
    if (KeyPressed.getKeyCode())
        this.textField.Text += "+" + KeyPressed.k.toString();

    KeyPressed.SetShift(false);
}

and here simply I append to a textField the input received, after that I set all keys to false to catch the next shortcut correctly.

This code works fine for all shortcuts like Ctrl+A, Ctrl+Alt+C, ⇧Shift+L, Alt, but when I press the shortcut like Alt+V, it catchs only Alt, not the other key.

How can I manage this? Is there a way to handle shortcuts in a better manner?



Solution 1:[1]

Store the Alt-modifier state in a local variable. I'm unsure of the reasons why but this made it work for me.

private bool _altModifierPressed = false;

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    _altModifierPressed = (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt));

    if (_altModifierPressed && Keyboard.IsKeyDown(Key.V))
    {
        // code to handle Alt + V
    }
}

UPDATE:

Alternatively, you could do something like this (no need for local variable)

if (((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) && Keyboard.IsKeyDown(Key.V))
{
    // code to handle Alt + V
}

But I noticed that with either approach (since the enum has the Flag attribute) any combination of keys including Alt & V will work. So both execute if I for example press Alt+G+V. Good luck.

Solution 2:[2]

If you want to use [Alt + A] in KeyboardHook in Office VSTO, this is how it's used.

if (IsKeyDown(Keys.Menu) &&
    keyData == Keys.A &&

    KeyWasAlreadyPressed == false &&
    !IsKeyDown(Keys.Controlkey) &&
    !IsKeyDown(Keys.ShiftKey))
{
    //Enter your code here
}

Note: Key.Menu denotes Alt Keys Also condition says, Alt+A (and do not invoke when control or shift key is pressed in addition to Alt + A)

Solution 3:[3]

switch (e.Key)
{
    case Key.System:
        if (((KeyboardEventArgs)e).KeyboardDevice.Modifiers == ModifierKeys.Alt)
        {
            if (e.SystemKey == Key.Left)
                moiveVideoPsition(-30);
            else if (e.SystemKey == Key.Right)
                moiveVideoPsition(30);
        }
        break;

This work well for me

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrii Omelchenko
Solution 2
Solution 3 unknown6656