Side Mouse Buttons not Working

Hey guys, so I'm using an Arduino Leonardo board and also a USB Host Shield. My issue is that my scroll wheel or side mouse buttons won't work. I've read online it has something to do with the libraries and your sketch. I've read the default Mouse and USB Host Shield libraries are only meant for basic mouse features such as cursor movement and for the right and left mouse buttons. My question, which is really a very hopeful request, is to see if someone can help me or guide me through how I can get my side mouse buttons to work and maybe even my scroll wheel. I installed the HID-Project library because chat gpt told me that was needed, however I don't know how to incorporate it into my .ino sketch. By the way I use a glorious model d. Here is my sketch:

#include <Mouse.h> 
#include <Wire.h> 
#include <SPI.h> 
#include <usbhub.h> 
USB     Usb; 
USBHub     Hub(&Usb); 
 
byte bf[2];
 
#include <hidboot.h> 
 
HIDBoot<USB_HID_PROTOCOL_MOUSE>    HidMouse(&Usb); 
 
String myString;  
int j = 0; 
int c = 0; 
int e = 0; 
int lmb = 0; 
int rmb = 0; 
int mmb = 0; 
int dx; 
int dy; 
int arr[2]; 
int arrv[8]; 
 
class MouseRptParser : public MouseReportParser 
 
{ 
 
  protected: 
 
    void OnMouseMove  (MOUSEINFO *mi); 
 
    void OnLeftButtonUp (MOUSEINFO *mi); 
 
    void OnLeftButtonDown (MOUSEINFO *mi); 
 
    void OnRightButtonUp  (MOUSEINFO *mi); 
 
    void OnRightButtonDown  (MOUSEINFO *mi); 
 
    void OnMiddleButtonUp (MOUSEINFO *mi); 
 
    void OnMiddleButtonDown (MOUSEINFO *mi); 
 
}; 
 
 
void MouseRptParser::OnMouseMove(MOUSEINFO *mi) 
 
{ 
 
  dx = mi->dX; 
 
  dy = mi->dY; 
 
}; 
 
  
 
  
 
void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi) 
 
{ 
 
  lmb = 0; 
 
}; 
 
  
 
  
 
void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi) 
 
{ 
 
  lmb = 1; 
 
}; 
 
  
 
  
 
void MouseRptParser::OnRightButtonUp  (MOUSEINFO *mi) 
 
{ 
 
  rmb = 0; 
 
}; 
 
  
 
  
 
void MouseRptParser::OnRightButtonDown  (MOUSEINFO *mi) 
 
{ 
 
  rmb = 1; 
 
}; 
 
  
 
  
 
void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi) 
 
{ 
 
  mmb = 0; 
 
}; 
 
  
 
  
 
void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi) 
 
{ 
 
  mmb = 1; 
 
}; 
  
MouseRptParser  Prs; 
 
void setup() {
 
  delay(5000); 
  Mouse.begin(); 
  Serial.begin(115200); 
  Serial.setTimeout(1);
   
  Usb.Init(); 
  HidMouse.SetReportParser(0, &Prs); 
  } 
 
void loop() {
  dx = 0; 
 
  dy = 0; 
 
   
 
  j = 0; 
 
  c = 0; 
 
  e = 0; 
 
     
 
  Usb.Task(); 
 
  
 
  //Clicking 
 
  if (lmb == 0){ 
 
    Mouse.release(MOUSE_LEFT); 
 
    } 
 
     
 
  else if (lmb == 1){ 
 
    Mouse.press(MOUSE_LEFT); 
 
    } 
 
  
 
  if (rmb == 0){ 
 
    Mouse.release(MOUSE_RIGHT); 
 
    } 
 
     
 
  else if (rmb == 1){ 
 
    Mouse.press(MOUSE_RIGHT); 
 
    } 
 
  
 
  if (mmb == 0){ 
 
    Mouse.release(MOUSE_MIDDLE); 
 
    } 
 
     
 
  else if (mmb == 1){ 
 
    Mouse.press(MOUSE_MIDDLE); 
 
    } 
    if (Serial.available() > 0) {
    Serial.readBytes(bf, 2);
   
    Mouse.move(bf[0], bf[1], 0);
 
  }
   else { 
 
    Mouse.move(dx, dy); 
 
    } 
}

If someone knows if I need to install more libraries please let me know. And if someone knows how to make my code work with the library I mentioned above please also let me know.

Thanks for reading haha

The Leonardo has an ATmega32u4 with built-in USB communication. This allows the Leonardo to appear to a connected computer as a mouse and keyboard (in addition to a virtual (CDC) serial / COM port). Why do you need the USB Host Shield for ?

can you share a link to your USB Host Shield ? how does it connect to the Leonardo?

see this example

and the doc

The ATmega32u4's built-in USB is Device only - not host.

AIUI, the OP is creating a Host for a mouse

ah thx - I had missed that. There is a physical mouse connected to the system then !

This:

?

would be good to know how the shield connects to the Arduino then

cf this discussion Arduino leonardo + usb host shield

1 Like

Do the "basic" mouse features work?

Yes, the cursor movement and both left and right click since that's what the default libraries allow.

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