yfoo Posted February 14, 2018 Share Posted February 14, 2018 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. Quote Link to comment Share on other sites More sharing options...
Tom Posted February 14, 2018 Share Posted February 14, 2018 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) Quote Link to comment Share on other sites More sharing options...
FrostBug Posted February 14, 2018 Share Posted February 14, 2018 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 1 Quote Link to comment Share on other sites More sharing options...
yfoo Posted February 14, 2018 Author Share Posted February 14, 2018 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. 1 Quote Link to comment Share on other sites More sharing options...
Alek Posted February 14, 2018 Share Posted February 14, 2018 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! Quote Link to comment Share on other sites More sharing options...