How to detect META key press during Drag and Drop on OSX
Asked Answered
H

2

8

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?

Hereinto answered 27/7, 2016 at 19:52 Comment(0)
A
1

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);
}
Agnail answered 2/8, 2016 at 21:1 Comment(4)
Just tried this. (remember, I'm working in Java 1.6, NOT 1.8) getModifiersEx() always returns a 1040. BUTTON1_MASK | BUTTON1_DOWN_MASK I tried Control, Option, Command. all keys gave the same value.Hereinto
Sorry didn't see the 1.6 in your post. I do now in the link you provided to the other issue.Agnail
yeah, I just noticed I mis-typed the version in the original question. Should have been Java 6/7, not 7/8.Hereinto
The only other way I would know how to do this is adding a KeyEventDispatcher to the KeyboardFocusManager but it looks like you already tried that. Although that will only work if your java application has the focus. If the OS has the focus then the key events won't get dispatched. Perhaps you could try to request the focus during the drag to see if that makes a difference in getting the key events during the dragAgnail
H
0

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.

Hereinto answered 4/8, 2016 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.