Search code examples
c#.netwpfwinformsmouseevent

When the mouse buttons are swapped in the OS, do the mouse click event handlers still work correctly with respect to which button was clicked?


Working in IT, I occasionally encounter users who are left-handed and prefer to use the mouse with their left hand, and often they switch the mouse buttons so the right button becomes the "primary" instead of the left button.

Here's what this configuration looks like in Windows:

enter image description here

How does this affect .NET applications (e.g. WPF, Winforms, etc.) which have code that relies on specific mouse buttons?

With the WPF PreviewMouseDown event, for example, MouseButtonEventArgs has a ChangedButton property which yields a MouseButton enum value. The description for Left is "The left mouse button." There is nothing said about "primary" or "secondary" buttons.

Similarly for the Winforms MouseClick event, there is MouseEventArgs with its Button property yielding a MouseButtons enum; the description for Left is simply "The left mouse button was pressed.", and again there is no mention of "standard" or "secondary" buttons.

In fact, all of the documentation I've seen related to the mouse and its buttons simply refer to the buttons by their location on a standard mouse setup. None of the documentation refers to "primary" or "secondary" mouse buttons.

Does the .NET framework adapt appropriately to changes in the OS' button configuration? Or will the following WPF and Winforms code break?

WPF:

void MainWindow_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left)
    {
        // do something where the "standard" mouse 
        // button must be the one that was clicked.
    }
}

Winforms:

private void MainForm_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        // do something where the "standard" mouse 
        // button must be the one that was clicked.
    }
}

Solution

  • By running a couple tests, I've determined that .NET is smart enough to recognize that the buttons have been swapped.

    For instance, in both of the following tests with WPF and Winforms, after switching the mouse buttons in Windows, I clicked the physical "right" mouse button, but the event responded by indicating that the "left" mouse button was used:

    WPF:

    enter image description here

    Winforms:

    enter image description here

    Therefore, despite the lack of documentation by Microsoft, Left actually refers to the "standard" mouse button and Right refers to the "secondary" mouse button.