Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8264172

[macos11] modifier keys are ignored when Drag and Drop a file from finder

    XMLWordPrintable

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      MacOs Big Sur 11.2.3 on an Intel MacBook
      JavaFX 16+8
      JDK 15.0.1+9

      A DESCRIPTION OF THE PROBLEM :
      On MaxOSX modifier keys are ignoed when dragging and dropping a file from Finder. No matter if I hold cmd or option + cmd while dragging a file on a javafx application, I always receive TransferMode.Copy in the event TransferMode Move and Link are never output.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Drag a file on the drop target with no modifier
      2. Drag a file on the drop target with option key pressed
      3. Drag a file on the drop target with option + cmd key pressed



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      1. Output: Copy
      2. Output: Move
      3. Output: Link
      ACTUAL -
      1. Output: Copy
      2. Output: Copy
      3. Output: Copy

      ---------- BEGIN SOURCE ----------
      Example adapted from https://docs.oracle.com/javafx/2/drag_drop/HelloDragAndDrop.java.html
      public class DragAndDropTest 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);

              source.setOnDragDetected(new EventHandler <MouseEvent>() {
                  @Override
                  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>() {
                  @Override
                  public void handle(DragEvent event) {

                      if ((event.getGestureSource() != target) &&
                              event.getDragboard().hasFiles()) {
                          event.acceptTransferModes(TransferMode.ANY);
                      }

                      event.consume();
                  }
              });

              target.setOnDragEntered(new EventHandler <DragEvent>() {
                  @Override
                  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>() {
                  @Override
                  public void handle(DragEvent event) {
                      /* mouse moved away, remove the graphical cues */
                      target.setFill(Color.BLACK);

                      event.consume();
                  }
              });

              target.setOnDragDropped(new EventHandler <DragEvent>() {
                  @Override
                  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.hasFiles()) {
                          System.out.println(event.getTransferMode());

                      }
                      /* let the source know whether the string was successfully
                       * transferred and used */
                      event.setDropCompleted(success);

                      event.consume();
                  }
              });

              source.setOnDragDone(new EventHandler <DragEvent>() {
                  @Override
                  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);
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            aghaisas Ajit Ghaisas
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated: