Reputation: 1283
There is a bug in Java 6/7 on OSX where during Drag and Drop operations, it ignores the META (CMD) key. (Ctrl key works just fine on Windows, Ctrl key is also ignores on OSX) I REALLY need to have this working.
See: Java Drag and drop on OS X reports Move instead of Copy
I tried adding a KeyEventDispatcher listener to the KeyboardFocusManager, but that isn't called during a Drag operation.
Nor does the processKeyEvent() method of the parent JPanel ever get invoked.
So, is there any place where I can put a hook to detect META key presses?
Upvotes: 8
Views: 705
Reputation: 1283
So, with a bunch of experimentation, I've kind-of found a workaround. While none of the mouse listeners receives the Command or Ctrl key modifier, the Ctrl key affects the DropAction for many of the DragNDrop classes.
One thing we noticed was that it would work IF you pressed the control key AFTER you had dragged something over the drop target. So to provide the user with a big more feedback, I was able to modify my DragSourceListener, and DragSourceMotionListener to (usually) update the drag icon. It's unreliable on the mac, as the mac frequently resets the drag cursor to the default. But at least the user CAN do a Drag-Copy operation, in a somewhat non-standard way, with inconsistent icon feedback.
Upvotes: 0
Reputation: 405
On the DragGestureEvent you can get the modifiers. e.getTriggerEvent().getModifiersEx()
javadocs state:
Extended modifiers represent the state of all modal keys, such as ALT, CTRL, META, and the mouse buttons just after the event occurred.
This code worked for me on OSX:
public void dragGestureRecognized(DragGestureEvent e)
{
boolean isMetaDown = InputEvent.META_DOWN_MASK == (e.getTriggerEvent().getModifiersEx() & InputEvent.META_DOWN_MASK));
System.out.println("metaDown:"+isMetaDown);
}
Upvotes: 1