I started playing around with JavaFX over the weekend, and I'm exploring the capabilities of collision detection. I want to setup a program where I can either click and drag a shape around a scene, or use the keyboard to move the shape around, and detect collisions. I can handle the mouse event and see the shape moving around, but when I add an EventHandler<KeyEvent> to the scene at the same point as my mouse handler it's not getting fired.
I thought my problem might be that I need to not attach the event handler to a node, maybe to the root or something, but I found Oracle's example of a keyboard and they are attaching events to scene graph objects.
There must be something obvious I'm doing wrong here. Can anyone give me a hand by pointing it out?
I created a custom shape (just two circles joined by a line) and a scene that contains that shape + another shape for it to collide with.
Then I create my scene somewhat like so:
Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
The way the key event processing works is that key events are routed to the focused node. If no node has focus, then no node will receive the key events and if no node is focus traversable then no node can be focused.
Most controls are set to be focus traversable by default, but other node types (like shapes) are not focus traversable by default (though you can set them to be focus traversable and request focus for them if you wish).
Note, you can also define a key handler on a scene and that will be called if nothing in the scene consumes the event. Or, you can add an event filter for key events to the scene and that will get called regardless of whether something in the scene consumes the events.
The key handler on a scene seems to be working for me. I tried a couple of the other techniques you mentioned and I'm having trouble getting those to work. I can work with the scene key handler though and I guess I'll just come back to the other issues later when I want to explore more. Thanks!