B4J Question How to 'read' mouse wheel rotation?

amorosik

Expert
Licensed User
I am using a canvas to draw figures
I would like to perform a zoom function using the mouse wheel
How to 'read' the scrolling of the mouse wheel on a canvas?
 

stevel05

Expert
Licensed User
Longtime User
You can add a filter using reflection (jReflector library), this will get the raw Mousewheel data, you can then do what you need with it.

For more information see the javadoc : https://docs.oracle.com/javase/8/javafx/api/javafx/scene/input/ScrollEvent.html

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Canvas1 As Canvas

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show

    Dim R As Reflector
    R.Target = Canvas1
    R.AddEventFilter("RPScroll","javafx.scene.input.ScrollEvent.SCROLL")


End Sub

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

    Log(MouseEvent.RunMethod("getDeltaY",Null))
End Sub

To get pixel values you can divide the DeltaY value by the appropriate Multiplier (See the javadoc)

B4X:
Log(MouseEvent.RunMethod("getDeltaY",Null) / MouseEvent.RunMethod("getMultiplierY",Null))
 
Last edited:
Upvote 1

amorosik

Expert
Licensed User
Thanks for the tip, but even using this minimal project code, the sub RPScroll never runs
The only change with respect to the indicated code is the line 19 that initializes the Canvas1 object since the compilation did not happen without it
And the addition of the jReflection 1.20 library
I am using B4J 9.50 (64bit), Jdk 11.0.1, on Win10 64bit
What is missing from the project to 'see' the mouse wheel?

My Main:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

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

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    Dim PagesManager As B4XPagesManager
    PagesManager.Initialize(MainForm)
 
    Canvas1.Initialize("evnCanvas")
    Dim R As Reflector
    R.Target = Canvas1
    R.AddEventFilter("RPScroll","javafx.scene.input.ScrollEvent.SCROLL")
End Sub

Private Sub RPScroll_Filter (E As Event)
    Dim MouseEvent As JavaObject = E
    Log(MouseEvent.RunMethod("getDeltaY",Null))
End Sub

'Template version: B4J-1.0
#Region Delegates
Sub MainForm_FocusChanged (HasFocus As Boolean)
    B4XPages.Delegate.MainForm_FocusChanged(HasFocus)
End Sub

Sub MainForm_Resize (Width As Double, Height As Double)
    B4XPages.Delegate.MainForm_Resize(Width, Height)
End Sub

Sub MainForm_Closed
    B4XPages.Delegate.MainForm_Closed
End Sub

Sub MainForm_CloseRequest (EventData As Event)
    B4XPages.Delegate.MainForm_CloseRequest(EventData)
End Sub

Public Sub MainForm_IconifiedChanged (Iconified As Boolean)
    B4XPages.Delegate.MainForm_IconifiedChanged(Iconified)
End Sub
#End Region

My B4XMainPage:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

I tried to attach the project file but I don't see anything in the post
Besides pressing ATTACH key and selecting the file to send, is there anything else to do?
 
Last edited:
Upvote 0

amorosik

Expert
Licensed User
You should move the code posted by stevel05 from Main to B4XMainPage.

No, also with code on B4XMainPage, no RPScroll events appear
Where am I doing wrong?

My MAIN:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

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

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    Dim PagesManager As B4XPagesManager
    PagesManager.Initialize(MainForm)
End Sub

'Template version: B4J-1.0
#Region Delegates
Sub MainForm_FocusChanged (HasFocus As Boolean)
    B4XPages.Delegate.MainForm_FocusChanged(HasFocus)
End Sub

Sub MainForm_Resize (Width As Double, Height As Double)
    B4XPages.Delegate.MainForm_Resize(Width, Height)
End Sub

Sub MainForm_Closed
    B4XPages.Delegate.MainForm_Closed
End Sub

Sub MainForm_CloseRequest (EventData As Event)
    B4XPages.Delegate.MainForm_CloseRequest(EventData)
End Sub

Public Sub MainForm_IconifiedChanged (Iconified As Boolean)
    B4XPages.Delegate.MainForm_IconifiedChanged(Iconified)
End Sub
#End Region


My B4XMainPages:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Canvas1 As Canvas
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    Canvas1.Initialize("Canvas100")
    Dim R As Reflector
    R.Target = Canvas1
    R.AddEventFilter("RPScroll","javafx.scene.input.ScrollEvent.SCROLL")
End Sub

Private Sub RPScroll_Filter (E As Event)
    Dim MouseEvent As JavaObject = E
    Log(MouseEvent.RunMethod("getDeltaY",Null))
End Sub

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you have added the Canvas with the designer you shouldn't Initialize it in code.

If this is not the case, can you post your project?
 
Upvote 0

amorosik

Expert
Licensed User
If you have added the Canvas with the designer you shouldn't Initialize it in code.

If this is not the case, can you post your project?

Without Canvas1.Initialize("Canvas100") now seem to be ok
I added it because initially the ide asked me to initialize it
Now even if I remove the row it compiles normally
It's a bit strange but now it works
Thank you very much
 

Attachments

  • B4J.zip
    208.9 KB · Views: 67
Upvote 0

amorosik

Expert
Licensed User
I tried to attach the project file but I don't see anything in the post
Besides pressing ATTACH key and selecting the file to send, is there anything else to do?

I was trying to attach a file with extension .7z
I didn't realize that the system only accepts files with some well-defined extensions
I prepared a .zip and everything worked normally
 
Upvote 0
Top