Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Game Controller name (Windows 10 / C++)

I've seen lots of info on how to read game controller input using XInput but I really want to know the name of the controller that is connected.

Game Controller

How can I find out the name of connected controllers on a PC or more specifically the name of the controller I am reading XInput from?

like image 891
PompeyPaul Avatar asked Feb 08 '16 20:02

PompeyPaul


People also ask

How do I find the name of my controller?

You can do this by calling the joyGetDevCaps function which returns a JOYCAPS structure containing all information (including name) of the connected controller.

How do I change the controller name in Windows 10?

Click on Hardware & Sound. Select Devices & Printers. On the new window, right-click on the paired device and select Properties. Click on the Bluetooth tab and edit the name of your paired device.

How do you rename a controller on PC?

In the Registry Editor, navigate to HKEY_CURRENT_USER\System\CurrentControlSet\Control\MediaProperties\PrivateProperties\Joystick\OEM\ , click the individual folders and you will see OEMName . Double-click that and you can rename each controller.


2 Answers

You can do this by calling the joyGetDevCaps function which returns a JOYCAPS structure containing all information (including name) of the connected controller.

like image 59
PompeyPaul Avatar answered Sep 28 '22 07:09

PompeyPaul


You can use DirectInput to get the name of the device. You need to do that using a callback:

pDirectInput->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumJoystickCallbackStatus, &joynum, DIEDFL_ATTACHEDONLY);

Then you have to be a bit creative: on Startup detect all devices using the callback and store their name/GUID... and then when a device is hot-plugged (which you detect with XInputGetState) look for the device which you don't know about yet, with a modified version of that earlier callback, something similar to this:

BOOL CALLBACK EnumJoystickCallbackStatus(LPCDIDEVICEINSTANCE pdevinst, LPVOID pref)
{
    DWORD devtype = GET_DIDEVICE_TYPE(pdevinst->dwDevType);
    DWORD subtype = GET_DIDEVICE_SUBTYPE(pdevinst->dwDevType);

    if (devtype == DI8DEVTYPE_KEYBOARD || (devtype == DI8DEVTYPE_SUPPLEMENTAL && subtype == DI8DEVTYPESUPPLEMENTAL_UNKNOWN)) {
        return DIENUM_CONTINUE;
    }

    ULONG* pjoynum = reinterpret_cast<ULONG*>(pref);
    if (IsXInputDevice(&pdevinst->guidProduct)) {
        // loop through your known devices and see if this GUI already exists
        // we are looking for one which we don't know about yet.
        if (!found) { 
            // store GUI / Name / ... in some global controllers-array
            return DIENUM_STOP;    // done
        }
    }
    DEBUG_INFO(Debug::XDF_General, "continue");
    return DIENUM_CONTINUE;
}

Note that if you have multiple xbox-controllers, you'll get a callback for each one separately.

Implementation of IsXInputDevice can be found in the MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ee417014(v=vs.85).aspx

like image 43
kalmiya Avatar answered Sep 28 '22 07:09

kalmiya