Skip to main content

Capturing keyPresses in a Canvas

1 reply [Last post]
robmcq
Offline
Joined: 2007-07-08
Points: 0

Optimally I'd like to be able to use onKeyTyped, onKeyPressed or onKeyReleased which are provided by javafx.scene.Node. Using the mouse handling functionality was dead simple. However, I can't figure out how to use the keyboard handling functionality.

I've tried to give the node a function, like this:

Node{ //Could be any node you desire, I actually use Polygon

focused:true

onKeyPressed:function(keyEvent){
System.out.println("{keyEvent.getKeyCode()} {keyEvent.getKeyChar()}");
}

}

When I run the program, none of my key presses are being registered by the application. Is there something that I'm neglecting to do, something that I'm forgetting?

Thanks,

Robert

P.S. I'm currently presenting my application as a javafx.swing.ext.SwingFrame if that matters at all.

I've done a few more tests and discovered that I can actually use tab to change focus to my Node objects. But that action doesn't change the "focused" parameter in the Node object nor does setting the focused parameter capture the focus. Is there any one who knows what the "focused" parameter is supposed to be used for in this context?

Message was edited by: robmcq

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
robmcq
Offline
Joined: 2007-07-08
Points: 0

I figured out the problem. Apparently setting the focused attribute of a node doesn't request the focus. For that you have to call the object method requestFocus(). See it's easy when you know how ;-).