The control keys do not work with Keyboard.h

cmussoni

Member
Hello everybody.
I've installed Teensyduino, Set "Usb type" = keyboard and "Keyboard layout": Italian (but I tried all the layouts).

When sending control characters (KEY_DELETE, KEY_UP_ARROW, KEY_DOWN_ARROW, KEY_BACKSPACE, etc.), these do not works.
eg. sending "KEY_DELETE" I retrieve an 'L' character.

The same Sketch on Arduino Leonardo works fine.

What could be the problem ?
Thanks so much

Code example:

Code:
#include "Keyboard.h"
char cancKey = KEY_DELETE;
void setup() {
  // open the serial port:
  Serial.begin(9600);
  // initialize control over the keyboard:
  Keyboard.begin();
}
void loop() {
  for (int i = 0; i <= 5; i++) {
    Keyboard.press(cancKey);//KEY_DELETE 0xD4
    Keyboard.releaseAll();
    delay (500);
  }
 while (true){};
}
 
Last edited by a moderator:
Hi

Doesn't using keyboard.begin use the Arduino HID library rather that the Teensy library ? Try commenting out. I cant remember if you need the #include "keyboard.h" on the teensy or not.
 
Sorry, I have not played much with this part of the code, but the first things I would try is to send KEY_DELETE directly...
Does that send? If so then the issue is you are truncating the value to a byte whereas the input to press takes a uint16_t...

And KEY_DELETE is defined as: #define KEY_DELETE ( 76 | 0xF000 )


i.e modify your program above to be:

Code:
#include "Keyboard.h"
[COLOR="#FF0000"]uint16_t cancKey = KEY_DELETE;[/COLOR]
void setup() {
  // open the serial port:
  Serial.begin(9600);
  // initialize control over the keyboard:
  Keyboard.begin();
}
void loop() {
  for (int i = 0; i <= 5; i++) {
    Keyboard.press(cancKey);//KEY_DELETE 0xD4
    Keyboard.releaseAll();
    delay (500);
  }
 while (true){};
}
 
macrosii:
Right, I removed both "keyboard.begin" and "Keyboard.h" respecting the examples of Teensy in the Arduino tool (I had already installed Teensyduino) and so it works.
But not in the BIOS, where I would need it.

KurtE:
thanks, replacing char with uint16_t everything works using Arduino libraries.
But not in the BIOS, where I would need it.

I have synthesized the sketch using examples from the Arduino tool where Teensyduino is installed.
Does not include (#include) any kind of library.
The code works without problems in Windows10 but not in BIOS where I really need it :confused::confused:

Code:
void setup() {
  //nothing
}

void loop() {
  for (int i = 0; i <= 5; i++) {
    Keyboard.press(KEY_DELETE);
    delay (1000);
    Keyboard.release(KEY_DELETE);
    delay (200);
  }
 while (true){};
}
 
I could be wrong, but I believe that when the teensy is setup to emulate a keyboard it is setup to be boot protocol...

That is:
Code:
#ifdef KEYBOARD_INTERFACE
        // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
        9,                                      // bLength
        4,                                      // bDescriptorType
        KEYBOARD_INTERFACE,                     // bInterfaceNumber
        0,                                      // bAlternateSetting
        1,                                      // bNumEndpoints
        0x03,                                   // bInterfaceClass (0x03 = HID)
        0x01,                                   // [COLOR="#FF0000"]bInterfaceSubClass (0x01 = Boot)[/COLOR]
        0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
        0,                                      // iInterface
        // HID interface descriptor, HID 1.11 spec, section 6.2.1
        9,                                      // bLength
        0x21,                                   // bDescriptorType
        0x11, 0x01,                             // bcdHID
        0,                                      // bCountryCode
        1,                                      // bNumDescriptors
        0x22,                                   // bDescriptorType
        LSB(sizeof(keyboard_report_desc)),      // wDescriptorLength
        MSB(sizeof(keyboard_report_desc)),
        // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
        7,                                      // bLength
        5,                                      // bDescriptorType
        KEYBOARD_ENDPOINT | 0x80,               // bEndpointAddress
        0x03,                                   // bmAttributes (0x03=intr)
        KEYBOARD_SIZE, 0,                       // wMaxPacketSize
        KEYBOARD_INTERVAL,                      // bInterval
#endif // KEYBOARD_INTERFACE
Maybe long shot, but maybe issue on where you plug the teensy in? On one of my older machines with a USB 3 board in it, if I plug in a keyboard into a USB3 slot, it does not work during boot process, but if I plug it into USB2 slot, it works fine... Again probably a long shot, but thought I would mention.
 
Thanks for the information, but:
teensyNO.JPG
:(:(:(
 
The code works without problems in Windows10 but not in BIOS where I really need it

Could you be more specific? You've only told us it does not work, but no real info about what actually goes wrong?

Perhaps your PC's bios doesn't recognize see Teensy as any sort of keyboard? That sort of problem would suggest something about the USB descriptors is not what your bios wants.

Or maybe it does, but some or all characters aren't using the Italian keymap? This sort of problem would suggest the descriptors are likely ok, but something in the code doing the communication isn't quite right.

Also, please try a quick experiment using 2 real keyboards (no Teensy or Arduino board). Does your PC's bios work correctly with 2 normal keyboards? If you reboot a few times, can each keyboard be used to enter the bios setup screen? While using the setup screen, does it respond to keystrokes on both keyboards? Windows supports 2 keyboards, but does your bios really support 2 keyboards? If your bios is only able to detect and use a single keyboard, then this problem may not be anything about Teensy's code, but a matter of which keyboard it chooses to recognize when more than 1 is present.
 
KurtE:
does it really work for you on USB2.0? :eek:
I'm also using two machines both with USB2.0 .

PaulStoffregen:
I try to do the tests you asked for...
 
PaulStoffregen:
I'm testing on a machine that has both USB and PS2. When I insert Teensy I don't have another keyboard connected.
Tomorrow however I try for curiosity to connect two keyboards together
That sort of problem would suggest something about the USB descriptors is not what your bios wants:I also think this but I don't know where to look.
Thanks a lot again.
 
@spikey - might help if you provide some additional information on what you are trying to do, which hardware (teensy, pc), what you have tried, and what you are seeing.

I don't think cmussoni has logged in here for maybe 4 years now
 
Back
Top