Mouse.click() does not work

I am running the following code to move the mouse and click it using input from the keyboard:

void setup() {

  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  
  Mouse.begin();
  
}

void loop() {
    
  // use serial input to control the mouse:
  if (Serial.available() > 0) {
    char inChar = Serial.read();
    Mouse.begin();
    switch (inChar) {   
    case 'u':
      // move mouse up
      Serial.println("u received");
      Mouse.move(0, -40);
      break; 
    case 'd':
      // move mouse down
      Mouse.move(0, 40);
      break;
    case 'l':
      // move mouse left
      Mouse.move(-40, 0);
      break;
    case 'r':
      // move mouse right
      Mouse.move(40, 0);
      break;
    case 'm':
      // perform mouse left click
      Mouse.click(MOUSE_LEFT);
      break;
    }
  }
}

When I run my code on the Arduino Due, and send 'u' on the serial port for example, which should move my mouse cursor up, nothing at all happens. Same goes for Mouse.click(). I have no idea what I'm doing incorrectly.

Once you have configured the Leonardo to be a mouse, it cannot also communicate via serial comms at the same time. It is either a serial port or a mouse (or a keyboard, etc.).

OK, so does that mean I have to have a "Serial.end()" segment before I do a "Mouse.begin()" every time?

Also, this example on the Arduino website seems to be doing exactly this:

Oh, OK. Looks like I was wrong. Well, not the first time, eh?

I just tested that on my Leonardo (the example you linked to). So you are right, it can do both.

But I don't know about the Due. I'll move this thread to the Due part of the forum.

a4rashid,

with the Arduino Due the Serial class communicate with the Programming Port.

If you have connected your PC on the Due's Native Port you shoud use the SerialUSB object
(this means that you must replace all the Serial with SerialUSB in your sketch).