breadboard
docs

Example project · Builds & projects

Uno R4 · USB keyboard and mouse

Official Keyboard and Mouse libraries send Ctrl+Shift+A, uppercase B, mouse buttons and relative movement through the emulated USB controller. Select the board to see USB console and HID reports in the Inspector. Serial echoes each line you send. Reports stay inside Breadboard.

The Uno R4 · USB keyboard and mouse circuit as rendered by the simulator

How it works

The RA4M1 on the UNO R4 has its own full-speed USB controller, so the board can be a USB device. With the official Keyboard 1.0.7 and Mouse 1.0.1 libraries, it shows up as a USB serial console plus a keyboard and a mouse. Breadboard emulates the controller and plays the part of the host computer: it configures the device and records each HID report that the device sends. The reports stay inside the simulator; nothing types or moves on your own computer.

setup() starts the libraries, waits until the USB console is open and prints public HID ready. In loop(), the sketch echoes any pending characters, runs a fixed sequence, and waits 250 ms before repeating. It presses Ctrl, Shift and A together and releases them, and types an uppercase B. It presses the left and right mouse buttons, moves the mouse 7 units right and 3 units up with one wheel step, releases the buttons and clicks the middle button. Each action becomes one or more real HID reports.

What to watch

Serial prints public HID ready, then public HID reports sent about four times per simulated second. Select the board: its Inspector shows the USB device, configured with its USB serial console, and the HID reports it has sent.

What's on the bench

  • Arduino Uno R4

The code

This Arduino C++ sketch lives in Code; sign in and press Compile & upload to build real firmware for the emulated board.

// SPDX-License-Identifier: MIT
// Official Keyboard 1.0.7 and Mouse 1.0.1 APIs; reports remain in the simulator.
#include <Keyboard.h>
#include <Mouse.h>

void setup() {
  Serial.begin(115200);
  Keyboard.begin();
  Mouse.begin();
  while (!Serial) {}
  Serial.println("public HID ready");
}

void loop() {
  while (Serial.available()) Serial.write(Serial.read());
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_SHIFT);
  Keyboard.press('a');
  Keyboard.release('a');
  Keyboard.releaseAll();
  Keyboard.write('B');
  Mouse.press(MOUSE_LEFT | MOUSE_RIGHT);
  Mouse.move(7, -3, 1);
  Mouse.release(MOUSE_ALL);
  Mouse.click(MOUSE_MIDDLE);
  Serial.println("public HID reports sent");
  delay(250);
}

Firmware licence

Notice: this firmware uses the LGPL-licensed Arduino core library

arduino-unor4wifi-usb-keyboard-mouse.bin is statically linked with ArduinoCore-renesas 1.6.0, in Breadboard's port of its UNO R4 WiFi variant to Renesas FSP 5.3.0. Parts of that core are licensed under the GNU Lesser General Public License, version 2.1 or (at your option) any later version ("the Library"). Breadboard distributes this firmware under terms that permit you to modify the work for your own use and to reverse engineer it for debugging such modifications.

This image also links official Keyboard 1.0.7 and Mouse 1.0.1. Keyboard layout files use the GNU Lesser General Public License, version 3. The package and source bundle include the LGPL-3.0 and GPL-3.0 texts.

Try this

  • Type a line in Serial and press Enter. The board echoes it back between its own status lines.
  • In Code, change Mouse.move(7, -3, 1) to Mouse.move(-20, 0, 0), then sign in and press Compile & upload. The mouse reports in the Inspector now carry a move to the left and no wheel step.
  • Replace Keyboard.write('B') with Keyboard.print("Hi"), comment out the Mouse calls in loop(), then upload again. Compare the keyboard report bytes under USB console and HID reports.

Related projects