1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

USB keyboard debounce and typematic repeat

Discussion in 'General Electronics Chat' started by jgharston, Nov 12, 2017.

  1. jgharston

    jgharston New Member

    Joined:
    Dec 15, 2010
    Messages:
    14
    Likes:
    1
    I've been doing some experiments interfacing to a USB keyboard, but am getting unexpected results from continued keypresses.

    Documentation says that it is the responsibility of the host to do typematic key repeats. When reading device data from a keyboard Human Interface Device, you get an 8-byte packet with up to six keypress bytes. When a key is pressed the keycode is in the packet, when the key is released, the keypress is removed from the packet.

    So, that should give me:
    read data: 00 00 00 00 00 00 00 00 - nothing pressed
    read data: 00 00 00 00 00 00 00 00 - nothing pressed
    read data: 00 00 04 00 00 00 00 00 - 'A' pressed
    read data: 00 00 04 00 00 00 00 00 - 'A' still pressed
    read data: 00 00 04 00 00 00 00 00 - 'A' still pressed
    read data: 00 00 00 00 00 00 00 00 - 'A' released
    etc.

    What I am getting instead is the packet immediately after the keypress packet is a key-released packet. Also, if the key is held down after a few empty packets I get another keypress packet - the keyboard is doing typematic repeats when I, the host, should be doing it. To make things worse, I sometimes get from a press/release two pairs of press/release packets.

    I get:
    read data: 00 00 00 00 00 00 00 00 - nothing pressed
    read data: 00 00 00 00 00 00 00 00 - nothing pressed
    read data: 00 00 04 00 00 00 00 00 - 'A' pressed
    read data: 00 00 00 00 00 00 00 00 - but 'A' is still pressed
    read data: 00 00 00 00 00 00 00 00 - but 'A' is still pressed
    a little later, with 'A' still pressed:
    read data: 00 00 04 00 00 00 00 00 - 'A' pressed
    read data: 00 00 00 00 00 00 00 00 - but 'A' is still pressed
    read data: 00 00 00 00 00 00 00 00 - but 'A' is still pressed
    etc.

    And sometimes, from pressing and releasing 'A' in a normal typematic manner:
    read data: 00 00 00 00 00 00 00 00 - nothing pressed
    read data: 00 00 04 00 00 00 00 00 - 'A' pressed
    read data: 00 00 00 00 00 00 00 00 - 'A' released
    read data: 00 00 00 00 00 00 00 00 -nothing pressed
    read data: 00 00 04 00 00 00 00 00 - oi! 'A' isn't pressed!
    read data: 00 00 00 00 00 00 00 00 - 'A' released
    read data: 00 00 00 00 00 00 00 00 -nothing pressed

    Is this a familiar problem? I've been testing with a handful of UK1 layout Dell keyboards. I'm planning on getting a handful of non-Dell and non-UK keyboards from the scrappie for more testing. Plugging them into my desktop PC gives the behaviour I expect - no double presses and repeats when I expect the input to repeat.
     
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    ONLINE
    The keyboard is doing the repeat for you. What do you want it to do different?

    Edit, Sorry, missread your post. I'm assuming that modern keyboards can be programmed to autorepeat or not.

    Mike.
     
  3. jgharston

    jgharston New Member

    Joined:
    Dec 15, 2010
    Messages:
    14
    Likes:
    1
    Device Class Definition for Human Interface Devices (HID) says:
    Appendix C: Keyboard Implementation
    "/Repeat Rate/ and /Delay Before First Repeat/ are implemented by the host and not in the keyboard"

    It does look like the keyboard is performing typematic delay/repeat processing. If so, I can't find how to tell the keyboard what delay/repeat settings to use. It must be possible as with it plugged into a bog standard desktop PC I can set the delay/repeat to whatever I want, not just the rate that it is giving my test hardware. The only host-to-keyboard command I can find is to set the LEDs.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    ONLINE

    As you say, there must be someway to set the repeat rate etc and maybe even turn it off. Try asking on Game software forums as they must turn this feature off to implement games.

    As an aside, A few years ago I wrote the code for an RS232 keyboard for the PC and posted it here. It might be of no interest but was an interesting project at the time.

    Mike.
     
  6. jgharston

    jgharston New Member

    Joined:
    Dec 15, 2010
    Messages:
    14
    Likes:
    1
    I've more-or-less solved it, the bane of my life, inconsistant and unclear documentation.

    When you ask for data from the keyboard you receive a packet length, then a packet of data. A keyboard packet is eight bytes. I was ignoring anything that wasn't an 8-byte packet as non-keyboard data.

    It turns out a packet length of zero is a valid packet meaning "no change from last packet". So, doing that correctly gives me:

    no key pressed:
    08 00 00 00 00 00 00 00 00
    00
    00
    00
    08 00 00 00 00 00 00 00 00
    00
    etc.
    press a key gives:
    08 mm 00 kk 00 00 00 00 00
    00
    00
    00
    00
    00
    08 mm 00 kk 00 00 00 00 00
    00
    00
    00
    etc.
    release the key gives
    08 mm 00 00 00 00 00 00 00
    00
    00
    00
    etc.

    I can now implement typematic repeat at the host end now that I'm getting a consistant stream of key-on and key-off data.
     

Share This Page