Wish BJ4 ScrollEvent for MouseWheel event

stevel05

Expert
Licensed User
While I don't think you can capture the mouse wheel scroll directly, you can capture it's effect on a Node using reflection and the onScrollProperty.

Something like:

B4X:
Dim R As Reflector
R.Target = Slider1
R.AddEventHandler("Slider1Scroll","onScrollProperty")

Sub Slider1Scroll_event(e As Event)
    Dim ThisEvent As JavaObject = e
    Dim Moved As Double = ThisEvent.RunMethod("getDeltaY",Null)
    Slider1.Value = Slider1.Value + (Moved / Abs(Moved))
    e.Consume
End Sub
After testing, you can also add the onScrollProperty to the MainForm.RootPane and catch the scrollwheel movement without it affecting a view.

B4X:
    Dim R As Reflector
    R.Target = MainForm.RootPane
    R.AddEventHandler("PaneScroll","onScrollProperty")
Sub PaneScroll_event(e As Event)
    Dim ThisEvent As JavaObject = e
    Dim Moved As Double = ThisEvent.RunMethod("getDeltaY",Null)
    Log(Moved)
    e.Consume
End Sub
 
Last edited:

Jim Brown

Active Member
Licensed User
Thanks Steve,

Whilst I couldn't get your code above to work I found the following works perfectly, which I use to catch the event from a canvas:
B4X:
Dim R As Reflector
R.Target = canvas1
R.AddEventHandler("canvasScroll","javafx.scene.input.ScrollEvent.SCROLL")

...

Sub canvasScroll_Event(e As Event)
    Dim ThisEvent As JavaObject = e
    Dim Moved As Double = ThisEvent.RunMethod("getDeltaY",Null)
    Log(Moved)
    e.Consume
End Sub
 
Top