JavaFX:在Drag& Drop期间处理KeyEvents?

时间:2017-03-15 07:44:45

标签: javafx drag-and-drop keyevent

我有一个使用drag& amp;的JavaFX应用程序drop(如this tutorial中所述)。此应用程序还使用KeyEvent过滤器在按下或释放某个键时更改其状态。

例如,我使用this example from the official Drag & Drop tutorial并在start方法中添加以下处理程序:

public class HelloDragAndDrop extends Application {

    // ...

    @Override public void start(Stage stage) {

        // ...

        stage.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
            if (event.getCode() == KeyCode.R)
                scene.setFill(Color.RED);
        });

        stage.addEventFilter(KeyEvent.KEY_RELEASED, event -> {
            if (event.getCode() == KeyCode.R)
                scene.setFill(Color.LIGHTGREEN);
        });

        // ...
    }

    // ...
}

一般情况下,当我按下R键时,背景会变为红色,当我释放它时,它会再次变为绿色。但问题就出现了:在拖动过程中没有调度KeyEvent。放下行动。假设以下用户操作:

  1. 用户按下R - >背景变红
  2. 用户开始拖动
  3. 用户发布R - >背景仍然是红色
  4. 用户完成拖动 - >背景仍然是红色
  5. 恢复常规颜色的唯一方法是再次按下并释放R.

    主要问题:在拖放操作过程中有没有办法接收这些KeyEvent?

    WorkAroundQuestion:如果我必须处理这些KeyEvent可能会丢失" - 有没有办法可以做boolean isKeyDown(KeyCode keyCode)这样的事情,所以我可以在拖放操作完成后至少调整应用程序的状态?

    (P.S。:这与that question不是同一个问题,因为这是关于拖拽和放弃,而这个问题是关于一般的拖动手势。)

1 个答案:

答案 0 :(得分:0)

我会把它写成评论,但遗憾的是我缺乏声誉 - 使用以下代码,我可以执行您建议的操作序列,但在步骤3中,后台切换回绿色。这已经解决了你的问题吗?我想它只是与事件处理程序的位置有关。什么是行不通的是开始拖动和按r,因为如果我这样做背景保持原样。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * Demonstrates a drag-and-drop feature.
 */
public class HelloDragAndDrop extends Application {

    @Override public void start(Stage stage) {
        stage.setTitle("Hello Drag And Drop");

        Group root = new Group();
        Scene scene = new Scene(root, 400, 200);
        scene.setFill(Color.LIGHTGREEN);

        final Text source = new Text(50, 100, "DRAG ME");
        source.setScaleX(2.0);
        source.setScaleY(2.0);

        final Text target = new Text(250, 100, "DROP HERE");
        target.setScaleX(2.0);
        target.setScaleY(2.0);

        stage.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
            if (event.getCode() == KeyCode.R)
                scene.setFill(Color.RED);
        });

        stage.addEventFilter(KeyEvent.KEY_RELEASED, event -> {
            if (event.getCode() == KeyCode.R)
                scene.setFill(Color.LIGHTGREEN);
        });

        source.setOnDragDetected(new EventHandler <MouseEvent>() {
            public void handle(MouseEvent event) {
                /* drag was detected, start drag-and-drop gesture*/
                System.out.println("onDragDetected");

                /* allow any transfer mode */
                Dragboard db = source.startDragAndDrop(TransferMode.ANY);

                /* put a string on dragboard */
                ClipboardContent content = new ClipboardContent();
                content.putString(source.getText());
                db.setContent(content);

                event.consume();
            }
        });

        target.setOnDragOver(new EventHandler <DragEvent>() {
            public void handle(DragEvent event) {
                /* data is dragged over the target */
                System.out.println("onDragOver");

                /* accept it only if it is  not dragged from the same node 
                 * and if it has a string data */
                if (event.getGestureSource() != target &&
                        event.getDragboard().hasString()) {
                    /* allow for both copying and moving, whatever user chooses */
                    event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                }

                event.consume();
            }
        });

        target.setOnDragEntered(new EventHandler <DragEvent>() {
            public void handle(DragEvent event) {
                /* the drag-and-drop gesture entered the target */
                System.out.println("onDragEntered");
                /* show to the user that it is an actual gesture target */
                if (event.getGestureSource() != target &&
                        event.getDragboard().hasString()) {
                    target.setFill(Color.GREEN);
                }

                event.consume();
            }
        });

        target.setOnDragExited(new EventHandler <DragEvent>() {
            public void handle(DragEvent event) {
                /* mouse moved away, remove the graphical cues */
                target.setFill(Color.BLACK);

                event.consume();
            }
        });

        target.setOnDragDropped(new EventHandler <DragEvent>() {
            public void handle(DragEvent event) {
                /* data dropped */
                System.out.println("onDragDropped");
                /* if there is a string data on dragboard, read it and use it */
                Dragboard db = event.getDragboard();
                boolean success = false;
                if (db.hasString()) {
                    target.setText(db.getString());
                    success = true;
                }
                /* let the source know whether the string was successfully 
                 * transferred and used */
                event.setDropCompleted(success);

                event.consume();
            }
        });

        source.setOnDragDone(new EventHandler <DragEvent>() {
            public void handle(DragEvent event) {
                /* the drag-and-drop gesture ended */
                System.out.println("onDragDone");
                /* if the data was successfully moved, clear it */
                if (event.getTransferMode() == TransferMode.MOVE) {
                    source.setText("");
                }

                event.consume();
            }
        });

        root.getChildren().add(source);
        root.getChildren().add(target);
        stage.setScene(scene);
        stage.show();
    }

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