拖动手势活动时不会引发JavaFX KeyEvent

时间:2014-04-30 22:39:31

标签: java events javafx mouseevent keyevent

所以我的问题是这个,我正在实现一个UI创建工具,它需要使用边缘上的拖动手势来调整元素大小。在按下检查修改键的同时执行此拖动时出现问题,即实现均匀缩放。当拖动手势处于活动状态时,永远不会引发按键事件,因此在拖动过程中我无法激活/停用此状态,这显然不是理想!我的问题是,JavaFX中有什么理由为什么会这样?有没有解决方法?我可以访问原始键盘状态,即没有事件回调吗?

1 个答案:

答案 0 :(得分:0)

这对我来说似乎很合适:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tooltip;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class DraggableStretchableNodeExample extends Application {
    @Override
    public void start(Stage primaryStage) {
        Pane root = new Pane();

        Rectangle rect = new Rectangle(50, 50, 50, 50);
        rect.setFill(Color.CORNFLOWERBLUE);

        Tooltip tooltip = new Tooltip("Drag to move, Z+Drag to stretch");
        Tooltip.install(rect, tooltip);

        root.getChildren().add(rect);

        enableDragging(rect);

        Scene scene = new Scene(root,400,400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void enableDragging(Rectangle rect) {

        // A mutable struct-like nested class for the last known mouse location:
        class MouseLocation { 
            double x, y ; 
            void set(double x, double y) {
                this.x=x ;
                this.y=y ;
            }
        }
        MouseLocation mouseLocation = new MouseLocation();

        // Another for the 'Z' key state:
        class Zstate {
            boolean pressed ;
        }
        Zstate zState = new Zstate();

        rect.setOnMousePressed(event -> mouseLocation.set(event.getX(), event.getY()));
        rect.setOnMouseDragged(event -> {
            if (zState.pressed) {
                stretch(rect, event.getX(), event.getY(), event.getX()-mouseLocation.x, event.getY()-mouseLocation.y);
            } else {
                move(rect, event.getX()-mouseLocation.x, event.getY()-mouseLocation.y);
            }
            mouseLocation.set(event.getX(), event.getY());
        });

        rect.setFocusTraversable(true);
        rect.setOnKeyPressed(event -> {
            if (event.getCode() == KeyCode.Z) {
                zState.pressed = true ;
            }
        });
        rect.setOnKeyReleased(event -> {
            if (event.getCode() == KeyCode.Z) {
                zState.pressed = false ;
            }
        });

    }

    private void stretch(Rectangle rect, double x, double y, double deltaX, double deltaY) {

        if (x < rect.getX() + rect.getWidth() / 2) {
            // if the mouse is in the left half, stretch left by adjusting x and width:
            rect.setX(rect.getX() + deltaX);
            rect.setWidth(rect.getWidth() - deltaX);
        } else {
            // if in the right half, just stretch by adjusting the width:
            rect.setWidth(rect.getWidth() + deltaX);
        }

        // same vertically:
        if (y <rect.getY() + rect.getHeight() / 2) {
            rect.setY(rect.getY() + deltaY);
            rect.setHeight(rect.getHeight() - deltaY);
        } else {
            rect.setHeight(rect.getHeight() + deltaY);
        }

    }

    private void move(Rectangle rect, double deltaX, double deltaY) {
        rect.setX(rect.getX()+deltaX);
        rect.setY(rect.getY()+deltaY);
    }

    public static void main(String[] args) {
        launch(args);
    }
}