How to prevent text entered in UEditableTextBox from triggering keyboard shortcuts (C++)

I’m using C++ and creating a UEditableTextBox in my user interface. However, I have keyboard shortcuts on keys without modifiers (e.g. I want to be able to use the T key for a shortcut by itself, not with Control or Shift), and whenever I enter text that contains that character, it triggers the app shortcut. Any way to prevent my UEditableTextBox from passing on the key event to the main app? Here’s the code I’m using:

// binding shortcut
InputComponent->BindKey(EKeys::T, IE_Released, this, &AMyPlayerController::someFunction);    

// creating UEditableTextBox
UEditableTextBox* myInput = customHUD->getDesktopWidget()->WidgetTree->ConstructWidget(UEditableTextBox::StaticClass());
UCanvasPanelSlot* inputSlot = customHUD->getDesktopPanel()->AddChildToCanvas(myInput);

First of all if you dont know UMG us not really UI system, it’s a blueprint usable wrapper for Slate which is the actual UI system that powers UMG. Because of that you got pair of objects, one is UMG widget one you know and Slate widget it self, contained in UMG widget (way system is constricted UMG widget can actually contain whole composition of Slate widgets).

Slate have OnKeyDownHandler event that by return passes whatever key is handled or not which stops key event to go forward, but UMG does not wrap it, so you will need to directly interact with Slate wdget.

There 2 methods. Make a class based of of UEditableTextBox and override RebuildWidget (you will need to look how it build in engine source code) and bind OnKeyDownHandler in widget decleration

Your binded event functiion need to look like this

the event will need to return FReply which you need return to FReply::Handled() informing rest of the widget code that you event handled the key… but it’s a lie ofcorse, to prevent it widget to handle the key :stuck_out_tongue: in any other case return FReply::Unhandled()

Method 2 also involve event binding, but instead of creating new widget class you access Slatye widget from exisitng UMG widget with this function:

And bind the event in slate widget with this function

Also here docummention on Slate

If you plan C++ with widgets this will let you cast extra juices out of UMG thru Slate as UMG don’t cover 100% of Slate features

Thank you so much for this detailed and very helpful reply! I did get your #2 method implemented, but it turned out that my keyboard shortcuts were assigned to work on key release rather than key press, and the slate input widget didn’t have a function I could assign to handle key release events (only key press events). However, changing my shortcuts to work on key press rather than key release made them work fine with my input, where key press events within the input don’t trigger key press keyboard shortcuts. In case anyone is interested, my code to implement your suggestion is this:

// outer class function
FReply MyOuterClass::onKeyCustom(const FGeometry& MyGeometry, const FKeyEvent& KeyEvent) {
    		return FReply::Handled();
// assigning key press override on UEditableTextBox* myInput
SEditableTextBox* mySlateText = (SEditableTextBox*) myInput->GetCachedWidget().Get();
mySlateText->SetOnKeyDownHandler(FOnKeyDown::CreateRaw(this, &MyOuterClass::onKeyCustom));

The solution in the end was to move my shortcuts from being bound on key release (IE_Released) to being bound on key press (IE_Pressed). The UEditableTextBox input doesn’t trigger the shortcuts if they’re activated on key press - it’s a bug somewhere in the depths of UE4 that key release events escape the UEditableTextBox unhandled and trigger shortcuts assigned to be handled on key release.

Thanks so much for taking the time to give such a detailed answer! I implemented your suggestion number 2 but it would only work for key press events - there was no way I could find to override key release events (my shortcuts were assigned to key release). But I posted below that changing to having the shortcuts triggered on key press fixed the problem.

Here’s the code I used to implement your suggestion, in case someone finds it helpful:

// outer class handler definition
FReply MyOuterClass::onKeyCustom(const FGeometry& MyGeometry, const FKeyEvent& KeyEvent) {
		printf("key pressed custom\n");
		return FReply::Handled();

// code to assign the handler for UEditableTextBox* myInput
SEditableTextBox* mySlateText = (SEditableTextBox*) myInput->GetCachedWidget().Get();
mySlateText->SetOnKeyDownHandler(FOnKeyDown::CreateRaw(this, &MyOuterClass::onKeyCustom));

Shadowriver super knowing man as usual) Thank you very much! I used the first method