0

Hope you can help me!

I want to controle my pc cursor with the values from an gyroscope and an accelerometer. So basically when I move the sensor, the cursor should be moved. I used an MPU-6050 chip and an ESP32.

How I get the position of the cursor:

public static void main (String[] args){

        for(int i = 0; i<= 1000000; i++) {

        PointerInfo info = MouseInfo.getPointerInfo();

        Point location = info.getLocation();

        System.out.println("x="+ location.x + " y=" + location.y);

}

How I will get the values from the sensor:

 public SensorData(JsonObject data) {
    //accerlation accelerometer
    ax = data.get("ax").asDouble()/ASENSETIFITY;
    ay = data.get("ay").asDouble()/ASENSETIFITY;
    az = data.get("az").asDouble()/ASENSETIFITY;

    //temperature 
    temp = data.get("t").asDouble()/340.00+36.53;

    //gyroscope
    gx = data.get("gx").asDouble()/GSENSETIFITY;
    gy = data.get("gy").asDouble()/GSENSETIFITY;
    gz = data.get("gz").asDouble()/GSENSETIFITY;

}

public String toString() {
    return "ax: " + Double.toString(ax) +", ay: " + Double.toString(ay)
        +", az: " + Double.toString(az) +", temp: " + Double.toString(temp)
        +", gx: " + Double.toString(gx) +", gy: " + Double.toString(gy)
        +", gz: " + Double.toString(gz);

}

What I got so far:

package mouse;

import java.awt.AWTException;

import java.awt.Robot;
import Connection.SensorData;

public class MouseMoving {
Robot robot;

public MouseMoving(){
    try {
        robot = new Robot();
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


    public static void verarbeite(SensorData data){

    System.out.print("Deine Maus befindet sich da:");
    System.out.println(data);

    //robot.mouseMove(1,1);
}


}   

My Question:

How can I now controle the cursor? Do you have an idea?

I thought, that you need the last position of the mouse cursor, then get the current position of the sensor and change the mouse position. But how can I write this whith my values?

I found this website, where 3 objects where rotated on the pc when the sensor will be moved. But this isn't written in Java and I don't understand his solution.

Also I found this and this question. Maybe it will work like that. But I haven't been programming that long and don't understand it.

  • Does Robot help at all ? https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html – Brian Agnew Dec 16 '19 at 09:44
  • @BrianAgnew I updated my question with: what I got so far. I tried Robot and Robot.MouseMove, but I can just the cursor one x and y position. But the cursor should be moved, whenever I move the sensor.Is this possible with mousemove? – JavaBeginner Dec 16 '19 at 10:02

1 Answers1

0

If I understand your question well, you are trying to move the mouse cursor with Java. To achieve that, you can use the Robot class, which hase a MouseMove(int x, int y) method.

By default, it positions the cursor on the primary screen. If you have a multiple screen setup, you will have to pass a GraphicsDevice object to indicate which screen should be used.

Example, moving your cursor 10 pixels to the right

PointerInfo pointerInfo = MouseInfo.getPointerInfo();
Point mouseLocation = pointerInfo.getLocation();
GraphicsDevice graphicsDevice = pointerInfo.getDevice();
new Robot(graphicsDevice).MouseMove(mouseLocation.getX()+10, mouseLocation.getY());
Fortega
  • 19,463
  • 14
  • 75
  • 113
  • Thank you for the answer! Yeah, I've tried MouseMove before. But I can only pass one new X and Y position, but the mouse should be moved permaently. So whenever I move the sensor, the mouse should be also moved. Is it possible to write this with MouseMove? – JavaBeginner Dec 16 '19 at 09:57
  • Okay, I get that! But how do I now get the mouse moved based on the sensor data? The X and Y position should be moved when I move the sensor. Do I need the values from the accelerometer or from the gyroscope? Or do I have to combine them? – JavaBeginner Dec 16 '19 at 10:22
  • I updated my question with some new links. Maybe it will worke like that? – JavaBeginner Dec 16 '19 at 11:08