Is there a way to determine if a Windows computer has a trackpad and a mouse connected?

StackOverflow https://stackoverflow.com/questions/22995397

  •  01-07-2023
  •  | 
  •  

Question

I'm looking for a way to determine if a computer running my game has a trackpad and a mouse connected to it. A typical example would be a laptop with an external mouse connected to it.

Is there a Windows API that will return the number of mice/pointing devices connected to the system?

I need to support Windows 7+.

EDIT

I'm keeping the accepted answer because it works and answers my question but I decided not to use it after discovering ManyMouse. It does what I want and allows me to use the same API on Mac, Windows and (hopefully, still need to test) Linux.

Was it helpful?

Solution

I happened to have a bit of code lying around that enumerates the mice attached to the system. If there's more than one, it's likely that one is a trackpad and the other one an external mouse, although I guess it might be a trackpad and one of those little sticks. It should be possible to use the same API to get enough information about each nominal mouse to make an intelligent guess; for example, the built-in mouse devices usually seem to be connected via PS/2 whereas external mice are almost always USB.

Anyway, hopefully this will help:

#include <windows.h>

#include <Hidsdi.h>
#include <SetupAPI.h>
#include <devguid.h>

#include <stdio.h>

#pragma comment(lib, "hid.lib")
#pragma comment(lib, "setupapi.lib")

int main(int argc, char ** argv)
{
    GUID hid_guid;
    GUID mouse_guid = GUID_DEVCLASS_MOUSE;
    HDEVINFO hdevinfo;
    SP_DEVICE_INTERFACE_DATA devinterface;
    SP_DEVINFO_DATA devinfo;
    BYTE devdetailbuffer[4096];
    PSP_DEVICE_INTERFACE_DETAIL_DATA devdetail;
    DWORD n;

    HidD_GetHidGuid(&hid_guid);

    hdevinfo = SetupDiGetClassDevs(&hid_guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

    if (hdevinfo == INVALID_HANDLE_VALUE)
    {
        printf("SetupDiGetClassDevs: %u\n", GetLastError());
        return 1;
    }

    for (n = 0;; n++)
    {
        devinterface.cbSize = sizeof(devinterface);
        if (!SetupDiEnumDeviceInterfaces(hdevinfo, NULL, &hid_guid, n, &devinterface))
        {
            printf("SetupDiEnumDeviceInterfaces: %u\n", GetLastError());
            break;
        }
        devdetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)devdetailbuffer;
        devdetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
        devinfo.cbSize = sizeof(devinfo);
        if (!SetupDiGetDeviceInterfaceDetail(hdevinfo, &devinterface, devdetail, sizeof(devdetailbuffer), NULL, &devinfo))
        {
            printf("SetupDiGetDeviceInterfaceDetail: %u\n", GetLastError());
            break;
        }
        if (IsEqualGUID(&devinfo.ClassGuid, &mouse_guid))
        {
            // This is a mouse
            printf("DevicePath: %ws\n", devdetail->DevicePath);
        }
    }
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top