# Capture mouse wheel movement

**URL:** <https://forum.inductiveautomation.com/t/capture-mouse-wheel-movement/19083>\
**Category:** Ignition\
**Created:** [August 2, 2018, 8:26pm UTC](https://forum.inductiveautomation.com/t/capture-mouse-wheel-movement/19083 "2018-08-02T20:26:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrogers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mrogers/32/4110_2.png) [@mrogers](https://forum.inductiveautomation.com/u/mrogers)\
**Post date:** [August 2, 2018, 8:26pm UTC](https://forum.inductiveautomation.com/t/capture-mouse-wheel-movement/19083/1 "2018-08-02T20:26:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 2, 2018, 8:41pm UTC](https://forum.inductiveautomation.com/t/capture-mouse-wheel-movement/19083/2 "2018-08-02T20:41:13Z")

</div>

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.

```auto
	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())

```

---

<div class="post-metadata">

**Author:** ![mrogers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mrogers/32/4110_2.png) [@mrogers](https://forum.inductiveautomation.com/u/mrogers)\
**Post date:** [August 2, 2018, 9:05pm UTC](https://forum.inductiveautomation.com/t/capture-mouse-wheel-movement/19083/3 "2018-08-02T21:05:58Z")

</div>

Very nice, working well so far. Thanks!

---

<div class="post-metadata">

**Author:** ![sfitze](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@sfitze](https://forum.inductiveautomation.com/u/sfitze)\
**Post date:** [August 9, 2018, 1:38pm UTC](https://forum.inductiveautomation.com/t/capture-mouse-wheel-movement/19083/4 "2018-08-09T13:38:00Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![Brian](https://avatars.discourse-cdn.com/v4/letter/b/97f17d/32.png) [@Brian](https://forum.inductiveautomation.com/u/Brian)\
**Post date:** [August 9, 2018, 4:18pm UTC](https://forum.inductiveautomation.com/t/capture-mouse-wheel-movement/19083/5 "2018-08-09T16:18:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mrogers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mrogers/32/4110_2.png) [@mrogers](https://forum.inductiveautomation.com/u/mrogers)\
**Post date:** [August 9, 2018, 5:34pm UTC](https://forum.inductiveautomation.com/t/capture-mouse-wheel-movement/19083/6 "2018-08-09T17:34:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Brian](https://avatars.discourse-cdn.com/v4/letter/b/97f17d/32.png) [@Brian](https://forum.inductiveautomation.com/u/Brian)\
**Post date:** [August 9, 2018, 7:41pm UTC](https://forum.inductiveautomation.com/t/capture-mouse-wheel-movement/19083/7 "2018-08-09T19:41:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jsorlie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jsorlie/32/3645_2.png) [@jsorlie](https://forum.inductiveautomation.com/u/jsorlie)\
**Post date:** [November 3, 2018, 12:36pm UTC](https://forum.inductiveautomation.com/t/capture-mouse-wheel-movement/19083/8 "2018-11-03T12:36:52Z")

</div>

Expanding on code by @sfitze to access custom properties added to the component; cf [AbstractVisionComponent](http://files.inductiveautomation.com/sdk/javadoc/ignition784rc1/com/inductiveautomation/vision/api/client/components/model/AbstractVisionComponent.html)

```auto
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
