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

jmon

Well-Known Member
Licensed User
Longtime User
I always thought that 'enter' the standard would be.
e9b82533f50538f4d36656f24bf2afb39642223033cd19d52ef1eea5b03ab1bf.jpg

( Me too! :) )
 
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:
  • Use the SPACEBAR key as the default action of a control, such as for pressing a button control or toggling the status of a check box control. This is similar to clicking the left or primary mouse button.
  • Use the ENTER key for the default action of a dialog box, if available.
and
Do not use access keys for default buttons or buttons in dialog box templates. More specifically:
  • Do not assign access keys to the OK, Finish, or Cancel command buttons. OK or Finish is typically assigned to the ENTER key for a dialog box default action, and Cancel is assigned to the ESC key.
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)?
In addition to the key view loop, a window can have a default button cell, which uses the Return (or Enter) key as its key equivalent. Programmatically, you can send setDefaultButtonCell: to an NSWindow object to set this button cell; you can also set it in Interface Builder by setting a button cell’s key equivalent to ‘\r’ in the Attributes pane of the Get Info window. The default button cell draws itself as a focal element for keyboard interface control unless another button cell is focused on. In this case, it temporarily draws itself as normal and disables its key equivalent.

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
Top