B4J Question [solved] Trigger Button_Action event with 'Enter'

Knoppi

Active Member
Licensed User
Longtime User
Trigger Button_Action event with 'Enter'

I have in my dialog 2 buttons "OK" and "Cancel".
The dialog is to be operated completely with the keyboard.
How can I trigger with the Enter-Key the "action" event for the selected button?

The "setDefaultButton"-function is not possible in this case.
 

Daestrum

Expert
Licensed User
Longtime User
If the button has focus, then pressing return/enter fires the Action event for the button, just as if you clicked it.
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
The dialog is modal (showandwait) the tab-key works but the enter-key dosnt work.
Any idea?
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
I know that link and it is a possible solution, but how does the enter-key not work?
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
It's difficult to answer without seeing an example. That could be related to your window not having the focus, or setDefaultButton acting on another button.

What you could test, is to .requestFocus on your button, and see if that helps.

It could also be because you call .ShowAndWait too early in you code, and that blocks the enter button. You can try with .Show first.
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
Here a simple Example:
(B4J 5.90)
B4X:
'Called from Main
Dialog.Buttons

B4X:
'Static code module
Sub Process_Globals
    Private fx As JFX
    Private frm As Form
End Sub

Public Sub Buttons
    Private OkEvent     As String = "ButtonsOk"
    Private CancelEvent As String = "ButtonsCancel"

    'Dim frm As Form
    frm.Initialize( "", 330, 160)
    frm.Title = "Title"
    frm.Resizable = False
  
    Dim btOK As Button
    btOK.Initialize( OkEvent)
    btOK.Text = "OkButton"
  
    Dim btCancel As Button
    btCancel.Initialize( CancelEvent)
    btCancel.Text = "CancelButton"

    frm.RootPane.AddNode( btOK, 100, 110,100,30)
    frm.RootPane.AddNode( btCancel, 220, 110, 100, 30)
  
    frm.Show
End Sub
Private Sub ButtonsOk_Action
    frm.Close
End Sub
Private Sub ButtonsCancel_Action
    frm.Close
End Sub
The Tab-key works but the Enter.key dont work.
I dont know why?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The buttons will fire when you press the Space bar. Have a search on the internet, it appears to be the norm.
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
steve05, you are right.
With space-bar it works.

I always thought that 'enter' the standard would be.

Thanks alot
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I always thought that 'enter' the standard would be
I guess it depends on the guidelines you follow for a particular UI and how far back you go in computing. In the DOS days, "Enter" was usually used to go from one entry field to another. In the first version of Windows, "Enter" was used to accept a form. What a pain! Do you know how many partial form entries I performed before I realized what was happening?

It looks like current Windows guidelines (here) mention:
and
but then it also gets a little wishy washy
Dialog box:
ENTER Carry out the default command of the dialog box or command of the selected control.
Gnome documentation (here):
Return Activate focused button, menu item, etc
On a Mac (here)?

And on and on. So I guess with various ways of handling the "Enter" key, JavaFX gives you the tools to handle it, but does not implement a default way of doing it. And BTW, they are guidelines which many a program has broken one way or another.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…