Search code examples
javadrag-and-dropkeypress

detect key press during java dnd


I am trying to detect whether a key, e.g. control key, is pressed in java DropTargetDropEvent of dropping an OS file. Due to the focus change from OS file browser to application, my understanding is key detection without focus may be needed. Some said it is hard or impossible without focus, while some posted solutions and have been accepted. I tried the accepted solution, but as one comment said, it never returns true for key press. Anyone has any idea?

I also tried to use getDropAction of DropTargetDropEvent to detect control key. Under Windows and MAC I tested, it returns ACTION_COPY if control is down, and ACTION_MOVE if neither control nor shift is down. Unfortunately it returns ACTION_COPY under Linux no matter control key is down or not. Any idea to override the drop actions supported by the drag source so getDropAction won't return ACTION_COPY under Linux when control key is not down?

Any idea and suggestion are welcome.


Solution

  • I posted this question for months, but there is no answer. The problem was, during DropTargetDropEvent under Linux, getDropAction gives ACTION_COPY no matter Control key is down or not.

    Today I just figured out a hack to detect ControlDown under Linux. The solution is to use java.awt.Robot to press Shift key in DragEnterCallback, and release the key in DropCallback or DragExitCallback if user revokes the drop. Then in DropCallback, getDropAction will give either ACTION_LINK or ACTION_MOVE depending on Control is down or not.

    Actually this is an OS-independent solution to detect Control key during drop event, although there is easier way for Windows/MAC.

    If one likes OS-independent method to detect Shift key, similar method can be used to robot-press Control in DragEnterCallback.