簡體   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