2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
macraee
  • 75
  • 1
  • 2
  • 6
  • Re *"it is not possible to detect a keyboard plugged into a PS/2 port after boot"*: It is very much possible, at least from a physical point of view (the physical way to doing it may or may not be restricted). When sending commands to the keyboard (for instance, [setting the LEDs](https://pmortensen.eu/world2/2022/08/29/controlling-leds-on-ps-2-keyboards/)), it will respond with an acknowledge byte for every command send. – Peter Mortensen Mar 21 '23 at 18:39
  • There is also acknowledgement on the bit level: In the sending sequence, the keyboard will start producing clock pulses (if not, it is not connected or otherwise inoperable). Setting the LEDs is somewhat destructive (it changes the state of the keyboard), but there are also commands that don't change the state, just query for information from the keyboard. – Peter Mortensen Mar 21 '23 at 18:40
  • Is it a BIOS restriction? – Peter Mortensen Mar 21 '23 at 18:40

2 Answers2

1

WMI seems to be 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 a PS/2 keyboard, so the status gives an error, but you should have an OK status if one is connected.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
puikos
  • 300
  • 1
  • 4
0

WMI should allow you to find it. There is a fantastic utility, WMI Code Creator, that allows to find and open different WMI classes and generate code in various formats, VBScript, C#, etc. which, when searching through the classes, makes life a lot easier.

As a general top level reference, I also found this helpful: WMI Tasks: Computer Hardware

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SGB
  • 616
  • 6
  • 10
  • Thanks for the link, although I went for puikos's solution in the end as it was exactly what I needed :) – macraee Dec 12 '11 at 15:13