my dog learned polymorphism*
The moose likes JavaFX and the fly likes Handling keyboard events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JavaFX
Bookmark "Handling keyboard events" Watch "Handling keyboard events" New topic
Author

Handling keyboard events

Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 174
    
    5

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?
Thanks!

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
John Damien Smith
Ranch Hand

Joined: Jan 26, 2012
Posts: 147
    
    9
The key reason Oracle's key event handler example works is that the key nodes are focusTraversable and requestFocus is called to ensure that a node has focus.
These two things (focusable and has focus) ensure that key events are delivered to the key nodes.

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.

You might also be interested in Checking collisions of Shapes with JavaFX on StackOverflow and CharacterMovement.java which is based on the answer to the Java Ranch question on JavaFX Key Controls.
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 174
    
    5

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!
 
wood burning stoves
 
subject: Handling keyboard events