Hi, I wanna fire event when i specific key down while dragging widget.
i think, using SetFocus function to focus the DefaultDragVisual widget
and cast it to my widget.
but i can’t figure out what to do.
Below is a code summary.
UItemWidget::NativeOnDragDetected(...)
{
UItemwidget* DragWidget = Cast<UItemwidget>(CreateWidget<UUserWidget>(...));
DragDropOperation->DefaultDragVisual = DragWidget;
}
//called only when has focus.
UItemWidget::NativeOnKeyDown(...)
{
bool bDragging = UWidgetBlueprintLibrary::IsDragDropping();
if (bDragging && key == EKeys::R)
{
//...code...
}
}
I want to do it like the picture below.
ok, it’s not the way i wanted it, but it works in a simliar way.

just SetFocus at OtherWidget And override OnKeyDown event.
i wanted to call the OnkeyDown event directly to a dragging widget,
but it didn’t work…
the code below is a summary of the code i’ve tried.
//Itemwidget.h
UMyDDOperation = DDOper;
UItemwidget::NativeConstruct()
{
if(DDOper)
{
SetFocus();
}
}
UItemwidget::NativeOnDragDetected(....)
{
DDOper = Cast<UCustomDDOperation>(UWidgetBlueprintLibrary::CreateDragDropOperation(UCustomDDOperation::StaticClass()));
UNewItemwidget* DragWidget = CreateWidget<UNewItemwidget>(GetWorld(), this->GetClass());
check(DragWidget)
DragWidget->ItemObj = ItemObj;
DragWidget->Tilesize = Tilesize;
DDOper->DefaultDragVisual = DragWidget;
DDOper->DragWidget = DragWidget;
DDOper->ItemObj = ItemObj;
DDOper->Pivot = EDragPivot::CenterCenter;
OutOperation = DDOper;
}
UItemwidget::NativeOnKeyDown(...)
{
FKey key = InKeyEvent.GetKey();
if(DDOper)
{
// code...
}
}
oh, in Otherwidget
you need to get DragDropOperation using UWidgetBlueprintLibrary::GetDragDroppingContent() ← this function
FReply UOtherWidget::NativeOnPreviewKeyDown(...)
{
UMyDDOperation* DDOper = Cast<UMyDDOperation>(UWidgetBlueprintLibrary::GetDragDroppingContent());
if (DDOper == nullptr) return FReply::Unhandled();
UItemObject* Obj = DDOper->ItemObj;
if (InKeyEvent.GetKey() == EKeys::R)
{
Obj->ItemRotate();
UDragwidget* widget = Cast<UDragwidget>(DDOper->DefaultDragVisual);
if (widget)
{
FReply::Handled();
}
}
}
return FReply::Unhandled();
}