Skip to content

Listening to Inputs from a Mouse

Ralph Niemitz edited this page Mar 29, 2018 · 2 revisions

First we have to create the DeviceManager. Then we can either listen to all connected mice at the same time, or we listen to one mouse individually. Unfortunately there is no unique identifier. All devices have a name but it is not unique to it.

We can listen to a mouse by adding an implementation of the MouseListener interface to it. The method DeviceManager.addMouseListener will add the given MouseListener to all connected mice. Then we have to call the startListening method for the mouse. Otherwise the thread that listens to the input events will not start.

It is only possible to listen to the left, middle, and right mouse button. Extra buttons will be ignored.

Example for an implementation of MouseListener

public class Adapter implements MouseListener {

	@Override
	public void onClick(MouseEvent event) { // Called when a mouse button was clicked
	
		System.out.println("Clicked " + event.getButton());
	}
	
	@Override
	public void onDrag(MouseEvent event) { // Called when the mouse is moving while a button is down
	
		// the current position
		int x = event.getX();
		int y = event.getY();
		
		// the delta to the original position since last update
		// negative delta on X means the mouse moved to the left and positive means it moved to the right
		// negative delta on Y means the mouse moved up and positive means it moved down
		int dx = event.getDeltaX();
		int dy = event.getDeltaY();
		
		// number of pixels the mouse cursor moved
		int movedX = (dx < 0 ? -dx : dx);
		int movedY = (dy < 0 ? -dy : dy);
		
		// the position before this method was called
		int oldX = x - dx;
		int oldY = y - dy;
		
		// the button that caused this to be a drag and not a move event
		int button = event.getButton();
		
		System.out.println("====");
		System.out.println("Moved: x=" + movedX + ",y=" + movedY);
		System.out.println("Old Position: x=" + oldX + ",y=" + oldY);
		System.out.println("New Position: x=" + x + ",y=" + y);
		Syste,.out.println("Button: " + button):
		System.out.println("====");
	}
	
	@Override
	public void onMove(MouseEvent event) { // Called when the mouse is moving (onDrag and onMove cannot be called at the same time)
	
		// the current position
		int x = event.getX();
		int y = event.getY();
		
		// the delta to the original position since last update
		// negative delta on X means the mouse moved to the left and positive means it moved to the right
		// negative delta on Y means the mouse moved up and positive means it moved down
		int dx = event.getDeltaX();
		int dy = event.getDeltaY();
		
		// number of pixels the mouse cursor moved
		int movedX = (dx < 0 ? -dx : dx);
		int movedY = (dy < 0 ? -dy : dy);
		
		// the position before this method was called
		int oldX = x - dx;
		int oldY = y - dy;
		
		System.out.println("====");
		System.out.println("Moved: x=" + movedX + ",y=" + movedY);
		System.out.println("Old Position: x=" + oldX + ",y=" + oldY);
		System.out.println("New Position: x=" + x + ",y=" + y);
		System.out.println("====");
	}
	
	@Override
	public void onRelease(MouseEvent event) { // Called when a mouse button is released
	
		System.out.println("Released " + event.getButton());
	}
	
	@Override
	public void onScroll(MouseEvent event) { // Called when the mouse wheel is used
	
		float units = event.getUnitsToScroll();
		
		if(units < 0) {
		
			System.out.println("Scrolled down");
		
		} else if(units > 0) {
		
			System.out.println("Scrolled up");
		}
	}
}

Example for listening to all mice

DeviceManager.create();
DeviceManager.addMouseListener(new Adapter());
DeviceManager.getMice().forEach(Mouse::startListening);

// YOUR CODE

DeviceManager.destroy();

Example for listening to an individual mouse

DeviceManager.create();
List<Mouse> mice = DeviceManager.getMice();
Mouse myMouse = mice.get(0); // the first mouse
myMouse.addMouseListener(new Adapter());
myMouse.startListening();

// When we listen to a single mouse, we can ask which buttons are currently down.
boolean isLeftButtonDown = myMouse.isButtonDown(MouseEvent.BUTTON_LEFT);

// YOUR CODE

DeviceManager.destroy();