简体   繁体   English

如何获取游戏控制器名称(Windows 10 / C ++)

[英]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. 我已经看到很多关于如何使用XInput读取游戏控制器输入的信息,但我真的想知道连接的控制器的名称。

游戏控制器

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? 如何在PC上找到连接控制器的名称,或者更具体地说是我正在读取XInput的控制器名称?

您可以通过调用joyGetDevCaps函数来执行此操作,该函数返回包含所连接控制器的所有信息(包括名称)的JOYCAPS结构。

You can use DirectInput to get the name of the device. 您可以使用DirectInput获取设备的名称。 You need to do that using a callback: 你需要使用回调来做到这一点:

pDirectInput->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumJoystickCallbackStatus, &joynum, DIEDFL_ATTACHEDONLY); 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: 然后你必须有点创造性:在Startup上使用回调检测所有设备并存储他们的名字/ GUID ...然后当设备热插拔时(你用XInputGetState检测到)寻找你不知道的设备知道了,使用之前回调的修改版本,类似于:

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. 请注意,如果您有多个xbox控制器,您将分别为每个控制器获得一个回调。

Implementation of IsXInputDevice can be found in the MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ee417014(v=vs.85).aspx 可以在MSDN中找到IsXInputDevice的实现: https ://msdn.microsoft.com/en-us/library/windows/desktop/ee417014(v = vs.85).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM