Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling keyboard events

 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
John Damien Smith
Ranch Hand
Posts: 296
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
ganesh swami
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To handle JavaFX KeyEvent there are many methods one is setOnKeyPressed() , use it to handle the key event fo type key-pressed. As explained in JavaFX KeyEvent http://javafxtuts.com/javafx-keyevent/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic