Hold Left Mouse Button

Am trying with a single button to hold the LEFT_MOUSE with the first left click and release it with the second left click (withou remove the selected text) , is that possible? I dont find the way to make it right.Thanks You

#include "Mouse.h"
const int mouseButtonLeft = 4;
boolean MantenerEstado = false;
void setup() {
pinMode(mouseButtonLeft, INPUT);
Mouse.begin();
Serial.begin(9600);
void loop() {
int clickStateLeftButton = digitalRead(mouseButtonLeft);
if (clickStateLeftButton == HIGH) {
if (MantenerEstado == false) {
MantenerEstado = true;
Mouse.press(MOUSE_LEFT);
}
else {
MantenerEstado = false;
Mouse.release(MOUSE_LEFT);
}
}

Your sketch is not compiling :frowning:

To detect the moment a button is pressed (the event, not the state of the button), the State Change Detection is used: https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection/
The Bounce2 library can debounce a button and has the State Change Detection built-in.

Apologies , the right code is that one , movement is right , but selection is not working well , i see in the serial monitor that when i press the button sometimes the value goes again to same state like boucing, maybe i can try do to that with an interrupt? What the best way?

#include "Mouse.h"// set pin numbers for the five buttons:
const int upButton = 6;
const int downButton = 2;
const int leftButton = 5;
const int rightButton = 3;
const int mouseButtonLeft = 4;
//const int mouseButtonRight = 7;
//const int mouseButtonCopy = 8;
int range = 20; // output range of X or Y movement; affects movement speed
int HoldButtonLeftState = 0;
int responseDelay = 20; // response delay of the mouse, in ms

void setup() {
// initialize the buttons' inputs:
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
pinMode(mouseButtonLeft, INPUT);
Mouse.begin();
Serial.begin(9600);

}

void loop() {
// read the buttons:
Serial.println(HoldButtonLeftState);
int upState = digitalRead(upButton);
int downState = digitalRead(downButton);
int rightState = digitalRead(rightButton);
int leftState = digitalRead(leftButton);
int clickStateLeftButton = digitalRead(mouseButtonLeft);
// calculate the movement distance based on the button states:
int xDistance = (leftState - rightState) * range;
int yDistance = (upState - downState) * range;

//Serial.println(clickStateLeftButton);
// if X or Y is non-zero, move:
if ((xDistance != 0) || (yDistance != 0)) {
Mouse.move(xDistance, yDistance, 0);
}

//Hold Left Key
if (clickStateLeftButton == HIGH) {
if (HoldButtonLeftState == false) {
HoldButtonLeftState = true;
Mouse.press(MOUSE_LEFT);

}
else {
HoldButtonLeftState = false;
Mouse.release(MOUSE_LEFT);
} //end-if HoldButtonLeftState

} //end-if clickStateLeftButton

delay(responseDelay);
}

as koepel suggested, the code needs to recognize the "press" not the state of the button

const byte ButPin       = 8;

int   buttonPushCounter = 0;
byte  butLast           = HIGH;

void setup()
{
    Serial.begin (9600);
    pinMode(ButPin, INPUT_PULLUP);      // configure with pullup
}

void loop() {
    byte  but = digitalRead(ButPin);    // read pin

    if (butLast != but)  {              // check for a change
        butLast = but;                  // capture new state

        if (LOW == but)  {              // check if FALLING
            buttonPushCounter++;
            Serial.println (buttonPushCounter);
        }
    }
}

Yes i try that, with a led works but not when i replace the led for the Mouse.press and Mouse.release instructions.

I need that option due i have just one arm and i can not press and move the stick at the same time.

sounds like you need to recognize both a press and release. both cases require recognizing a change in state

Will try that maybe will work!

Thanks all , solved using 2 variables

if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
if(stateLED == HIGH){
stateLED = LOW;
Mouse.release(MOUSE_LEFT);

:o :o :o :o have been very hard to solve !!!!!

Once again thanks for the brainstorm

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.