JDK-8273379 : GTK3 stops sending key events during drag and drop
  • Type: Bug
  • Component: javafx
  • Sub-Component: window-toolkit
  • Affected Version: openjfx11,openjfx16,openjfx17
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: linux
  • CPU: x86_64
  • Submitted: 2021-08-31
  • Updated: 2022-12-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Tested with JavaFX 16, JDK 16 on Ubuntu 20.04 and Fedora 34.

A DESCRIPTION OF THE PROBLEM :
KeyEvents used to be sent during drag and drop so modifier keys could be used to specify copy or move actions.
KeyEvents are sent in JavaFX 13, stops in 14 and later. Most likely related to this change: https://bugs.openjdk.java.net/browse/JDK-8225571 - "Port Linux glass drag source (DND) to use gtk instead of gdk".


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a draggable image.
2. Create an event filter on stage that prints key events.
3. Press keys before and during dragging of image.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Key events printed while dragging image.
ACTUAL -
Key events are not printed while dragging image.

---------- BEGIN SOURCE ----------
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * Example code for JavaFX bug "GTK3 stops sending key events during drag and drop".
 * KeyEvents used to be sent during drag and drop so modifier keys could be used to specify copy or move actions.
 *
 * KeyEvents are sent in JavaFX 13, stops in 14 and later.
 * Most likely related to this change: https://bugs.openjdk.java.net/browse/JDK-8225571 - "Port Linux glass drag source
 * (DND) to use gtk instead of gdk".
 *
 * Workaround:
 * No workaround found for GTK3 that works with JavaFX 14 or later.
 * #1: Use JavaFX 13.
 * #2: Use GTK2 at runtime with JVM argument "-Djdk.gtk.version=2".
 *
 * Tested with JavaFX 16, JDK 16 on Ubuntu 20.04 and Fedora 34.
 */
public class Main extends Application {

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

    @Override
    public void start(Stage stage) {
        Text text = new Text("Instructions:\n" +
                "1. Press and release any key.\n" +
                "2. Notice key events appearing in text area.\n" +
                "3. While dragging image, press any key.\n" +
                "4. Notice that no key events appear while dragging.");
        Image image = createImage(180, 180);
        ImageView imageView = new ImageView(image);
        imageView.setOnDragDetected(event -> {
            ClipboardContent content = new ClipboardContent();
            content.putImage(image);
            Dragboard dragboard = imageView.startDragAndDrop(TransferMode.ANY);
            dragboard.setContent(content);
            dragboard.setDragView(image);
        });
        TextArea textArea = new TextArea("KeyEvent log:\n");
        textArea.setEditable(false);

        VBox vBox = new VBox(text, imageView, textArea);
        vBox.setSpacing(5.0);
        vBox.setAlignment(Pos.BOTTOM_CENTER);
        stage.setScene(new Scene(vBox, 480, 480));
        stage.setTitle("KeyEvents Stop During Drag & Drop (GTK3)");
        stage.addEventFilter(KeyEvent.KEY_PRESSED, event ->
                textArea.appendText(event.getEventType().getName() + ": " + event.getCode().getName() + "\n"));
        stage.addEventFilter(KeyEvent.KEY_RELEASED, event ->
                textArea.appendText(event.getEventType().getName() + ": " + event.getCode().getName() + "\n"));
        stage.show();
    }

    private static Image createImage(int width, int height) {
        WritableImage image = new WritableImage(width, height);
        PixelWriter pixelWriter = image.getPixelWriter();
        for (int y=0; y<height; y++) {
            for (int x=0; x<width; x++) {
                pixelWriter.setColor(x, y, Color.GREEN);
            }
        }
        return image;
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
No workaround found for GTK3 that works with JavaFX 14 or later.
#1: Use JavaFX 13.
#2: Use GTK2 at runtime with JVM argument "-Djdk.gtk.version=2".

FREQUENCY : always



Comments
A pull request was submitted for review. URL: https://git.openjdk.org/jfx/pull/986 Date: 2022-12-27 13:19:06 +0000
27-12-2022

Is this really a bug? I don't understand why the user would send key events during a drag if not to change the drag behaviour between MOVE/COPY/LINK. The MOVE/COPY/LINK is working. Tested with: java @build/run.args -cp apps/toys/DragDrop/dist/DragDrop.jar dragdrop.DragDropWithControls
12-09-2021

Checked with attached testcase in Ubuntu 20.04, while image is dragged, no key event is observed in text area, Test Result ======== 8u301: Pass openjfx11-(11.0.2),(11.0.12).: Fail openjfx16: Fail openjfx17ea15: Fail with -Djdk.gtk.version=2, while image is dragged, key event is observed in text area,
06-09-2021