Robot not pressing mouse during mouse movement method

66 Views Asked by At

I am currently working on a basic utility software that has the ability to record Keyboard & Mouse input, save the input to a .txt file, and playback the data on a file. I am revising the program for added file and playback functionality. The problem I'm having is with the Robot.mousePress() method within the mouse movement method:

public static void executeMouseMovementData() {

    mouseRobot.mousePress(InputEvent.BUTTON1_MASK);
    for (int i=0; i < MouseDataHandler.mouseData.size(); i++) {
        mouseRobot.moveMouse(MouseDataHandler.mouseData.get(i).getX(), MouseDataHandler.mouseData.get(i).getY());
        mouseRobot.delay(MouseDataHandler.mouseData.get(i).getTimeElapsed());
    }
    mouseRobot.releaseMouse();
}

This program follows a basic sequence of events: 1 Data initialization, 2 Press mouse, 3 Move mouse, 4 Release mouse. Unlike another method I've successfully implemented, this method does not press the mouse at any time for no obvious reason. Mouse movement works beautifully with the playback feature. I just can't seem to get the Robot to execute any type of mouse event other than movement, even if I restructure the method.

I've tried editing the method to make sure the Robot doesn't press the mouse at the time in which the "playback" button on the GUI is pressed, as to not mess with the focus of the mouse event. The error likely isn't related to other aspects of the code, because everything else in the program runs smoothly. The object "mouseRobot" is an basic extension class of the Java.awt.Robot class with a basic interface for compound Robot mouse methods(), and I even directly call the mousePress method from the Robot class.

What could be the malfunction that occurs with within this method?


1

There are 1 best solutions below

0
On

Solved. Improved the method in which mouse movements are handled to do one mouse movement per frame. The class can now accurately perform various checks and data changes in between mouse movements, while also allowing other classes to function without being held up from a lengthy for loop. The method in the question was extremely inefficient, impractical and basically acted as a 'while' loop.

public void handleMouseMovements() {
    if (shouldAttemptToMoveMouse) {
        if (!targetHasBeenReached(currentAdjustedX, currentAdjustedY, targetX, targetY)) {
            if (!movementCreated) {
                calculateDirection(startX, startY, targetX, targetY);
                getLineIndexToUse();
                initializeMoveData(repositoryFileIndex, fileIndex);
                movementCreated = true;
                firstTime = System.currentTimeMillis();
            }
            if (CMMI >= Main.mouseDataHandler.getSizeOfRepositoryIndex(repositoryFileIndex, fileIndex)){
                CMMI =0;
                loopMovement();
            }
            if (movementfileIndexTimeHasElapsed(repositoryFileIndex, fileIndex)) {
                moveMouse(repositoryFileIndex, fileIndex);
                CMMI++;
                firstTime = System.currentTimeMillis();
            }
        }
        else {
            resetData();
        }
    }
}
public void moveMouse(int repositoryFileIndex, int fileIndex) {
    currentX = MouseDataHandler.mdr.get(repositoryFileIndex).get(fileIndex).get(CMMI).getX();
    currentY = MouseDataHandler.mdr.get(repositoryFileIndex).get(fileIndex).get(CMMI).getY();
    currentAdjustedX = currentX + distanceX;
    currentAdjustedY = currentY + distanceY;
    Main.bot.moveMouse(currentAdjustedX + Main.getX(), currentAdjustedY + Main.getY() + 25);
}

This method is vastly more efficient and handles all criteria necessary to determine direction, determine file index of mouse data to be used, calculates target-file index offsets, and has proper time intervals inbetween mouse movements.