Capture mouse wheel movement

Can you capture a mouse wheel button event? I can’t seem to in the tests I’ve been doing, and it’d be helpful if I could get these events.

Sure is. I dropped this into the Power Table’s initialize event hook since it’s nice to have it only fire once. If you add this, you’ll probably want to close the window and re-open it to make sure the listener was actually added.

	from java.awt.event import MouseWheelEvent
	from java.awt.event import MouseWheelListener
	
	class DebugMouseWheelListener(MouseWheelListener):
		def mouseWheelMoved(self, event):
			print "Scroll wheel notches moved:", event.getWheelRotation()
			print "Units to scroll:", event.getUnitsToScroll()
			
	self.addMouseWheelListener(DebugMouseWheelListener())
5 Likes

Very nice, working well so far. Thanks!

I found a great use for this. I have a large amount of data I want to show on a horizontal bar chart. the chart would be unreadable if I tried to show it all at once. I already had a slider set up to update the bar chart to show a section of the data. So I wanted the scrollwheel on the chart to scroll through the data. I added this to the MouseEntered event on the chart.

from java.awt.event import MouseWheelEvent
from java.awt.event import MouseWheelListener
class EnteredMouseWheelListener(MouseWheelListener):
	def mouseWheelMoved(self, event):
		blips = event.getWheelRotation()
		sldr = event.source.parent.getComponent('sldr_chartWindow')
		sldr.value = max(sldr.minimum, min(sldr.value+blips, sldr.maximum))
for i in event.source.mouseWheelListeners:
	if i.__class__.__name__ == 'EnteredMouseWheelListener':
		event.source.removeMouseWheelListener(i)
event.source.addMouseWheelListener(EnteredMouseWheelListener())

Now the scrollwheel updates the chart very intuitively.

2 Likes

Just out of curiousity, does one need to remove the MouseWheelListener on the MouseExited event?

I’ve never played with this kind of code, but I could see it being useful in the future here for something.

I don’t think so, I’ve been using this method since it was suggested up there and its worked fine. Though usually I go through any existing mousewheellisteners that already exist (like if I’ve added it already) and remove them before adding a new one.

Interesting. So, does the component always react to the mouse wheel after it’s been initialized? For example, if below your bar chart you had a power table or list, would scrolling when hovering over one of those also move the bar chart?

I know if I have two power tables on a window, they will only scroll when my mouse is hovering over each table. I’m wondering if your code will make the bar chart behave similarly, or if it will always be listening.

Expanding on code by @sfitze to access custom properties added to the component; cf AbstractVisionComponent

def MouseWheelListener(self, initialized):

	if not initialized:
		from java.awt.event import MouseWheelEvent
		from java.awt.event import MouseWheelListener
		
		class canvasMouseWheelListener(MouseWheelListener):
			def mouseWheelMoved(self, event):
				# https://docs.oracle.com/javase/tutorial/uiswing/events/mousewheellistener.html
				# https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
				if event.isAltDown() or event.isControlDown() or event.isShiftDown():
					canvas = event.source.parent.getComponent('Paintable Canvas')
					scale = canvas.getPropertyValue("scale") + 10*event.getWheelRotation()
					canvas.setPropertyValue("scale",scale)
				return

		for i in self.mouseWheelListeners:
			if i.__class__.__name__ == 'canvasMouseWheelListener':
				self.removeMouseWheelListener(i)
		self.addMouseWheelListener(canvasMouseWheelListener())
		self._mwlInit = True
	return

Kudos to @pturmel for all the guidance

1 Like