Search code examples
javafxjavafx-1

Right click in JavaFX?


How do I detect/handle a right click in JavaFX?


Solution

  • Here's one way:

    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.paint.Color;
    import javafx.scene.input.*;
    
    var r = Rectangle {
        x: 50, y: 50
        width: 120, height: 120
        fill: Color.RED
        onMouseClicked: function(e:MouseEvent):Void {
            if (e.button == MouseButton.SECONDARY) {
                println("Right button clicked");
            }
        }
    }
    
    Stage {
        title : "ClickTest"
        scene: Scene {
            width: 200
            height: 200
            content: [ r ]
        }
    }