BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 1.1+

Checking the Mouse Status

User interfaces sometimes adjust their operation depending upon the availability and type of pointing devices that are installed on the user's computer. Using Windows Management Instrumentation, this information can be detected and acted upon.

Windows Management Instrumentation

When you are developing an application's user interface, you may want to adjust the experience according to the type of pointing device that the user has installed. For example, if the user has a mouse you may present a classic interface with menus and small toolbar buttons. If the user has a touch-sensitive screen, you may use larger buttons and introduce gestures that are suitable for fingertip control, which is less accurate but may allow multi-touch input. You may also adjust the way in which your application is used if no pointing device is available.

Detecting the available devices allows you to provide default behaviour that varies according to the hardware, though you should generally give the user the option to change their preference. To detect which pointing devices are available and to check the status of those devices, you can use Windows Management Instrumentation (WMI) queries. In this article we'll look at one WMI class that provides key information about connected pointing devices.

To follow the sample code, you need access to the WMI classes. Ensure that your project includes a reference to the System.Management assembly and add the following using directive to your C# files.

using System.Management;

Win32_PointingDevice

To obtain the mouse status information we'll be querying the Win32_PointingDevice WMI class with a simple WMI Query Language (WQL) query. Although you can select which information to retrieve and filter the rows returned using WQL, we'll keep the query simple. We'll return every property using the asterisk symbol (*) and details of every pointing device. Remember that many computers include multiple input devices. Each of these will be represented by a single result row.

To retrieve the details for the devices, we'll use the following code:

ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_PointingDevice");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();

Reading the Pointing Device Status

Each result from the WQL query includes a large number of values that tell us about the mouse or similar device. We'll look at several items that you might wish to use in order to change the behaviour of your program to suit the user's hardware. The properties we are interested in are:

  • PointingType . This value tells us what type of pointing device has been detected. It returns a numeric value that can be decoded as follows. Unfortunately some devices do not provide the exact information, requiring options for "Other" and "Unknown".
    • 1. Other. A device of a type that cannot be represented by any of the values below.
    • 2. Unknown. The device type could not be identified. If you are changing the user interface based upon device type, you should use a default that represents the largest group of users. For example, if the software is designed for use on a tablet you might default to the touch screen options.
    • 3. Mouse. The device is a standard mouse.
    • 4. Track Ball. The device is a track ball.
    • 5. Track Point Pointing Stick. The device is a pointing stick, usually a small rubberised stick in the centre of a notebook's keyboard.
    • 6. Glide Point Touchpad. The device is a Glide Point touchpad.
    • 7. Touchpad. The device is a touchpad, controlled using the fingertips.
    • 8. Touch Screen. The pointing device is a touch screen. This may allow input from a single finger, or may support multi-touch gestures.
    • 9. Optical Mouse. The device is a mouse with an optical sensor.
  • NumberOfButtons. The NumberOfButtons value returns the number of buttons provided by the pointing device as an integer. Unfortunately, for many devices this returns zero.
  • Status. This string property returns a description of the status of the pointing device. Many values may be returned, potentially indicating problems. Generally, you will want to check if the value is "OK", indicating that the device is operating normally. Reading this property is a reliable way to obtain the status of the user's hardware but you may wish to combine its use with the StatusInfo property.
  • StatusInfo. The StatusInfo property returns a numeric value that provides information about the status of the pointing device. The following five values are possible, though for some devices this property will return null.
    • 1. Other. The status of the device is other than those listed below.
    • 2. Unknown. The status of the device cannot be determined.
    • 3. Enabled. The pointing device is enabled.
    • 4. Disabled. The pointing device is not currently enabled.
    • 5. Not applicable. Reading the status for the device is not applicable.

In addition to the above, we'll also read the Name property for each device. This will allow us to identify individual devices when there are several attached to the computer.

The following sample code can be added after the query to output the details of the available pointing devices. The comments show the example output when the entire program is executed on a notebook computer with a touchpad and a USB optical mouse. Note that in this case the number of buttons is reported as zero for both pointing devices, the StatusInfo property returns null and the pointing device type is unknown.

int deviceNumber = 1;
foreach (var device in devices)
{
    Console.WriteLine("POINTING DEVICE {0}", deviceNumber++);
    Console.WriteLine("Name: {0}", device["Name"]);
    Console.WriteLine("Status: {0}", device["Status"]);
    Console.WriteLine("StatusInfo: {0}", device["StatusInfo"]);
    Console.WriteLine("Type: {0}", device["PointingType"]);
    Console.WriteLine("Buttons: {0}\n", device["NumberOfButtons"]);
}

/* OUTPUT

POINTING DEVICE 1
Name: Synaptics PS/2 Port TouchPad
Status: OK
StatusInfo:
Type: 2
Buttons: 0

POINTING DEVICE 2
Name: HID-compliant mouse
Status: OK
StatusInfo:
Type: 2
Buttons: 0

*/
8 June 2012