B4J Question Adding an onAction event to a button to handle pressing ENTER

MegatenFreak

Active Member
Licensed User
Hi. I've been searching around and reading about events and stuff, but I'm so confused.
As you know, the key that acts as "pressing" a button in Windows is Space Bar, not Enter! I wanna make it so a focused button can be activated by pressing ENTER.
I thought the only solution would be to add an onAction event, which will be fired when Enter is pressed. But I haven't managed to add the event. I tried calling JavaObject.CreateEvent, but I have no idea what the interface name (first parameter) should be.
Am I approaching this correctly? If so, how do I add the event? If not, What can I do to make this work?
Thank you so much in advance.
 

Jorge M A

Well-Known Member
Licensed User
I wanna make it so a focused button can be activated by pressing ENTER

B4J:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

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

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

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

Sub AddKeyPressedListener
    Dim r As Reflector
    r.Target = Button1
    r.AddEventHandler("keypressed", "javafx.scene.input.KeyEvent.KEY_PRESSED")
End Sub

Sub KeyPressed_Event (e As Event)
    
    Dim jo As JavaObject = e
    Dim keycode As String = jo.RunMethod("getCode", Null)

    Log(keycode)

    If keycode = "ENTER" Then
        Log("Executing...")
    Else If keycode = "SPACE" Then
        Log("NOT executing...")
    Else If keycode = "Y" Then

    Else If keycode = "N" Then
            
    End If
    
  e.Consume
    
End Sub

Libs: jReflection, JavaObject or XUI Views
 

Attachments

  • button.zip
    2.1 KB · Views: 182
Upvote 0

MegatenFreak

Active Member
Licensed User
B4J:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

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

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

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

Sub AddKeyPressedListener
    Dim r As Reflector
    r.Target = Button1
    r.AddEventHandler("keypressed", "javafx.scene.input.KeyEvent.KEY_PRESSED")
End Sub

Sub KeyPressed_Event (e As Event)
   
    Dim jo As JavaObject = e
    Dim keycode As String = jo.RunMethod("getCode", Null)

    Log(keycode)

    If keycode = "ENTER" Then
        Log("Executing...")
    Else If keycode = "SPACE" Then
        Log("NOT executing...")
    Else If keycode = "Y" Then

    Else If keycode = "N" Then
           
    End If
   
  e.Consume
   
End Sub

Libs: jReflection, JavaObject or XUI Views

Thank you so much Jorge. Erel pointed me in the right direction, but without your code I'd still have to dig around. Your code worked perfectly;)
 
Upvote 0
Top