Jump to content

Using new BotMouseListener class


yfoo

Recommended Posts

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. 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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