Hi, I need help, I don't know how to modify the code so that the right mouse button works, when I turn on the steamvr application, it doesn't register a right click, what about it (it works normally on the desktop, but not in steamvr, I use mobile on vr) ![]()
here is the code
#include <Mouse.h>
// Mouse buttons
#define MOUSE_SWITCH 2 // switch to turn on and off mouse control
#define MOUSE_LEFT_BTN 5
#define MOUSE_RIGHT_BTN 3
#define MOUSE_X_AXIS_INPT A0
#define MOUSE_Y_AXIS_INPT A1
#define MOUSE_SENSITIVITY 10
#define JOYSTICK_DEADZONE 1
#define LOOP_DELAY 5
// Operation modes for readMouseButton function
#define INPUT_LOW_MODE 1
#define INPUT_HIGH_MODE 2
#define INPUT_CHANGED_MODE 3
bool mouseActive = false; // whether or not to control the mouse
bool lastSwitchState = HIGH; // previous switch state
bool lastMouseRightState = HIGH;
bool lastMouseLeftState = HIGH;
void setup() {
pinMode(MOUSE_SWITCH, INPUT); // the switch pin
pinMode(MOUSE_LEFT_BTN, INPUT);
pinMode(MOUSE_RIGHT_BTN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void handleMouse() {
int xReading = analogRead(MOUSE_X_AXIS_INPT);
int yReading = analogRead(MOUSE_Y_AXIS_INPT);
// map the reading from the analog input range to the output range:
int mouseX = map(xReading, 0, 1024, -MOUSE_SENSITIVITY, MOUSE_SENSITIVITY);
int mouseY = map(yReading, 0, 1024, MOUSE_SENSITIVITY, -MOUSE_SENSITIVITY);
// if joystick moved out of the deadzone, move the mouse
if (mouseX > JOYSTICK_DEADZONE || mouseX < -JOYSTICK_DEADZONE || mouseY < -JOYSTICK_DEADZONE || mouseY > JOYSTICK_DEADZONE)
{
Mouse.move(mouseX, mouseY, 0);
}
}
/**
- Has folloving operation modes:
-
- INPUT_LOW_MODE - returns true when btn state changes from HIGH to LOW
-
- INPUT_HIGH_MODE - returns true when btn state changes from LOW to HIGH
-
- INPUT_CHANGED_MODE - return true when btn state changes from HIGH to LOW or from LOW to HIGH
*/
bool readMouseButton(int button, bool &lastState, unsigned char mode = INPUT_LOW_MODE) {
bool ret = false;
bool state = digitalRead(button);
if (state != lastState) {
if ((mode == INPUT_LOW_MODE && state == LOW) || (mode == INPUT_HIGH_MODE && state == HIGH) || mode == INPUT_CHANGED_MODE) {
ret = true;
}
}
lastState = state;
return ret;
}
- INPUT_CHANGED_MODE - return true when btn state changes from HIGH to LOW or from LOW to HIGH
void loop() {
// check if mouse in ON/OFF
if (readMouseButton(MOUSE_SWITCH, lastSwitchState)) {
if (mouseActive) {
Mouse.end();
mouseActive = false;
} else {
Mouse.begin();
mouseActive = true;
}
}
digitalWrite(LED_BUILTIN, mouseActive);
if (mouseActive) {
handleMouse();
// check left mouse btn
if (readMouseButton(MOUSE_LEFT_BTN, lastMouseLeftState, INPUT_CHANGED_MODE)) {
if (lastMouseLeftState == HIGH && !Mouse.isPressed()) {
Mouse.press();
} else if (lastMouseLeftState == LOW && Mouse.isPressed())
Mouse.release();
}
// check right mouse btn
if (readMouseButton(MOUSE_RIGHT_BTN, lastMouseRightState, INPUT_HIGH_MODE)) {
Mouse.press(MOUSE_RIGHT);
Mouse.release(MOUSE_RIGHT);
}
}
delay(LOOP_DELAY);
}
Does the issue only occur with the right mouse button, or do you have the same problem with any mouse button?
right button, left button works thank you in advance. ![]()
As the mouse.press and mouse.release come directly after each other you may need a slight delay between them or you could try using mouse.click(MOUSE_RIGHT) that does both.
thanks, it works, but it doesn't work in steamvr (so i can't move) when i hold down the right button and the left button on a purchased mouse so can move but when i do it on my made mouse it doesn't work what about it? can it be solved somehow?
Why are left button and right button treated completely differently?!?
// check left mouse btn
if (readMouseButton(MOUSE_LEFT_BTN, lastMouseLeftState, INPUT_CHANGED_MODE)) {
if (lastMouseLeftState == HIGH && !Mouse.isPressed()) {
Mouse.press();
} else if (lastMouseLeftState == LOW && Mouse.isPressed())
Mouse.release();
}
// check right mouse btn
if (readMouseButton(MOUSE_RIGHT_BTN, lastMouseRightState, INPUT_HIGH_MODE)) {
Mouse.press(MOUSE_RIGHT);
Mouse.release(MOUSE_RIGHT);
}
the code is not mine, i downloaded it from one side and now i will try to connect it to work for steamvr thank you in advance for the reply i don't know how to set up two buttons as on a mouse (i'm a beginner) ![]()
thank you all that it already works, i had to try it more ways, it's solved ![]()
I would still like to ask something how can I add more mouse buttons?
stromecek_555:
how can I add more mouse buttons?
Find the documentation for the Mouse.press() and Mouse.release()functions. See what values can be passed to them. Your code already passes no argument for Left and MOUSE_RIGHT for Right so if there are other valid values they could be used for other buttons.
Thank you ![]()
I have to ask why this code does not work, for me only the left button works, not the right button why? Thank you in advance for your reply
#include <Mouse.h>
#define left 3
#define right 2
void setup() {
pinMode(left, INPUT);
pinMode(right, INPUT);
//initiate the Mouse library
Mouse.begin();
}
void loop() {
if (digitalRead(left) == HIGH) {
Mouse.press();
delay(20);
if (digitalRead(left) == LOW) {
Mouse.release();
delay(100);
if (digitalRead(right) == HIGH) {
Mouse.press();
if (digitalRead(right) == LOW) {
Mouse.release();
}
}
}
}
}
void loop()
{
if (digitalRead(left) == HIGH)
{
Mouse.press();
delay(20);
if (digitalRead(left) == LOW)
{
Mouse.release();
delay(100);
if (digitalRead(right) == HIGH)
{
Mouse.press();
if (digitalRead(right) == LOW)
{
Mouse.release();
}
}
}
}
}
Problems:
Your 'if' statements are nested very deeply so all the outer 'if' statements have to be true before the inner 'if' statements get to execute.
You only check for "digitalRead(left) == LOW" 20 milliseconds after it was HIGH.
You only check for "digitalRead(right) == HIGH" 100 milliseconds after you find that 'left' was HIGH and then LOW.
You use "Mouse.press()" and "Mouse.release()" for both left and right buttons. In your earlier code you used "Mouse.press(MOUSE_RIGHT)" and "Mouse.release(MOUSE_RIGHT)" for the right button.
You should add State Change Detection for the two buttons. See: File->Examples->02.Digital->StateChangeDetection for an example.
this seems very related to your other post...
please keep everything into one discussion..
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
