Scene2D/VisUI - Auto scroll focus on hover ( Desktop Only )

I looked around Scene2D’s event system and I found a pretty quick solution to an annoying problem I’ve been having with scroll-focus in LibGDX. Since scene2D is designed by default for mobile UIs, you need to click on something to scroll it, this bit of code is a static function that auto-focuses the stage’s scroll focus to the scrollpane it’s added to! No more annoying click-to-scroll!

Put this static function anywhere:

public static EventListener getScrollOnHover(final Actor scrollPane) {
    return new EventListener() {
        @Override
        public boolean handle(Event event) {
            if(event instanceof InputEvent)
                if(((InputEvent)event).getType() == InputEvent.Type.enter)
                    event.getStage().setScrollFocus(scrollPane);
            return false;
        }
    };
}

myScrollPane.addListener(getScrollOnHover(myScrollPane));

You can also modify it to lose focus on the mouse exiting:


public static EventListener getLoseScrollOnHoverExit() {
    return new EventListener() {
        @Override
        public boolean handle(Event event) {
            if(event instanceof InputEvent)
                if(((InputEvent)event).getType() == InputEvent.Type.exit)
                    event.getStage().setScrollFocus(null);
            return false;
        }
    };
}
myScrollPane.addListener(getLoseScrollOnHoverExit());