0

I managed to create a JavaFX application for Android using the client-maven-plugin from Gluon. Everything is fine so far, but I did not manage to react to the native BACK navigation on Android. I tried adding key event listeners to the scene - that works on Desktop and allows reacting to a pressed ESC key, but on Android the event handler isn't even called.

I see, that the event itself reaches the GraalVM and it looks like it is interpreted as a pressed and released BACK key:

02-17 20:46:52.937  8275  8275 V GraalActivity: Activity, process get key event, action = 0
02-17 20:46:52.937  8275  8275 I System.out: KeyEvent: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=244804749, downTime=244804749, deviceId=-1, source=0x101, displayId=0 } with action = 0
02-17 20:46:52.937  8275  8275 I System.out: [JVDBG] eventkeycode = 4 and jfxkc = ESCAPE with code 27
02-17 20:46:52.937  8275  8275 E GraalGluon: Native Dalvik layer has to dispatch key event, pass to native Graal layer with 1 chars...
02-17 20:46:52.937  8275  8275 E GraalGluon: passed count = 1 and realcount = 1
02-17 20:46:52.937  8275  8275 E GraalGluon: c0 = nd c1 = s
02-17 20:46:52.937  8275  8275 E GraalGluon: c0 = 1b and c1 = 7473
02-17 20:46:53.003  8275  8275 V GraalActivity: Activity, process get key event, action = 1
02-17 20:46:53.003  8275  8275 I System.out: KeyEvent: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=244804818, downTime=244804749, deviceId=-1, source=0x101, displayId=0 } with action = 1
02-17 20:46:53.003  8275  8275 E GraalGluon: Native Dalvik layer has to dispatch key event, pass to native Graal layer with 1 chars...
02-17 20:46:53.003  8275  8275 E GraalGluon: passed count = 1 and realcount = 1
02-17 20:46:53.003  8275  8275 E GraalGluon: c0 = nd c1 = s
02-17 20:46:53.003  8275  8275 E GraalGluon: c0 = 1b and c1 = 7473

My current attempt to capture that keyboard input was

scene.setOnKeyPressed(ev -> {
    System.out.println("onKeyPressed: "+ev.getCode() +" bzw. "+ev.getCharacter());
});

but judging from the logs it never gets called.

So: What do I need to do, to handle this event?

taranion
  • 631
  • 1
  • 6
  • 17
  • 1
    `ESCAPE` key is processed when Android's Back button is pressed. If you add `scene.addEventFilter(KeyEvent.ANY, e -> System.out.println("key = " + e.getCode() + " " + e.getEventType()));` you should see both `KEY_PRESSED` and `KEY_RELEASED`. If you try the Gluon samples, like [Notes](https://github.com/gluonhq/gluon-samples/tree/master/notes), you will see it works to navigate from views or quit the app. – José Pereda Feb 17 '21 at 20:35
  • I've got it working now. The example with the EventFilter was a good one, because it helped me the real problem was a PEBKAC - I missed that the code I changed wasn't in the main application but in a library I did not automatically compile. Oh well. – taranion Feb 17 '21 at 22:39

0 Answers0