B4J Question Button btn_KeyPressed event triggered twice in a row

B4JExplorer

Active Member
Licensed User
Longtime User
Hi,

In order to intercept the ENTER key for a button, as well as the LEFT and RIGHT arrow keys, they are intercepted thus:

B4X:
		' obj_Reflector.AddEventHandler( "btn_Click", "javafx.scene.input.KeyEvent.ANY" )
		obj_Reflector.AddEventHandler( "btn_KeyPressed", "javafx.scene.input.KeyEvent.ANY" )

The problem is that the btn_KeyPressed_Event is being triggered twice for any click or keypress. So if you press ENTER, it's triggered two times. If you click LEFT or RIGHT arrow, same thing.

As it turns out, this is ALSO true if you comment out the keypressed event above, and uncomment the click event. In that case, the btn_Click_Event gets triggered twice.

Does anyone have suggestions about how to avoid this, or to ignore the second trigger somehow? I'd hate to use a global counter, and check whether this is the second time triggered in order to ignore it.

Thanks,
 

Daestrum

Expert
Licensed User
Longtime User
It is the KeyEvent.ANY that is causing this, it is being triggered on KEY_PRESSED and KEY_RELEASED.

Change it to KEY_TYPED, this will trigger once when a key is pressed then released.
 
Upvote 0

B4JExplorer

Active Member
Licensed User
Longtime User
It is the KeyEvent.ANY that is causing this, it is being triggered on KEY_PRESSED and KEY_RELEASED.

Change it to KEY_TYPED, this will trigger once when a key is pressed then released.

Daestrum, this is another one of those mysteries that I would not have figured out for days.

Unfortunately, the keycode returns UNDEFINED, rather than ENTER or LEFT or RIGHT.

I'm using this to get the code:

B4X:
KeyCode = KE.RunMethod("getCode")

But I think this is the right track, thanks..
 
Last edited:
Upvote 0
Top