🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Tab5 USBHIDKeyboard

    General
    2
    2
    27
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      jalmalogni
      last edited by

      I am attempting to use Tab5 to send keyboard events through a usb connection. I am able to use similar code to send keystrokes when using a Cardputer. I took the touch button example and added keyboard print(string) and write(keycode) to the button event. I also tried adding a delay after calling keyboard.begin()

      Using the same cable connection, the Cardputer successfully sends keyboard events to the computer, but the computer does not seem to receive any events from the Tab5.

      Is any way I can try to debug this?

      #include <M5Unified.h>
      #include <M5GFX.h>
      
      #include "USB.h"
      #include "USBHIDKeyboard.h"
      
      m5::touch_detail_t touchDetail;
      static int32_t w;
      static int32_t h;
      
      LGFX_Button button;
      
      USBHIDKeyboard Keyboard;
      
      void setup() {
        M5.begin();
      
        w = M5.Lcd.width();
        h = M5.Lcd.height();
        
        M5.Lcd.fillScreen(WHITE);
        M5.Display.setRotation(0);
        M5.Display.setTextDatum(top_center);
        M5.Display.drawString("Button Released", w / 2, 0, &fonts::FreeMonoBold24pt7b);
      
        button.initButton(&M5.Lcd, w / 2, h / 2, 200, 200, TFT_BLUE, TFT_YELLOW, TFT_BLACK, "BTN", 4, 4);
        button.drawButton();
      
      
        Keyboard.begin();
        USB.begin();
        delay(4000);
      
        M5.Display.drawString("usb done", w / 2, 100, &fonts::FreeMonoBold24pt7b);
        
      }
      
      
      
      void loop() {
        M5.update();
        touchDetail = M5.Touch.getDetail();
      
        if (touchDetail.isPressed()) {
          if(button.contains(touchDetail.x, touchDetail.y)){
              M5.Display.drawString("Button  Pressed", w / 2, 0, &fonts::FreeMonoBold24pt7b);
              Keyboard.print("hello");
              Keyboard.write(KEY_RETURN);
              delay(2000);
          }
        }
        else {
          M5.Display.drawString("Button Released", w / 2, 0, &fonts::FreeMonoBold24pt7b);
        }
      } 
      
      
      Y 1 Reply Last reply Reply Quote 0
      • Y
        yuyun2000 @jalmalogni
        last edited by

        @jalmalogni To debug the USB keyboard issue with your Tab5 (SKU:C145/K145), please try the following steps:

        Verify USB OTG Configuration: Ensure your code correctly initializes USB OTG mode. Add these build flags to your PlatformIO configuration:
        build_flags =
        -DARDUINO_USB_CDC_ON_BOOT=1
        -DARDUINO_USB_MODE=1

        Check USB Initialization Order: Modify the initialization sequence in setup():
        USB.begin(); // Initialize USB first
        delay(1000); // Add delay before keyboard initialization
        Keyboard.begin();

        Monitor USB Enumeration: Connect via WebTerminal through UiFlow2 to check if the USB device is recognized. Navigate to the serial monitor and look for USB HID initialization messages.

        Test Hardware Connection: Ensure you're using the USB-C port (labeled as USB 2.0 OTG) on Tab5, not the USB-A host port. Try different USB cables known to work with data transfer.

        Simplify Test Code: Use this minimal example to isolate the issue:
        #include <M5Unified.h>
        #include "USB.h"
        #include "USBHIDKeyboard.h"

        USBHIDKeyboard Keyboard;

        void setup() {
        M5.begin();
        USB.begin();
        delay(2000);
        Keyboard.begin();
        M5.Lcd.print("USB HID Ready");
        }

        void loop() {
        M5.update();
        if (M5.BtnA.wasPressed()) {
        Keyboard.print("Hello");
        Keyboard.write(KEY_RETURN);
        }
        }

        Check Power Management: Ensure Tab5 is sufficiently powered (use external power if battery level is low). USB HID functionality may be disabled under low power conditions.

        https://chat.m5stack.com/

        1 Reply Last reply Reply Quote 0
        • First post
          Last post