Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use C# to read handedness setting from Windows 8

Tags:

c#

windows

On Windows 8 I'm trying to determine what the handedness of the mouse is using C#. In other words, I'm trying to read this setting:

Control Panel\Hardware and Sound\Mouse -> Switch primary and secondary buttons.

I've tried using WMI but with no luck. The handedness property value is always null, whatever mouse I use.

SelectQuery selectQuery = new SelectQuery("Win32_PointingDevice");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);

foreach (var mouse in searcher.Get())
{
    foreach (var property in mouse.Properties)
    {
        Console.WriteLine("{0}: {1}", property.Name, property.Value);
    }
}

Are there any other ways to accomplish this task?

like image 309
Aetherix Avatar asked Oct 31 '25 12:10

Aetherix


1 Answers

I found that GetSystemMetrics, exposed on user32.dll can be used to return the swapped mouse button data you seek. Here are some references and a quick console app thrown together for testing. The 3rd link contains some more 'official' examples of how to use GetSystemMetrics with C#.

GetSystemMetrics

Wrong way to detect swapped mouse

Reference for constants, etc

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{

    class Program
    {
        [DllImport("user32.dll")]
        public static extern Int32 GetSystemMetrics(Int32 bSwap);

        static void Main(string[] args)
        {
            //normally you would make this as a constant:
            int SM_SWAPBUTTON = 23;

            int isLeftHanded = GetSystemMetrics(SM_SWAPBUTTON);
            //0 means not swapped, 1 means swapped (left handedness?)

        }
    }
}
like image 85
DaFi4 Avatar answered Nov 02 '25 03:11

DaFi4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!