Astinog
Astinog

Reputation: 1181

KeyEvents for AnchorPane don't work

I want to handle the CRTL-Z key combination. I have a Main class that extends Stage and inside it I have an AnchorPane I'd like to add to that AnchorPane an EventHandler for a KeyPressed event, but if I do

_anchorPane.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>()
                {
                    @Override
                    public void handle(KeyEvent event)
                    {
                        System.out.println("Check");
                        KeyCombination ctrlZ = new KeyCodeCombination(KeyCode.Z, KeyCombination.CONTROL_DOWN);
                        if(ctrlZ.match(event))
                            cancelLastShape();
                    }
                });

it does nothing. I put there a print function to see if the program "enters" there, but nothing. I tryed to set _anchorPane.setFocusTraversable(true);, but it doesn't change anything. What can I do about it?

Upvotes: 1

Views: 1115

Answers (1)

Thomas
Thomas

Reputation: 170

Try to put the listener on the scene : _anchorPane.getScene().addEventHandler();

Upvotes: 2

Related Questions