Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: The control keys do not work with Keyboard.h

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    9

    The control keys do not work with Keyboard.h

    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 KurtE; 11-28-2019 at 01:44 PM. Reason: code tag

  2. #2
    Junior Member
    Join Date
    Nov 2019
    Posts
    9
    ...I have a Teensy 3.2

  3. #3
    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.

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,750
    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"
    uint16_t 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){};
    }

  5. #5
    Junior Member
    Join Date
    Nov 2019
    Posts
    9
    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

    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){};
    }

  6. #6
    There is yet another HID library https://github.com/NicoHood/HID that i think supports the boot protocol

    I have not tried it and don't know if it will support Teensy 3.2 (although Teensy is mentioned).
    It has lots of examples and one of them is "Bootkeyboard" which states "Shows that keyboard works even in bios."

    From info page , https://github.com/NicoHood/HID/wiki...#boot-keyboard "Works with BIOS (and normal PC mode)"

  7. #7
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,750
    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,                                   // bInterfaceSubClass (0x01 = Boot)
            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.

  8. #8
    Junior Member
    Join Date
    Nov 2019
    Posts
    9
    Thanks for the information, but:
    Click image for larger version. 

Name:	teensyNO.JPG 
Views:	15 
Size:	55.6 KB 
ID:	18285

  9. #9
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,835
    Quote Originally Posted by cmussoni View Post
    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.

  10. #10
    Junior Member
    Join Date
    Nov 2019
    Posts
    9
    KurtE:
    does it really work for you on USB2.0?
    I'm also using two machines both with USB2.0 .

    PaulStoffregen:
    I try to do the tests you asked for...

  11. #11
    Junior Member
    Join Date
    Nov 2019
    Posts
    9
    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.

  12. #12
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,750
    What I wonder from your posting #8, why it does not support Teensy?

    Could it be that it does not like the other HID protocol defined for the Serial emulation?

    I don't remember how hard it is to try to configure to not have SEREMU... I have seen a few threads mention it, like:
    https://forum.pjrc.com/threads/50937...l=1#post175106

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •