递归火山软件开发平台
标题:
如何获取鼠标键盘型号
[打印本页]
作者:
addminlinux
时间:
2024-10-21 23:18
标题:
如何获取鼠标键盘型号
请问如何获取鼠标键盘型号
一点思路都没有啊
有没有大哥指点一下
作者:
上等兵
时间:
2024-10-28 14:14
这应该取不到吧,牌子这么多
作者:
阿杰
时间:
2024-10-28 14:39
你用这个代码试下
#include <windows.h>
#include <iostream>
int main() {
// 获取系统中鼠标设备信息
RAWINPUTDEVICELIST list;
UINT numDevices = GetRawInputDeviceList(NULL, &numDevices, sizeof(RAWINPUTDEVICELIST));
if (numDevices > 0) {
PRAWINPUTDEVICELIST devices = new RAWINPUTDEVICELIST[numDevices];
if (GetRawInputDeviceList(devices, &numDevices, sizeof(RAWINPUTDEVICELIST)) > 0) {
for (UINT i = 0; i < numDevices; i++) {
if (devices[i].dwType == RIM_TYPEMOUSE) {
// 获取鼠标设备名称长度
UINT size;
GetRawInputDeviceInfoW(devices[i].hDevice, RIDI_DEVICENAME, NULL, &size);
WCHAR* deviceName = new WCHAR[size];
if (GetRawInputDeviceInfoW(devices[i].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
#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[numDevices];
if (GetRawInputDeviceList(deviceList, &numDevices, sizeof(RAWINPUTDEVICELIST)) > 0) {
for (UINT i = 0; i < numDevices; i++) {
if (deviceList[i].dwType == RIM_TYPEMOUSE) {
UINT deviceNameSize;
GetRawInputDeviceInfoW(deviceList[i].hDevice, RIDI_DEVICENAME, NULL, &deviceNameSize);
WCHAR* deviceName = new WCHAR[deviceNameSize];
if (GetRawInputDeviceInfoW(deviceList[i].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;
}
复制代码
欢迎光临 递归火山软件开发平台 (https://bbs.voldp.com/)
Powered by Discuz! X3.4