Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

Functionality of SetKeyboardState and keybd_event

I currently have a requirement that means I need to be able to read scan codes from a keyboard AND send scan codes to the keyboard.  In the same way as you would control the CAPS LOCK, SCROLL LOCK etc.  My research to date is fairly standard stuff, I’ve looked at SetKeyboardState, keybd_eventand MapVirtualKey functions and am able to control said LEDs accordingly.

However, my question is as follows; does the mapping and setting other keyboard scan codes send a message to the keyboard or are only the standard led controls signals passed directly.  Could I send an unassigned scan code via keybd_event for example and have a signal reach the keyboard?

I hope that makes sense, thanks in advance for any help received!
1 Solution
Mapping and setting keyboard scan codes does not affect the phsyical keyboard. No messages are normally sent down the wire by any Windows API calls unless you do something that would alter the Led status.

keybd_event simply injects a key stroke into the windows system queue. If you change the caps lock status, the keyboard driver will transmit a command to the keyboard to alter the led status.

In fact, the scancodes (the actual bytes transmitted down the wire) from the hardware are completely different from the scancodes that you get in Windows. The actual data from the keyboard is translated into the windows scan code by the keyboard contoller (usually an 8042 microcontroller chip) and there are several different scan code sets that a typical keyboard can support. The 8042 controller will send a command to the keyboard to request a particular set.

Any additional features on the keyboard, such as the LED lights are controlled by sending commands up the wire to the keyboard.

You can send bytes to the keyboard by writing to the 8042 controller port (normally port 0x60 in DOS) but what the commands do can depend on the make and model of your keyboard. I am not sure if you could use this techinque in Windows without writing a kernel mode driver. All PS/2 compatible keyboards will respond to a standad set of commands (for altering the led status, selecting the scan code set, etc), which the Windows keyboard driver uses.

If you want to build a custom keyboard and communicate with it via a standard keyboard controller, your best approach would be to write a replacement keyboard driver that has additional functionality that would allow you to send data to the keyboard.
BLAZEPOINTAuthor Commented:
Many thanks for the prompt feedback.   I had a horrible feeling this was the case. I was really hoping that I could get away without going through the bespoke driver route - such is life I guess.  So now I have to write a keyboard driver......hmm.....no pressure then! :)

The points are yours for the sanity check and confirming my view of reality!



Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now