B4J Question Using mouse scxroll wheel when a key is pressed

yo3ggx

Active Member
Licensed User
Longtime User
Hello,

I want to use SHIFT key to control how scroll event is acting.
Unfortunately ..RunMethod("getDeltaY", Null) return 0 when SHIFT key is pressed.
This is a normal behavior?

Thank you.
 
Please provide an example project to provide context and so we can see how you are creating the listener and it can be tested.
 
Upvote 0
A simple example:
.
Code: Copy
    Dim jo As JavaObject = MainForm.RootPane
    Dim e As Object = jo.CreateEventFromUI("javafx.event.EventHandler", "scroll", Null)
    jo.RunMethod("setOnScroll", Array(e))
    Dim r As Reflector
    r.Target = MainForm.RootPane
    r.AddEventFilter("KeyDown", "javafx.scene.input.KeyEvent.KEY_PRESSED")
    r.AddEventFilter("KeyUp", "javafx.scene.input.KeyEvent.KEY_RELEASED")

Code: Copy
Sub Scroll_Event(MethodName As String, Args() As Object) As Object
    Dim scrollevent As JavaObject = Args(0)
    log(scrollevent.RunMethod("getDeltaY", Null))
    Return Null
End Sub

Code: Copy
Sub KeyDown_Filter(e As Event)
    Dim r As Reflector
    r.Target = e
    Dim keycode As String = r.RunMethod("getCode")
    log(keycode)
    e.Consume
Return
Sub KeyUp_Filter(e As Event)
    Dim r As Reflector
    r.Target = e
    Dim keycode As String = r.RunMethod("getCode")
    e.Consume
End Sub


Returned value is +/- 13 for slow scrolling without pressing SHIFT (or pressing any other key), but 0 if SHIFT is pressed.
 
Upvote 0
I think you are probably right, but you can use the reflector library and event filter which provides the MouseEvent.
B4X: Copy
Dim R As Reflector
R.Target = MainForm.RootPane
R.AddEventFilter("RPScroll","javafx.scene.input.ScrollEvent.SCROLL")

Then

B4X: Copy
Private Sub RPScroll_Filter (E As Event)
    Dim MouseEvent As JavaObject = E

    If MouseEvent.RunMethod("isShiftDown",Null) Then
        
    Else

    End If
     
End Sub
 
Upvote 0
Tried your suggestion, but the issue seems to be the same. When SHIFT key is pressed, returned value for getDeltaY is still 0.
I was not able to find another way to get scroll direction without using getDeltaY.
 
Upvote 0
Paste this into a new project with jReflection and JavaObject libraries selected:

B4X: Copy
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
'    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
    Dim R As Reflector
    R.Target = MainForm.RootPane
    R.AddEventFilter("RPScroll","javafx.scene.input.ScrollEvent.SCROLL")
End Sub

Private Sub RPScroll_Filter (E As Event)
    Dim MouseEvent As JavaObject = E

    If MouseEvent.RunMethod("isShiftDown",Null) Then
        Log("Shift Down")
    Else
        Log("Shift Not Down")
    End If
    
End Sub
 
Upvote 0
Even my initial test was using the same code, but inside my app, I've redone the test in a new project with the same result.
It works using isAltDown and IsControlDown, but not isShiftDown
Exactly! Any other key can be used except SHIFT, even with the original key filters ( as I need them for other shortcuts). Looks strange ...
 
Upvote 0
Probable answer is here: https://stackoverflow.com/questions/42429591/javafx-shiftscrollwheel-always-return-0-0

It appears that the shift key is reserved to control the scroll direction, which doesn't mean you cant use it, it just means use getDeltaX.

Something like:

B4X: Copy
Private Sub RPScroll_Filter (E As Event)
    Dim MouseEvent As JavaObject = E

    If MouseEvent.RunMethod("isShiftDown",Null) Then
        Log("Shift Down")
        Log(MouseEvent.RunMethod("getDeltaX", Null))
    Else
        Log("Shift Not Down")
        Log(MouseEvent.RunMethod("getDeltaY", Null))
    End If
 
End Sub

Unless you want to control both directions, in which case avoid using the shift key for anything else.
 
Last edited:
Upvote 0
Back
Top