B4J Question ScrollPane kill/consume KeyEvents for it..

Magma

Expert
Licensed User
Longtime User
Hi there !

I have a question... well i want to have a pane moving with keys into a scrollpane...

Well... i can have check keyevents and moving the pane... and any object... but scrollpane also listening for keys too.. and moving scrollbar too (that is something i don't want to be automatically)

Thought that the solution was somewhere in the middle... but as i see is not so easy or something passing in front of me...

Yes the following works for moving my b4xview that is into scrollpane... but also moving the scrollbar :-(

dim DrawPane as Scrollpane 'loaded with layout bjl
dim SelectedB4XObject as b4xview 'can be a pane... into scrollpane...
dim stepd as int=1
...
Dim r As Reflector
r.Target = DrawPane 'drawpane is my scrollpane
r.AddEventHandler("Drawpane_KeyPressed", "javafx.scene.input.KeyEvent.ANY")
...
Sub Drawpane_KeyPressed_Event(e As Event)
Dim KE As Reflector
KE.Target = e
e.Consume 'kill it fast... but the same..
Dim keycode As String = KE.RunMethod("getCode")
Select Case keycode
'MOVING SELECTEDOBJECT WITH KEYS...
Case "UP"
SelectedB4XObject.TOP=SelectedB4XObject.TOP-stepd
Case "DOWN"
SelectedB4XObject.TOP=SelectedB4XObject.TOP+stepd
End Select

End Sub
 

Alexander Stolte

Expert
Licensed User
Longtime User
and moving scrollbar too
You will not be able to deactivate this behavior. I also had this problem with AS_ViewPager when it was still based on xCLV in B4J. That's why I developed the jPager back then, because you can't deactivate it in the scrollview. I had been looking for a solution for over a year.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
You will not be able to deactivate this behavior. I also had this problem with AS_ViewPager when it was still based on xCLV in B4J. That's why I developed the jPager back then, because you can't deactivate it in the scrollview. I had been looking for a solution for over a year.
I think may be... if trick it... set height and width of scrollpane or the inside pane at showable.. move the selectedobject-item-pane and then set the real width and height of scrollpane... but is somehow overkill...

*** or may be there is a special "java" thing we are not... know it...
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Have you tried

a, save the OnKeyPressed Handler for the scrollpane (getOnKeyPressed)
b, set the OnKeyPressed (scrollpane) to null
c, do your moving etc
d, restore the original Handler (setOnKeyPressed(savedHandler) )

Not tried it though - just a thought
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Have you tried

a, save the OnKeyPressed Handler for the scrollpane (getOnKeyPressed)
b, set the OnKeyPressed (scrollpane) to null
c, do your moving etc
d, restore the original Handler (setOnKeyPressed(savedHandler) )

Not tried it though - just a thought
a. How i can do that?
b. and is that possible and how ?
c. That is the only can do :) but with scrollpane scroll too :)
d. ...if a and b can be real... i am sure can be that too...

Thanks for the answers
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Hi there !

I have a question... well i want to have a pane moving with keys into a scrollpane...

Well... i can have check keyevents and moving the pane... and any object... but scrollpane also listening for keys too.. and moving scrollbar too (that is something i don't want to be automatically)

Thought that the solution was somewhere in the middle... but as i see is not so easy or something passing in front of me...

Yes the following works for moving my b4xview that is into scrollpane... but also moving the scrollbar :-(
Try this code.
B4X:
jo.RunMethod("addKeypressed", Array(ScrollPane1))
...
public Sub keypressed_event(KeyCode As String)
    Log("KeyCode="& KeyCode)
End Sub

#if java
import javafx.scene.control.ScrollPane;
import anywheresoftware.b4a.BA;
public static void addKeypressed(ScrollPane sp) {
    sp.setOnKeyPressed(e->ba.raiseEventFromUI(null, "keypressed_event",  e.getCode().toString()));
}
#End If
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Try this code.
B4X:
jo.RunMethod("addKeypressed", Array(ScrollPane1))
...
public Sub keypressed_event(KeyCode As String)
    Log("KeyCode="& KeyCode)
End Sub

#if java
import javafx.scene.control.ScrollPane;
import anywheresoftware.b4a.BA;
public static void addKeypressed(ScrollPane sp) {
    sp.setOnKeyPressed(e->ba.raiseEventFromUI(null, "keypressed_event",  e.getCode().toString()));
}
#End If


Am I doing something wrong.. ?

#if java
import javafx.scene.control.ScrollPane;
import anywheresoftware.b4a.BA;
public static void addKeypressed(ScrollPane sp) {
sp.setOnKeyPressed(e->ba.raiseEventFromUI(null, "skeypressed_event", e.getCode().toString()));
}
#End If

Dim asjo As JavaObject=DrawPane 'drawpane is my scrollpane....

asjo.RunMethod("addKeypressed", Array(DrawPane))

....
public Sub skeypressed_event(KeyCode As String)
Log("KeyCode="& KeyCode)
Select Case keycode
'MOVING SELECTEDOBJECT WITH KEYS...
Case "UP"
SelectedB4XObject.TOP=SelectedB4XObject.TOP-stepd
Case "DOWN"
SelectedB4XObject.TOP=SelectedB4XObject.TOP+stepd
End Select

End Sub

getting the following error:

src\b4j\example\b4xmainpage.java:792: error: non-static variable ba cannot be referenced from a static context
sp.setOnKeyPressed(e->ba.raiseEventFromUI(null, "skeypressed_event", e.getCode().toString()));
^
1 error
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Try this attached project, it seems to work.
 

Attachments

  • ScrollPaneKeyPress.zip
    2 KB · Views: 45
Upvote 0

teddybear

Well-Known Member
Licensed User
Dim r As Reflector
r.Target = DrawPane 'drawpane is my scrollpane
r.AddEventHandler("Drawpane_KeyPressed", "javafx.scene.input.KeyEvent.ANY")

This problem is that you are using KeyEvent.ANY for EventHandler , it will fire twice, you may add EventType testing or change it to KeyEvent.KEY_PRESSED .

B4X:
    Dim r As Reflector
    r.Target =  DrawPane
    r.AddEventHandler("keypressed", "javafx.scene.input.KeyEvent.KEY_PRESSED")
    ...
Sub KeyPressed_Event (e As Event)
    Dim jo As JavaObject = e
    Dim keycode As String = jo.RunMethod("getCode", Null)
    Log(keycode)
End Sub
 
Last edited:
Upvote 0

Magma

Expert
Licensed User
Longtime User
This problem is that you are using KeyEvent.ANY for EventHandler , it will fire twice, you may add EventType testing or change it to KeyEvent.KEY_PRESSED .

B4X:
    Dim r As Reflector
    r.Target =  DrawPane
    r.AddEventHandler("keypressed", "javafx.scene.input.KeyEvent.KEY_PRESSED")
    ...
Sub KeyPressed_Event (e As Event)
    Dim jo As JavaObject = e
    Dim keycode As String = jo.RunMethod("getCode", Null)
    Log(keycode)
End Sub
Well...
not working ...because... at your code not setting innernode width and height...

- Yes i don't want to scroll with keys...
- But i want to scroll with vertical horizontal scroll bar...

so when putting:

DrawPane.InnerNode.PrefHeight = 1080
DrawPane.InnerNode.PrefWidth = 1920

the arrow keys moving the drawpane...

So the solution not works with javaobject and not with reflection too.. (is the same)

but wait a minute.... thinking something...
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Found a solution...

We thought that targetting Drawpane (scrollpane in general) was the right... guess what... no...

We need to Focus to different control... and then make the movement...

attaching project for everyone need to see how and understand the problem :)
 

Attachments

  • ScrollPaneKeyPress-magma.zip
    5.2 KB · Views: 46
Upvote 0
Top