Scroll and right-click

Hi. I've just taken a PS/2 touchpad out of an old laptop and connected it to an Arduino Pro Micro to use it as a USB touchpad on a Raspberry Pi.

This is the project I followed: https://www.instructables.com/Arduino-Controlled-USB-Trackpad/

This is the code that I used (from one of the comments in the Instructables page):

/*

* PSMouse Library - http://github.com/jazzycamel/PS2Mouse

* Script adapted by Collie147 from example - added Mouse.h and press/release functions

*/

#include "PS2Mouse.h"

#include "Mouse.h"

PS2Mouse PS2mouse(9,10)

void setup(){

Mouse.begin();

PS2mouse.begin();

}

void loop(){

uint8_t stat;

int x,y;

PS2mouse.getPosition(stat,x,y);

Mouse.move(x, -y, 0);

if (bitRead(stat, 0) == 1){

Mouse.press();

}

else{

Mouse.release();

}

}

This makes the touchpad work as a USB mouse, and double-tap to double-click works.

However, I am unable to scroll or right-click using the touchpad. Is there some code I can add that will enable this? Do I need a different library? Are there different connections that should be made between the two boards?

The Mouse library doesn't support scrolling but you can add the right click using the following code segment, given the used touchpad supports right clicks:

if (bitRead(stat, 2) == 1){
  Mouse.press(MOUSE_RIGHT);
}
else{
  Mouse.release(MOUSE_RIGHT);
}
1 Like

Thank you.

This doesn't seem to be working, but maybe I'm adding it to the wrong section? It compiles and uploads successfully, but then it does not seem to register as a HID.

Here is my code as it stands.

/*

* PSMouse Library - http://github.com/jazzycamel/PS2Mouse

* Script adapted by Collie147 from example - added Mouse.h and press/release functions

*/

#include "PS2Mouse.h"

#include "Mouse.h"

PS2Mouse PS2mouse(9,10);

void setup(){

Mouse.begin();

PS2mouse.begin();

}

void loop(){

uint8_t stat;

int x,y;

PS2mouse.getPosition(stat,x,y);

Mouse.move(x, -y, 0);

if (bitRead(stat, 0) == 1){

Mouse.press();

}

else{

Mouse.release();

}

if (bitRead(stat, 2) == 1){
  Mouse.press(MOUSE_RIGHT);
}
else{
  Mouse.release(MOUSE_RIGHT);
}

}

}

I appreciate any assistance.

EDIT: It seems that if I disconnect and then re-connect the ground wire, the touchpad does work again, but right clicking still does not. Note that simply unplugging the USB and plugging it back in again does not seem to do the trick.

Try the following code:

/*

* PSMouse Library - http://github.com/jazzycamel/PS2Mouse

* Script adapted by Collie147 from example - added Mouse.h and press/release functions

*/

#include "PS2Mouse.h"

#include "Mouse.h"

PS2Mouse PS2mouse(9,10);

void setup(){

	Mouse.begin();

	PS2mouse.begin();

}

void loop() {

	uint8_t stat;

	int x,y;

	PS2mouse.getPosition(stat,x,y);

	Mouse.move(x, -y, 0);

	if (bitRead(stat, 0) == 1){
		Mouse.press();
	}
	else {
		if (Mouse.isPressed()) {
			Mouse.release();
		}
	}

	if (bitRead(stat, 2) == 1){
		Mouse.press(MOUSE_RIGHT);
	}
	else {
		if (Mouse.isPressed(MOUSE_RIGHT)) {
			Mouse.release(MOUSE_RIGHT);
		}
	}

}

The USB HID driver might be confused by too many release events of both mouse buttons without a press event before.

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