Search code examples
usbhid

HID USB Keyboard


I am trying make an HID USB Device. I have searched about it and find that the output from the Keyboard has 8 bytes. First byte is a modifier, 2'nd byte is reserved and the remaining 6 bytes are key codes.I think in some cases like "prtsc" more than single byte is needed.I would like to know

1.Is the data output from the usb hid keyboard is always 8 bytes or not (will it vary depends on the types of key pressed).

2.Is there any other prefix or suffix data along with these 8 bytes to identify the start and end of new key press?

Regards , Rahul.


Solution

  • I think in some cases like "prtsc" more than single byte is needed.I would like to know

    No, the currently defined set of keyboard usages ranges from 0x04 to 0xE7 so they can all be indexed by a single byte.

    1.Is the data output from the usb hid keyboard is always 8 bytes or not (will it vary depends on the types of key pressed).

    It is a fixed length but it does not always have to be 8 bytes (unless your device needs to be recognised as a keyboard when booting a PC ... the CMOS boot firmware does not process HID report descriptors so the report buffer is a predetermined fixed 8 bytes). The minimum (that I have tested) is 3 bytes (1 modifier byte, 1 reserved byte and 1 key index byte) as defined by the HID report descriptor.

    2.Is there any other prefix or suffix data along with these 8 bytes to identify the start and end of new key press?

    No. The 6 bytes (after the first two bytes) represent the keys that are concurrently pressed (up to 6 in this case). If no keys are currently pressed then all 6 bytes should contain 0x00. Each byte is an index between LOGICAL_MINIMUM and LOGICAL_MAXIMUM that is mapped one-for-one to a range of keyboard usages that are (usually) defined by USAGE_MINIMUM and USAGE_MAXIMUM, or a list of individual USAGEs, or a combination of both. If, for example, the A and B keys are pressed then the buffer will contain the index for those two usages (in any order) and the remaining bytes should be 0x00. If A is subsequently released then the buffer should still contain the index for the B usage (with the remaining bytes set to 0x00). If you forget to signal key releases by clearing the buffer to zeros then you will most likely see the last changed key being repeated on your host device (usually a PC). The key repeat delay and rate is purely a host function.

    The Device Class Definition for HID 1.11 has more information in Appendix C.