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