我正在尝试使用JavaFX开发Android应用。 有没有办法在我的手机上按下音量按钮,这样每当我按下音量按钮时我都可以启动一些代码?
答案 0 :(得分:0)
您可以收听各种密钥代码,例如
KeyCode.ESCAPE
==后退按钮KeyCode.VOLUME_UP
/ KeyCode.VOLUME_DOWN
KeyCode.HOME
- 我不确定,还没有测试过它KeyCode.CONTEXT_MENU
- 我不确定,还没有测试过它在Application-Extending Main类中,您可以执行以下操作:
@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();
}
}
此致 丹尼尔