Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Using new BotMouseListener class

Featured Replies

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. 

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)

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

  • Author

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. 

I tried to make the API doc for that class pretty detailed. The changes were necessary to fix a variety of bugs. Make sure you are consuming the event as well!

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.