B4J Question Action event triggered by mouse click, but not by Enter

B4JExplorer

Active Member
Licensed User
Hi,

Final question for the day.

I have a series of 6 buttons defined, horizontally. They are the first objects on the screen. So the first button is highlighted, as soon as the form is run.

The arrow keys move from button to button. But when any one of the buttons is selected and you click <Enter>, the btn_Action event is not triggered.

If you do a Mouse Click, then the btn_MouseClicked event is properly triggered, followed by the btn_Action event.

I'd like the _Action event to be triggered, when pressing Enter on a button. How do we do that?
 

B4JExplorer

Active Member
Licensed User
Don't use the Action event. Use Click instead. Same behavior but cross platform.

The click event is triggered when the user presses on the space bar. You will need to do some work to handle the enter key. You can use jReflector to intercept key presses and then call the button's click sub.
Ok, thanks for saving me some time. I'll take a look.

Regards,
 

B4JExplorer

Active Member
Licensed User
Looks good, thanks Erel.

And btn_Action works well, also. But you're saying it's better to focus on the Click event, because the same code snippets will work in Android and IOS. So we'll keep _Action out of the project.

Btw, for anyone who is as dense as I am, and you often takes hours or days to get past very obvious oversights/syntax errors - DON'T FORGET to Add "_Event" to the end of the event name. In other words, if you use AddEventHandler to register "object_Click", then the name of the actual event has to be

B4X:
Sub object_Click_Event( e As Event )
If you DON'T do this, you will be pulling your hair out, wondering why your elegant little event is being neglected (wah!) by the rest of the application.


One odd quirk is that the event seems to fire two times, for a single ENTER key pressed. At least it seems that way, according to the Log() output.

I can live with that, but if anyone has a suggestion to limit the event firing to one, that would help.


B4X:
Sub AppStart (Form1 As Form, Args() As String)
'...
'...
'...
	obj_Reflector.AddEventHandler( "btn_Click", "javafx.scene.input.KeyEvent.ANY" )
'...
'...
'...
End Sub


Sub btn_Click_Event( e As Event )
	Dim KE As Reflector
	KE.Target = e
	Dim KeyCode As String
	KeyCode = KE.RunMethod("getCode")
	If KeyCode = "ENTER" Then
		Log(KeyCode)
	End If
End Sub
 
Top