Ctrl key press condition in WPF MouseLeftButtonDown event-handler

rem picture rem · Dec 7, 2009 · Viewed 27.4k times · Source

How I can add an additional condition for a certain keyboard key, to a WPF MouseLeftButtonDown event-handler?

For example Ctrl + key

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{         
    ...
}

Answer

Stanislav Kniazev picture Stanislav Kniazev · Dec 7, 2009
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");
    }
}