addminlinux 发表于 2024-10-21 23:18:28

如何获取鼠标键盘型号

请问如何获取鼠标键盘型号一点思路都没有啊
有没有大哥指点一下

上等兵 发表于 2024-10-28 14:14:56

这应该取不到吧,牌子这么多

阿杰 发表于 2024-10-28 14:39:15

你用这个代码试下
#include <windows.h>
#include <iostream>

int main() {
    // 获取系统中鼠标设备信息
    RAWINPUTDEVICELIST list;
    UINT numDevices = GetRawInputDeviceList(NULL, &numDevices, sizeof(RAWINPUTDEVICELIST));
    if (numDevices > 0) {
      PRAWINPUTDEVICELIST devices = new RAWINPUTDEVICELIST;
      if (GetRawInputDeviceList(devices, &numDevices, sizeof(RAWINPUTDEVICELIST)) > 0) {
            for (UINT i = 0; i < numDevices; i++) {
                if (devices.dwType == RIM_TYPEMOUSE) {
                  // 获取鼠标设备名称长度
                  UINT size;
                  GetRawInputDeviceInfoW(devices.hDevice, RIDI_DEVICENAME, NULL, &size);
                  WCHAR* deviceName = new WCHAR;
                  if (GetRawInputDeviceInfoW(devices.hDevice, RIDI_DEVICENAME, deviceName, &size) > 0) {
                        std::wcout << "Mouse device name: " << deviceName << std::endl;
                  }
                  delete[] deviceName;
                }
            }
      }
      delete[] devices;
    }
    return 0;
}

阿杰 发表于 2024-10-28 14:42:40

#include <windows.h>
#include <iostream>
#include <string>
#include <vector>

// 结构体用于存储鼠标信息
struct MouseInfo {
    std::wstring name;
    std::wstring deviceId;// 可以看作类似序号的标识
};

// 比较函数用于排序鼠标设备列表
bool CompareMouseInfo(const MouseInfo& m1, const MouseInfo& m2) {
    return m1.deviceId < m2.deviceId;
}

// 从设备路径中提取设备名称
std::wstring ExtractMouseName(const std::wstring& devicePath) {
    size_t startIndex = devicePath.find_last_of(L'\\') + 1;
    size_t endIndex = devicePath.find_last_of(L'#');
    if (endIndex == std::wstring::npos) {
      endIndex = devicePath.length();
    }
    return devicePath.substr(startIndex, endIndex - startIndex);
}

int main() {
    // 存储鼠标信息的向量
    std::vector<MouseInfo> mice;

    // 获取系统中所有原始输入设备的数量
    UINT numDevices;
    GetRawInputDeviceList(NULL, &numDevices, sizeof(RAWINPUTDEVICELIST));
    if (numDevices > 0) {
      PRAWINPUTDEVICELIST deviceList = new RAWINPUTDEVICELIST;
      if (GetRawInputDeviceList(deviceList, &numDevices, sizeof(RAWINPUTDEVICELIST)) > 0) {
            for (UINT i = 0; i < numDevices; i++) {
                if (deviceList.dwType == RIM_TYPEMOUSE) {
                  UINT deviceNameSize;
                  GetRawInputDeviceInfoW(deviceList.hDevice, RIDI_DEVICENAME, NULL, &deviceNameSize);
                  WCHAR* deviceName = new WCHAR;
                  if (GetRawInputDeviceInfoW(deviceList.hDevice, RIDI_DEVICENAME, deviceName, &deviceNameSize) > 0) {
                        MouseInfo mouse;
                        mouse.name = ExtractMouseName(deviceName);
                        mouse.deviceId = deviceName;
                        mice.push_back(mouse);
                  }
                  delete[] deviceName;
                }
            }
      }
      delete[] deviceList;
    }

    // 对鼠标信息进行排序(可选,便于查看)
    std::sort(mice.begin(), mice.end(), CompareMouseInfo);

    // 输出鼠标信息
    for (const auto& mouse : mice) {
      std::wcout << "Mouse Name: " << mouse.name << std::endl;
      std::wcout << "Mouse Device ID (similar to serial number): " << mouse.deviceId << std::endl;
    }

    return 0;
}
页: [1]
查看完整版本: 如何获取鼠标键盘型号