Home > OS >  Arduino Mouse.Click Function (C#)
Arduino Mouse.Click Function (C#)

Time:05-24

Arduino IDE Codes

#include <Mouse.h>
 
byte bt[3];
void setup() {
  Serial.begin(9600);
  Mouse.begin();
}

void loop(){
  if (Serial.available() > 0) {
    Serial.readBytes(bt, 3);
    Mouse.move(bt[0], bt[1], 0);
    Serial.read();
    if(bt[2]==1){
    Mouse.click(MOUSE_MIDDLE);
    }
    if(bt[2]==2){
    Mouse.click(MOUSE_RIGHT);
    }
  }
}

C# Codes

        public static void ArduinoMove(int xcoord, int ycoord)
        {
            port.Write(GetRealVal(xcoord), 0, 1);
            port.Write(GetRealVal(ycoord), 0, 1);
        } 


In this case, I can move the mouse using the "ArduinoMove(x, y)" code, but how can I make the mouse left click?

CodePudding user response:

Like shown in the documentation, the Mouse library also allows you to use MOUSE_LEFT. So you just can add an additional case for that in your Arduino implementation

if (bt[2] == 3)
{
    Mouse.click(MOUSE_LEFT);
}

which then can be written like

port.Write(new byte[] { 0, 0, 3 } , 0, 3);

Note that the first two bytes are set to 0 since they are used to move the mouse in your implementation.

CodePudding user response:

I guess by utilizing Mouse.click(MOUSE_LEFT); something similar to this:

void loop(){
  if (Serial.available() > 0) {
    Serial.readBytes(bt, 3);
    Mouse.move(bt[0], bt[1], 0);
    Serial.read();
    if(bt[2]==1){
    Mouse.click(MOUSE_MIDDLE);
    }
    if(bt[2]==2){
    Mouse.click(MOUSE_RIGHT);
    }
    if(bt[2]==3){
    Mouse.click(MOUSE_LEFT);
    }
  }
}

I mean, you already use MOUSE_RIGHT and MOUSE_MIDDLE, it's not much of a leap of faith to expect there is also a MOUSE_LEFT available to use, or?

  • Related