繁体   English   中英

C#检测USB鼠标(Logitech鼠标)

[英]C# Detect USB Mouse (Logitech Mouse)

我在该网站上关注了其他一些有关在C#中检测鼠标状态的问题。 我的第一个理解是鼠标检测并不简单。 没有这样做的本机库,这对我来说似乎很奇怪。 但是我希望编写一个轻巧的C#控制台应用程序,最终将其转换为Windows服务,以检测是否存在我的USB Logitech鼠标。

当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

将代码更改为此解决了我的问题

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);
        }

暂无
暂无

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

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