How to detect META key presses while dragging and dropping on OSX

In Java 6/7, there is an error in OSX where, during drag and drop operations, it ignores the META key (CMD). (The Ctrl key works fine on Windows, the Ctrl key also ignores OSX) I really need this to work.

See: Java Drag and Drop in OS X Reports Moving Instead of Copying

I tried to add a KeyEventDispatcher listener to the KeyboardFocusManager, but this is not called during the drag operation.

Also, the processKeyEvent () method of the parent JPanel is not called.

So, is there a place where I can put a hook to detect META key presses?

+8
java swing drag-and-drop keyevent macos
source share
2 answers

In DragGestureEvent you can get modifiers. e.getTriggerEvent().getModifiersEx() javadocs state:

Advanced modifiers represent the state of all modal keys, such as ALT, CTRL, META and mouse buttons immediately after the event.

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); } 
+1
source share

So, with a bunch of experiments, I found a workaround. Although none of the mouse listeners receive the Command or Ctrl modifier, the Ctrl key affects DropAction for many DragNDrop classes.

One thing we noticed was that it would work if you pressed the control key AFTER you dragged something over the target. Therefore, to provide the user with great feedback, I was able to modify my DragSourceListener and DragSourceMotionListener to (usually) update the drag and drop icon. This is unreliable on a Mac, as the mac often resets the drag and drop cursor to the default value. But at least the user can perform the Drag-Copy operation in a somewhat non-standard way with inconsistent feedback of the icons.

0
source share

All Articles