递归火山软件开发平台

标题: 如何获取鼠标键盘型号 [打印本页]

作者: addminlinux    时间: 2024-10-21 23:18
标题: 如何获取鼠标键盘型号
请问如何获取鼠标键盘型号一点思路都没有啊
有没有大哥指点一下

作者: 上等兵    时间: 2024-10-28 14:14
这应该取不到吧,牌子这么多
作者: 阿杰    时间: 2024-10-28 14:39
你用这个代码试下
  1. #include <windows.h>
  2. #include <iostream>

  3. int main() {
  4.     // 获取系统中鼠标设备信息
  5.     RAWINPUTDEVICELIST list;
  6.     UINT numDevices = GetRawInputDeviceList(NULL, &numDevices, sizeof(RAWINPUTDEVICELIST));
  7.     if (numDevices > 0) {
  8.         PRAWINPUTDEVICELIST devices = new RAWINPUTDEVICELIST[numDevices];
  9.         if (GetRawInputDeviceList(devices, &numDevices, sizeof(RAWINPUTDEVICELIST)) > 0) {
  10.             for (UINT i = 0; i < numDevices; i++) {
  11.                 if (devices[i].dwType == RIM_TYPEMOUSE) {
  12.                     // 获取鼠标设备名称长度
  13.                     UINT size;
  14.                     GetRawInputDeviceInfoW(devices[i].hDevice, RIDI_DEVICENAME, NULL, &size);
  15.                     WCHAR* deviceName = new WCHAR[size];
  16.                     if (GetRawInputDeviceInfoW(devices[i].hDevice, RIDI_DEVICENAME, deviceName, &size) > 0) {
  17.                         std::wcout << "Mouse device name: " << deviceName << std::endl;
  18.                     }
  19.                     delete[] deviceName;
  20.                 }
  21.             }
  22.         }
  23.         delete[] devices;
  24.     }
  25.     return 0;
  26. }
复制代码

作者: 阿杰    时间: 2024-10-28 14:42
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>

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

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

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

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

  26.     // 获取系统中所有原始输入设备的数量
  27.     UINT numDevices;
  28.     GetRawInputDeviceList(NULL, &numDevices, sizeof(RAWINPUTDEVICELIST));
  29.     if (numDevices > 0) {
  30.         PRAWINPUTDEVICELIST deviceList = new RAWINPUTDEVICELIST[numDevices];
  31.         if (GetRawInputDeviceList(deviceList, &numDevices, sizeof(RAWINPUTDEVICELIST)) > 0) {
  32.             for (UINT i = 0; i < numDevices; i++) {
  33.                 if (deviceList[i].dwType == RIM_TYPEMOUSE) {
  34.                     UINT deviceNameSize;
  35.                     GetRawInputDeviceInfoW(deviceList[i].hDevice, RIDI_DEVICENAME, NULL, &deviceNameSize);
  36.                     WCHAR* deviceName = new WCHAR[deviceNameSize];
  37.                     if (GetRawInputDeviceInfoW(deviceList[i].hDevice, RIDI_DEVICENAME, deviceName, &deviceNameSize) > 0) {
  38.                         MouseInfo mouse;
  39.                         mouse.name = ExtractMouseName(deviceName);
  40.                         mouse.deviceId = deviceName;
  41.                         mice.push_back(mouse);
  42.                     }
  43.                     delete[] deviceName;
  44.                 }
  45.             }
  46.         }
  47.         delete[] deviceList;
  48.     }

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

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

  56.     return 0;
  57. }
复制代码






欢迎光临 递归火山软件开发平台 (https://bbs.voldp.com/) Powered by Discuz! X3.4