Enabling Scroll Wheel cross OS for Flash applications

Many developers come across the problem of enabling scroll wheel enabled UI controls for scroll regions and wonder how to manage the focus - so when the scroll wheel is used it scrolls the right control. This can be done easier than thought there is a 2nd parameter on the onMouseWheel event that provides the MovieClip that the Mouse is currently over - scrollTarget. This target will be the deepest MovieClip reference that the Mouse is currently over, so what can be done is iterate backwards from this to find out if the Mouse position is currently over the scrolling region.

Now getting Scroll Wheel to work on Mac is a little trickier as Flash doesn't have native support for this but there is a way to get this to work in Flash 8. The Javascript and Actionscript provided code from PixelBreaker as a SWFObject add-on allows you to do this. It's pretty good but has one shortcoming and that is that it only provides the delta parameter and not scrollTarget, I have an Actionscript fix for this and it's a bit of a hack but hey it works, I hope u find this useful.

This code iterates back through using the scrollTarget variable to find out which control it's currently over

  1.  
  2. import com.pixelbreaker.ui.MouseWheel;
  3. var mouseWheelManager:Object = new Object();
  4. mouseWheelManager.onMouseWheel = function(delta:Number, scrollTarget:MovieClip) {
  5. var sT;
  6. var i = scrollTarget;
  7. while(sT == undefined && (i!=undefined && i!=_root)) {
  8. if(i instanceof ListBox) {
  9. sT = i;
  10. delete i;
  11. break;
  12. }
  13. i = i._parent;
  14. }
  15. sT.mouseWheelEvt(delta);
  16. }
  17. MouseWheel.addListener(mouseWheelManager);
  18.  

This Actionscript fixes the code on PixelBreaker to allow for ScrollTarget to be available

  1.  
  2. private static function externalMouseEvent(delta:Number):Void
  3. {
  4. var st:MovieClip = _root.createEmptyMovieClip("st", _root.getNextHighestDepth());
  5. st.startDrag();
  6. //
  7. st.func = macBroadcaster.broadcastMessage;
  8. st.scope = macBroadcaster
  9. st.args = ["onMouseWheel", delta];
  10. //
  11. st.onEnterFrame = function() {
  12. //
  13. this._x = _root._xmouse;
  14. this._y = _root._ymouse;
  15. //
  16. if (this._droptarget != "" && this._droptarget!= undefined) {
  17. //
  18. this.args.push(eval(this._droptarget));
  19. this.func.apply(this.scope, this.args);
  20. //
  21. delete this.onEnterFrame;
  22. this.removeMovieClip();
  23. }
  24. };
  25. }
  26.