bouton-clic-souris.ino

Arduino Leonardo — Board Fiche · File : bouton-clic-souris.ino · Language : Arduino · Use case : Un bouton physique qui fait un clic de souris gauche

The button acts like a mouse micro-switch: Mouse.click sends the left-click press/release. Handy for a presentation clicker or a physical panic button.

Hardware required

An Arduino Leonardo (ATmega32U4, 16 MHz, 5 V) with native USB: the board can pose as a keyboard or a mouse. 1 push button.

Wiring

Button between pin 2 and GND (internal pull-up).

How it works

The two-state machine prevents double clicks; Mouse.click(MOUSE_LEFT) emits the full HID event. In the Arduino IDE, select « Arduino Leonardo » and the port, then Upload. Caution: a sketch that sleeps too fast can « lose » the USB port; hold RESET for 12 s to wipe the bootloader.

Going further

Right click with MOUSE_RIGHT; wheel scroll with Mouse.move(0, 0, 1).

bouton-clic-souris.ino

// Bouton sur la broche 2 = clic gauche HID.
#include <Mouse.h>

const int BOUTON = 2;
bool enfonce = false;

void setup() {
  pinMode(BOUTON, INPUT_PULLUP);
  Mouse.begin();
}

void loop() {
  bool appui = digitalRead(BOUTON) == LOW;
  if (appui && !enfonce) {
    Mouse.click(MOUSE_LEFT);
    enfonce = true;
  } else if (!appui && enfonce) {
    enfonce = false;
  }
}

← Back to the board