Detecting PS/2 port state in C#

自闭症网瘾萝莉.ら 提交于 2019-12-24 04:12:33

问题


this is my first post!

I am attempting to simply detect whether there is a keyboard attached to the PS/2 port on my machine. The idea is that the computer will boot up, although if it detects a USB device or PS/2 keyboard, it reboots into an administrator mode.

I have handled the USB aspect, although I have had no luck in finding any documentation for the PS/2 port. Some posts have said it is not possible to detect a keyboard plugged into a PS/2 port after boot, although I simply wish to check whether there is one connected at boot time.

I am using C# for my program and therefore any solution in this language would be very helpful, although assistance in any language would be beneficial.

Many thanks

Euan


回答1:


WMI seems doing it :

ConnectionOptions opts = new ConnectionOptions();
ManagementScope scope = new ManagementScope(@"\\.\root\cimv2", opts);
string query = "select * from Win32_Keyboard";
System.Management.ObjectQuery oQuery = new ObjectQuery(query);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, oQuery);
ManagementObjectCollection recordSet = searcher.Get();
foreach (ManagementObject record in recordSet)
{
    Console.WriteLine("" + record.Properties["Description"].Value);
    Console.WriteLine("" + record.Properties["Layout"].Value);
    Console.WriteLine("" + record.Properties["DeviceID"].Value);
    Console.WriteLine("" + record.Properties["PNPDeviceID"].Value);
    Console.WriteLine("" + record.Properties["Status"].Value + "\n");
}

returns :

USB Human Interface Device 0000040C USB\VID_03F0&PID_0024\6&1A939CC4&0&1 USB\VID_03F0&PID_0024\6&1A939CC4&0&1 OK

Standard 101/102-Key or Microsoft Natural PS/2 Keyboard 0000040C ACPI\PNP0303\4&3432CBB0&0 ACPI\PNP0303\4&3432CBB0&0 Error

I don't have ps/2 keyboard so status gives error but you should have an OK status if one is connected




回答2:


WMI should allow you to find it. There is a fantastic utility WMI code creator http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=8572 that allows to find and open different WMI classes and generate code in various formats VB script, C# etc which when searching through the classes makes life a lot easier - I hope this helps!

As a general top level reference I found this helpful also http://msdn.microsoft.com/en-us/library/windows/desktop/aa394587(v=vs.85).aspx



来源:https://stackoverflow.com/questions/8475187/detecting-ps-2-port-state-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!