我在这个网站上跟踪了几个关于在C#中检测鼠标状态的其他问题。我的第一个理解是鼠标检测并不简单。没有本地库可以这样做,这在我看来有点奇怪。但我想写一个小的重量轻的C#控制台应用程序,它最终将转换为一个窗口服务,以检测我的USB鼠标是否存在。
当我的鼠标出现在Windows 10中时,我一直试图找到禁用触控板的方法,而我所遵循的所有指南都不适合我,所以我决定为它构建自己的小程序。我不想使用nuget库来实现这一点,我想要一些小而简单的东西。
我有目前的代码块,但我只看到Elan设备,我相信这是我的触控板。
static async Task StartListenForMouseChangeAsync()
{
await Task.Factory.StartNew(()=> {
do
{
var mouse = new ManagementObjectSearcher("winmgmts:\\.\\root\\CIMV2");
mouse.Query = new ObjectQuery("SELECT * FROM Win32_PointingDevice");
var data = mouse.Get();
foreach (var obj in data)
{
foreach (var item in obj.Properties)
{
Console.WriteLine($"{item.Name} -- {item.Value}");
}
}
Thread.Sleep(5000);
Console.Clear();
} while (true);
}, TaskCreationOptions.LongRunning);
}样本输出:
Availability --
Caption -- HID-compliant mouse
ConfigManagerErrorCode -- 0
ConfigManagerUserConfig -- False
CreationClassName -- Win32_PointingDevice
Description -- HID-compliant mouse
DeviceID -- HID\VID_046D&PID_C231\2&AF07C4&0&0000
DeviceInterface -- 162
DoubleSpeedThreshold --
ErrorCleared --
ErrorDescription --
Handedness --
HardwareType -- HID-compliant mouse
InfFileName -- msmouse.inf
InfSection -- HID_Mouse_Inst.NT
InstallDate --
IsLocked --
LastErrorCode --
Manufacturer -- Microsoft
Name -- HID-compliant mouse
NumberOfButtons -- 0
PNPDeviceID -- HID\VID_046D&PID_C231\2&AF07C4&0&0000
PointingType -- 2
PowerManagementCapabilities --
PowerManagementSupported -- False
QuadSpeedThreshold --
Resolution --
SampleRate --
Status -- OK
StatusInfo --
Synch --
SystemCreationClassName -- Win32_ComputerSystem
SystemName -- MSI
Availability --
Caption -- ELAN Input Device
ConfigManagerErrorCode -- 0
ConfigManagerUserConfig -- False
CreationClassName -- Win32_PointingDevice
Description -- ELAN Input Device
DeviceID -- ACPI\ETD0306\4&1464EB1C&0
DeviceInterface -- 1
DoubleSpeedThreshold --
ErrorCleared --
ErrorDescription --
Handedness --
HardwareType -- ELAN Input Device
InfFileName -- oem22.inf
InfSection -- ETD_STD_Inst
InstallDate --
IsLocked --
LastErrorCode --
Manufacturer -- ELAN
Name -- ELAN Input Device
NumberOfButtons -- 0
PNPDeviceID -- ACPI\ETD0306\4&1464EB1C&0
PointingType -- 2
PowerManagementCapabilities --
PowerManagementSupported -- False
QuadSpeedThreshold --
Resolution --
SampleRate --
Status -- OK
StatusInfo --
Synch --
SystemCreationClassName -- Win32_ComputerSystem
SystemName -- MSI
Availability --
Caption -- USB Input Device
ConfigManagerErrorCode -- 0
ConfigManagerUserConfig -- False
CreationClassName -- Win32_PointingDevice
Description -- USB Input Device
DeviceID -- USB\VID_046D&PID_C24A&MI_00\6&2E5B0EB1&0&0000
DeviceInterface -- 162
DoubleSpeedThreshold --
ErrorCleared --
ErrorDescription --
Handedness --
HardwareType -- USB Input Device
InfFileName -- input.inf
InfSection -- HID_Inst.NT
InstallDate --
IsLocked --
LastErrorCode --
Manufacturer -- (Standard system devices)
Name -- USB Input Device
NumberOfButtons -- 0
PNPDeviceID -- USB\VID_046D&PID_C24A&MI_00\6&2E5B0EB1&0&0000
PointingType -- 2
PowerManagementCapabilities --
PowerManagementSupported -- False
QuadSpeedThreshold --
Resolution --
SampleRate --
Status -- OK
StatusInfo --
Synch --
SystemCreationClassName -- Win32_ComputerSystem
SystemName -- MSI发布于 2017-08-10 13:42:20
将代码更改为此解决了我的问题。
static async Task StartListenForMouseChangeAsync()
{
await Task.Factory.StartNew(()=> {
do
{
Console.Title = "Scanning";
var mouse = new ManagementObjectSearcher($"winmgmts:\\.\\root\\CIMV2");
mouse.Query = new ObjectQuery("SELECT * FROM Win32_PointingDevice");
var data = mouse.Get();
var states = new Boolean[data.Count];
var props = new List<ManagementBaseObject>();
foreach (var item in data)
{
props.Add(item);
}
for (int i = 0; i < data.Count; i++)
{
states[i] = props[i].Properties["DeviceID"].Value.ToString().StartsWith("USB");
}
var hasUsbMouse = states.Contains(true);
Console.Title = $"Mouse Status: {hasUsbMouse}";
Thread.Sleep(5000);
Console.Clear();
} while (true);
}, TaskCreationOptions.LongRunning);
}https://stackoverflow.com/questions/45614343
复制相似问题