2016-11-13 3 views
0

javaFY 프로젝트에서 키 누름 수신기를 전체 창에 추가하려고합니다.키 누름 이벤트가 실행되지 않습니다.

<VBox onKeyPressed="#windowKeyPressed" fx:controller="hu.kleni.tetris.EventController" ...> 

그리고 HTE 이벤트 핸들러 클래스 : 윈도우의 FXML 파일의 루트 노드입니다 main() 방법에서

public class EventController { 
    @FXML 
    public void windowKeyPressed(KeyEvent event) { 
     System.out.println(event.getCode()); 
    } 
    ... 
} 

, 그것은 단지로드하고 창을 시작합니다. 프로그램을 시작하면 창이 나타나지만 키를 누르면 콘솔에서 아무 것도 볼 수 없습니다. 내가 뭐 놓친 거 없니?

편집 : 나는 이것을 사용할 수있다 (그리고 잘 작동) 만 :

scene.setOnKeyPressed((event) -> { 
    // maybe call EventController.windowKeyPressed(event); 
}) 

, 나는 단지 FXML 파일의 모든 이벤트 핸들러를 정의하기 원합니다.

답변

1

onKeyPressed에 초점을 맞추려면 root (VBox)이 필요합니다. 당신의 rootStage가 표시 한 후, 예컨대 :

@Override 
public void start(Stage stage) throws Exception { 
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));   
    Scene scene = new Scene(root); 
    stage.setScene(scene); 
    stage.show(); 
    root.requestFocus(); // add this, root is the VBox in your case 
} 
에 당신의 Application 클래스에서

, requestFocus()

관련 문제