Choose Move or Copy Mode by Ctrl Key during Drag & Drop

Is it possible to detect the status of control key in drop event so that I can copy instead of move if ctrl is being pressed?

I saw some solutions but they’re all for Framework 8, but I’m using Vaadin Flow 24.

Looks this is an open issue in Flow: Generic Drag&Drop get ctrl, shift, alt and meta key · Issue #10956 · vaadin/flow · GitHub However there is one possible workaround, you can “monkey patch” the com.vaadin.flow.component.dnd.DropEvent:

  1. Create a package com.vaadin.flow.component.dnd into your project
  2. Create DropEvent.java into that package
  3. Copy the source code of com.vaadin.flow.component.dnd.DropEvent into that class
  4. Add new fields:
    private final boolean altKey;
    private final boolean shiftKey;
    private final boolean ctrlKey;
  1. Customize the monkey patched class with the following constructor:
public DropEvent(T source, boolean fromClient,
                     @EventData("event.dataTransfer.effectAllowed") String effectAllowed, 
                     @EventData("event.altKey") boolean altKey, 
                     @EventData("event.shiftKey") boolean shiftKey, 
                     @EventData("event.ctrlKey") boolean ctrlKey) {
        super(source, fromClient);

        this.altKey = altKey;
        this.shiftKey = shiftKey;
        this.ctrlKey = ctrlKey;
// rest of the constructor...
}
  1. add getters for these new fields

In Mac and Chrome at least the shift key’s boolean status was correctly resolved in my dropTarget.addDropListener.

1 Like