Issue
I have an existing application that I use to move a 3 d plot of star patterns around (translate, rotate, etc.)
I have the mouse events (press, drag, etc) working fine.
I wanted to added a keyboard event handler, but the odd thing is the it never catches any of the key press events.
I define a subscene to manage things. Here is the event setup and processing code. In the below code, all the mouse events work exactly as expected. No key press events are logged.
private void handleUserEvents() {
subScene.setOnKeyPressed((KeyEvent key)-> {
log.info("key={}", key);
});
subScene.setOnScroll((ScrollEvent event) -> {
double deltaY = event.getDeltaY();
zoomGraph(deltaY * 5);
updateLabels();
});
subScene.setOnMousePressed((MouseEvent me) -> {
mousePosX = me.getSceneX();
mousePosY = me.getSceneY();
mouseOldX = me.getSceneX();
mouseOldY = me.getSceneY();
}
);
subScene.setOnMouseDragged((MouseEvent me) -> {
int direction = userControls.isControlSense() ? +1 : -1;
mouseOldX = mousePosX;
mouseOldY = mousePosY;
mousePosX = me.getSceneX();
mousePosY = me.getSceneY();
mouseDeltaX = (mousePosX - mouseOldX);
mouseDeltaY = (mousePosY - mouseOldY);
// double modifier = 0.2;
double modifier = UserControls.NORMAL_SPEED;
if (me.isPrimaryButtonDown() && me.isControlDown()) {
log.info("shift sideways");
camera.setTranslateX(mousePosX);
camera.setTranslateY(mousePosY);
} else if (me.isPrimaryButtonDown()) {
if (me.isAltDown()) { //roll
rotateZ.setAngle(((rotateZ.getAngle() + direction * mouseDeltaX * modifier) % 360 + 540) % 360 - 180); // +
} else {
rotateY.setAngle(((rotateY.getAngle() + direction * mouseDeltaX * modifier) % 360 + 540) % 360 - 180); // +
rotateX.setAngle(
clamp(
(((rotateX.getAngle() - direction * mouseDeltaY * modifier) % 360 + 540) % 360 - 180),
-60,
60
)
); // -
}
}
updateLabels();
}
);
}
Solution
So in the question above. I played around with things and found an answer that seems to work. I don't like it much but it works so...
I added one line to the below code "subScene.requestFocus();" This forces the focus and makes it work.
subScene.setOnMousePressed((MouseEvent me) -> {
mousePosX = me.getSceneX();
mousePosY = me.getSceneY();
mouseOldX = me.getSceneX();
mouseOldY = me.getSceneY();
subScene.requestFocus();
}
);
Answered By - EvilJinious1