2016-09-07 53 views

回答

0

您可以收聽各種鍵碼,例如

  • KeyCode.ESCAPE ==後退按鈕
  • KeyCode.VOLUME_UP/KeyCode.VOLUME_DOWN
  • KeyCode.HOME - 我不知道,沒有測試它
  • KeyCode.CONTEXT_MENU - 我不知道,沒有測試

在您擴展應用程序的主類,你可以做這樣的事情:

@Override 
public void start(Stage stage) throws Exception { 
    // init scene and UI 
    scene.addEventFilter(KeyEvent.ANY, this::handleGlobalKeyEvents); 
    // do whatever you want 
} 

private void handleGlobalKeyEvents(KeyEvent event) { 
    // use a more specific key event type like 
    // --> KeyEvent.KEY_RELEASED == event.getEventType() 
    // --> KeyEvent.KEY_PRESSED == event.getEventType() 
    // without it, we would react on both events, thus doing one operation too much 
    if (event.getCode().equals(KeyCode.ESCAPE) && KeyEvent.KEY_RELEASED == event.getEventType()) { 
     if (backHandler != null) 
      backHandler.back(); 
     event.consume(); 
    } 
} 

Regards, Daniel