• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Handling keyboard events

 
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Rancher
Posts: 387
30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic