Functionality of SetKeyboardState and keybd_event

Posted on 2006-05-22
Last Modified: 2013-11-12
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!
Question by:BLAZEPOINT
    LVL 3

    Accepted 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.

    Author Comment

    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 pressure then! :)

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



    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Pretext and Context: In this series, I wish to design the data model of systems in question. I will start by defining the scope and boundaries of the system, followed by subsystems involved, then finally the entities in those systems and subsystems…
    Introduction Many of the most common information processing tasks require sorting data sets.  For example, you may want to find the largest or smallest value in a collection.  Or you may want to order the data set in numeric or alphabetical order. …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now