How does the Button_Click event determine which mouse button was pressed? I tried to use the tips given in the Question question , but they did not help:

MouseEventArgs me = (MouseEventArgs) e; 

Failed to cast object type "System.Windows.RoutedEventArgs" to type "System.Windows.Input.MouseEventArgs". And when using this:

 if (SystemParameters.SwapButtons) { // тут код } else { // тут код } 

only the left mouse button worked, nothing happened when the right button was clicked. So how can we programmatically determine inside the OnClick event of a button, which of the mouse buttons was pressed?

    1 answer 1

    You need to subscribe the button to the PreviewMouseDown event, and remove the Click subscription because Click is tied to the system settings of the mouse (which mouse button is selected as a working, i.e., left-handed person or right-handed).

     private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { MessageBox.Show("Левая"); } if (e.RightButton == MouseButtonState.Pressed) { MessageBox.Show("Правая"); } }