Jump to content

Using new BotMouseListener class


Recommended Posts

Posted

Hey @Alek

When OSbot updated to 2.5 some API changes were made to implementing a mouse listener. As the title suggest, how do you use the new class BotMouseListener? Specifically what does the API doc mean when  

checkMouseEvent(java.awt.event.MouseEvent e)

 states: 

Use this method in place of mouseClicked(MouseEvent), mousePressed(MouseEvent), mouseReleased(MouseEvent).

 

Before 2.5 I was using a mouse listener to have a draggable paint:

onStart(){
	this.bot.addMouseListener(this); //method doesnt exist anymore	
	this.bot.getCanvas().addMouseMotionListener(this); //marked as deprecated
}

@Override
    public void mouseClicked(MouseEvent e) {
        //not used
    }

    @Override
    public void mousePressed(MouseEvent e) {
        Point clickPt = e.getPoint();
        if(paintArea.contains(clickPt)){
            movingPaint = true;
            xOffset = clickPt.x - paintArea.x;
            yOffset = clickPt.y - paintArea.y;
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        movingPaint = false;
        xOffset = 0;
        yOffset = 0;
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //not used
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //not used
    }


    @Override
    public void mouseDragged(MouseEvent e) {
        if(movingPaint){
            Point mousePos = e.getPoint();
            paintArea.x = mousePos.x - xOffset;
            paintArea.y = mousePos.y - yOffset;
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        //not used
    }

Thanks. 

Posted

Create a class that extends the BotMouseListener, and pass your Script instance into it. Then add that as the mouse listener, e.g. addMouseListener(new CustomMouseListener(this))

All the events are build into one now, you should be able to seperate the actions (e.g. dragging vs releasing)

Posted

You can still override motion events in BotMouseListener subclasses

You use checkMouseEvent as a sort of multiplexer like so:

public void checkMouseEvent(MouseEvent e) {
	switch(e.getID()) {
		case MouseEvent.MOUSE_PRESSED:
			//Code
			break;
		case MouseEvent.MOUSE_RELEASED:
			//Code
			break;
		case MouseEvent.MOUSE_CLICKED:
			//Code
			break;
	}
}

But remember to consume the events that you process, if you don't want them to be propagated

  • Like 1
Posted

Thanks, I got it working again. It took me a while to realize that Mouse dragged is not supposed to be in checkMouseEvent(MouseEvent). 

public void checkMouseEvent(MouseEvent e) {
	switch(e.getID()) {
		case MouseEvent.MOUSE_DRAGGED:
			//This should not be here, Mouse dragged needs be its own seperate overridden method. 
			break;
        ....
		
	}
}

Makes sense too as dragged likely needs to be "listened" differently than a press or release. 

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...