Getting multiple clicks on using state change (Leonardo)

The problem is I’m getting at least 3 clicks from (or on a state change. When I pull the pin low, the cursor goes to the position, but the click is more than 1 click and the cursor stays there for about 2 seconds then is normal again. (normal meaning my logitech mouse can control the cursor again.)

My main goal, - when a pin is pulled low the cursor will click (only once) on a certain position on the screen. The position is determined on what pin I activate.

I searched for weeks with no resolve, can someone confirm they get the same result. I’m not sure what is going on. I appreciate any help.

on my Leonardo:

// Global varibles:
int lastButtonState = LOW;        // state of the button last time you checked
int lastButton2State = LOW;       // state of the other button last time you checked
boolean mouseIsActive = true;    // whether or not the Arduino is controlling the mouse

void setup() {
  // initialize mouse control:

  // initialize serial communication:
  // make pin 2, 3 an input, using the built-in pullup resistor:
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);

void loop() {
  // read the first pushbutton:
  int buttonState = digitalRead(2);
  int x;
  int y;
  // if it's changed and it's high, toggle the mouse state:
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      // if mouseIsActive is true, make it false;
      // if it's false, make it true:
      mouseIsActive = !mouseIsActive;
      Serial.print("Mouse control state" );
  // save button state for next comparison:
  lastButtonState = buttonState;

  // read the analog sensors:
  int sensor1 = analogRead(A0);
  int sensor2 = analogRead(A1);

  int xAxis = map(sensor1, 0, 1023, -5, 5);
  int yAxis = map(sensor2, 0, 1023, -5, 5);

  if (mouseIsActive == true) {
    Mouse.move(xAxis, yAxis, 0);

    // read the second pushbutton:
    int button2State = digitalRead(3);

    // if it's changed and it's high, toggle the mouse state:
    if (button2State != lastButton2State) {
      if (button2State == LOW) {
        Serial.println("mouse pressed");
        for (int z = 1; z <= 10; z++) {

          Mouse.move(50, 50);

      }      else {
        Serial.println("mouse released");

    // save second button state for next comparison:
    lastButton2State = button2State;

When I pull the pin low

Does "the pin" refer to pin 2 or to pin 3? Details matter. Like, HOW are you pulling the pin LOW?

Hi PaulS,

I'm pulling to the pins to GND on the arduino, with a simple button.

Hi PaulS,

I'm pulling to the pins to GND on the arduino, with a simple button.

The correct ratio of answers to questions is 1:1.

Pin 3 is pulled to GND (I've tried with a wire direct to GND or using a button) . The change state should then activate a mouse click (once) but I'm getting more than one click, and getting more than one cursor position activating also.

Printing the state of the switch only when it is HIGH doesn't help with debugging. You need to print when the change is detected, regardless of type of change. If the message is printed when you don't expect, then the problem is not with the Keyboard or Mouse libraries.