How to cancel drag-and-drop with ESC key?


#1

Apologies if this is obvious but I couldn't find it anywhere.  How can I allow the user to press ESC to cancel a drag-and-drop operation (with DragAndDropContainer and DragAndDropTarget)?


#2

Coincidentally, I just hit this same requirement for tracktion, so will figure something out v. soon!


#3

Is there a way to achieve this behavior currently?


#4

It works already doesn’t it?
The only time I’ve not seen it work is with the standalone target for a plugin, where you need to have the file over the app window for Escape to work (on macOS 10.12 at least).


#5

Not sure about the file drag and drop, but we’re using DragAndDropContainer and DragAndDropTarget for re-ordering UI items and there’s no way to cancel a drag once you start it (without just dropping the item back in the same location or off to the side)


#6

Could you listen for an esc keypress and use that to toggle what the result of isInterestedInDragSource would be?


#7

I havre the same problem. Using the Escape key to cancel a drag needs to be implemented as part of the framework. It is default behaviour that just must be there. Imaging you start a drag accidentally and all areas you could drop it at have a meaning and perform an an operation.


#8

I have the same problem. Using the Escape key to cancel a drag needs to be implemented as part of the framework. It is default behaviour that just must be there. Imaging you start a drag accidentally and all areas you could drop it at have a meaning and perform an an operation.


#9

There is one tricky workaround to cancel a drag when using DragAndDropContainer and DragAndDropTarget: generate a mouse up event programatically when some key is pressed (for example Esc). On Windows there is SendInput function (I did not implement this workaround on Mac but should be something similar). You have also to inform your components to ignore itemDropped otherwise this fake mouse up event will cause a regular drop.

#include <windows.h>
#include <iostream>

void simMouseLeftUp()
{
	INPUT Input = { 0 };
	Input.type = INPUT_MOUSE;
	Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
	SendInput(1, &Input, sizeof(INPUT));
};

#10

That’s why it needs to be handled internally. The OS does it this way.