当拖动手势时,JavaFX KeyEvent不会被激活 [英] JavaFX KeyEvent not raised when dragging gesture active

查看:148
本文介绍了当拖动手势时,JavaFX KeyEvent不会被激活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So my issue is this, I'm implementing a UI creation tool that requires elements to be resized using drag gestures on the edges. The problem arises when doing this drag while checking a modifier key is being pressed, i.e. to implement uniform scaling.The key event is never raised while the drag gesture is active so i cannot activate/deactivate this state during a drag, which is obviously not ideal! My question is, is there some reason in JavaFX why this is the case? Is there any workarounds? Can I access the raw keyboard state, i.e. without event callbacks?

推荐答案

这似乎对我来说很好:

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);
    }
}

这篇关于当拖动手势时,JavaFX KeyEvent不会被激活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆