2

So I started playing around with setting up key for different functions in my application and I was wondering is there a way to set the right/left mouse click to do something? I was not able to find any mouse events in the KeyCode.java And I am not sure how to application that.

Here is my code:

public class Exercise_31 extends Application {
 @Override
 public void start(Stage primaryStage) throws Exception {
 PendulumPane pendulumPane = new PendulumPane(500, 500);
 Scene scene = new Scene(pendulumPane);
 primaryStage.setTitle("Pendulum Animation");
 primaryStage.setScene(scene);
 pendulumPane.play();
 primaryStage.show();
 scene.setOnKeyPressed(e-> {
 switch (e.getCode()) {
 case UP: pendulumPane.increase(); break;
 case DOWN: pendulumPane.decrease(); break;
 case LEFTMOUSECLICK? 
 }
 });
 }
 public static void main(String[] args) {
 Application.launch(args);
 }
 private class PendulumPane extends Pane {
 private double w = 400;
 private double h;
 PathTransition bPath;
 Circle topC;
 Circle lowerC;
 Line line;
 Arc arc;
 PendulumPane(double width, double height) {
 w = width;
 h = height;
 setPrefWidth(w);
 setPrefHeight(h);
 arc = new Arc(w / 2, h * 0.8, w * 0.15, w * 0.15, 180, 180);
 arc.setFill(Color.TRANSPARENT);
 arc.setStroke(Color.BLACK);
 lowerC = new Circle(arc.getCenterX() - arc.getRadiusX(), arc.getCenterY(), 10);
 topC = new Circle(arc.getCenterX(), arc.getCenterY() - h / 2, lowerC.getRadius() / 2);
 arc = new Arc(topC.getCenterX(), topC.getCenterY(), w / 2, h / 2, 240, 60);
 line = new Line(
 topC.getCenterX(), topC.getCenterY(),
 lowerC.getCenterX(), lowerC.getCenterY());
 line.endXProperty().bind(lowerC.translateXProperty().add(lowerC.getCenterX()));
 line.endYProperty().bind(lowerC.translateYProperty().add(lowerC.getCenterY()));
 bPath = new PathTransition();
 bPath.setDuration(Duration.millis(4000));
 bPath.setPath(arc);
 bPath.setNode(lowerC);
 bPath.setOrientation(PathTransition.OrientationType.NONE);
 bPath.setCycleCount(PathTransition.INDEFINITE);
 bPath.setAutoReverse(true);
 getChildren().addAll(lowerC, topC,line);
 }
 public void play() {
 bPath.play();
 }
 public void increase() {
 bPath.setRate(bPath.getCurrentRate() + 1);
 }
 public void decrease() {
 bPath.setRate(bPath.getCurrentRate() - 1);
 }
 public void stop() {
 bPath.setRate(bPath.getCurrentRate() * 0);
 }
 public void start() {
 bPath.setRate(bPath.getCurrentRate() + 1);
 }
 }
}

What I am trying to accomplish is to make right clicking your mouse button stop the movement and left clicking start it again. How would I do that? Thank you.

Ross Drew
8,2563 gold badges44 silver badges53 bronze badges
asked Dec 17, 2015 at 14:32
3
  • 1
    The mouse isn't keyboard input. You need to use the setOnMousePressed (or similar) method to capture mouse clicks. Commented Dec 17, 2015 at 14:36
  • 1
    Possible duplicate of Java Mouse Event Right Click Commented Dec 17, 2015 at 14:40
  • 2
    @Blip that question and its answers are all about Swing though, not JavaFX. Commented Dec 17, 2015 at 14:45

1 Answer 1

3

Your event is a KeyEvent. If you define a listener on for the mouse (e.g. setOnMouseReleased you get a MouseEvent and on that one you have functions to check if certain keys are pressed that are usually combined with a mouse click:

event.isAltDown();
event.isControlDown();
event.isMetaDown();

If you need something more you will have to implement it yourself. See the JavaDoc for MouseEvent.

jewelsea
160k15 gold badges382 silver badges425 bronze badges
answered Dec 17, 2015 at 14:39
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.