modifier key doesn't work (Keyboard and Mouse library)

Issue #342 resolved
Wenjie Wu created an issue

Here is my demo.

press A work well, but press press Command-A(select all) doesn't work.

environment:

  • Macbook Air m1 (13.2.1)
  • board: Pico
  • MicroBlocks IDE: 1.2.6, vm: 176

Comments (14)

  1. John Maloney repo owner

    There seems to be an issue with the HID library on Pico. (It works on the Adafruit Metro M0.)

    Investigating...

  2. John Maloney repo owner

    As I thought, this problem occurred only on RP2040 (Pico and Pico-W) boards. There is a fix (actually a workaround for the low-level library bug) in pilot release v1.2.29.

  3. John Maloney repo owner

    This HID keyboard stuff is new to me but I found this list of key codes in the Arduino Keyboard.h file.

    // Misc keys
    #define KEY_UP_ARROW      0xDA
    #define KEY_DOWN_ARROW    0xD9
    #define KEY_LEFT_ARROW    0xD8
    #define KEY_RIGHT_ARROW   0xD7
    #define KEY_BACKSPACE     0xB2
    #define KEY_TAB           0xB3
    #define KEY_RETURN        0xB0
    #define KEY_MENU          0xED // "Keyboard Application" in USB standard
    #define KEY_ESC           0xB1
    #define KEY_INSERT        0xD1
    #define KEY_DELETE        0xD4
    #define KEY_PAGE_UP       0xD3
    #define KEY_PAGE_DOWN     0xD6
    #define KEY_HOME          0xD2
    #define KEY_END           0xD5
    #define KEY_CAPS_LOCK     0xC1
    #define KEY_PRINT_SCREEN  0xCE // Print Screen / SysRq
    #define KEY_SCROLL_LOCK   0xCF
    #define KEY_PAUSE         0xD0 // Pause / Break
    
    // Numeric keypad
    #define KEY_NUM_LOCK      0xDB
    #define KEY_KP_SLASH      0xDC
    #define KEY_KP_ASTERISK   0xDD
    #define KEY_KP_MINUS      0xDE
    #define KEY_KP_PLUS       0xDF
    #define KEY_KP_ENTER      0xE0
    #define KEY_KP_1          0xE1
    #define KEY_KP_2          0xE2
    #define KEY_KP_3          0xE3
    #define KEY_KP_4          0xE4
    #define KEY_KP_5          0xE5
    #define KEY_KP_6          0xE6
    #define KEY_KP_7          0xE7
    #define KEY_KP_8          0xE8
    #define KEY_KP_9          0xE9
    #define KEY_KP_0          0xEA
    #define KEY_KP_DOT        0xEB
    
    // Function keys
    #define KEY_F1            0xC2
    #define KEY_F2            0xC3
    #define KEY_F3            0xC4
    #define KEY_F4            0xC5
    #define KEY_F5            0xC6
    #define KEY_F6            0xC7
    #define KEY_F7            0xC8
    #define KEY_F8            0xC9
    #define KEY_F9            0xCA
    #define KEY_F10           0xCB
    #define KEY_F11           0xCC
    #define KEY_F12           0xCD
    #define KEY_F13           0xF0
    #define KEY_F14           0xF1
    #define KEY_F15           0xF2
    #define KEY_F16           0xF3
    #define KEY_F17           0xF4
    #define KEY_F18           0xF5
    #define KEY_F19           0xF6
    #define KEY_F20           0xF7
    #define KEY_F21           0xF8
    #define KEY_F22           0xF9
    #define KEY_F23           0xFA
    #define KEY_F24           0xFB
    

    So, you could see if B0 gives you the Enter key. My understanding of the USB "human interface device" (HID) protocol is that keyboard devices report key events based on keycodes and the OS decides how to map them to Unicode characters.

  4. Wenjie Wu reporter

    you could see if B0 gives you the Enter key.

    B0 does not give me the Enter key.

    I'm guessing the problem might be Keyboard.write (Maybe the enter key can only use press like the modifier key?)

  5. Bernat Romagosa

    Hi, Wenjie. John means ( hex [B0] ), not the string B0, is that what you're trying?

    Alternatively, you can input 176, which is decimal for hexadecimal B0.

  6. Wenjie Wu reporter

    you can input 176

    Hi @Bernat Romagosa , Thanks for your reminder !

    What I tried was 176, but it doesn't seem to work.

  7. John Maloney repo owner

    The documentation for "special keys" is here.

    However, I think the HID support on the Pico may not not be mature, or it may not follow the above documentation. In order to get the modifier keys to work, I had to add a delay of at least 7 msecs, which is not needed on SAM D21 boards.

    We might use "trial and error" to find the code for the enter key: try every keycode in the range 0-255.

  8. John Maloney repo owner

    Unfortunately, not. It requires a USB controller that is capable of emulating an HID device (keyboard/mouse) and the ESP32 does not have such hardware. It looks like some of the newer ESP chips (ESP32-S2 and ESP32-C3) may have such hardware based on this table. But those chips are still quite new and MicroBlocks does not support them yet.

    RIght now, boards with SAM D21 or RP2040 chips are the only ones that have the hardware required for the Keyboard and Mouse library.

  9. Log in to comment