views:

634

answers:

2

How I can add additional condition of certain keyboard key, say, Ctrl-key state to WPF MouseLeftButtonDown event-handler?

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {         
     ...
    }
+3  A: 
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
        if(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) {
            MessageBox.Show("Control key is down");
        } else {
            MessageBox.Show("Control key is up");
        }
    }
Stanislav Kniazev
Thanks so much!
rem
+1  A: 

If you want to detect modifiers only, you can also use:

if (Keyboard.Modifiers == ModifierKeys.Control) {}
if (Keyboard.Modifiers == ModifierKeys.Shift) {}

etc. More here.

742