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

**URL:** <https://forums.unrealengine.com/t/how-to-prevent-text-entered-in-ueditabletextbox-from-triggering-keyboard-shortcuts-c/471793>\
**Category:** UI\
**Tags:** unreal-engine, shortcuts, slate, CPP, question\
**Created:** [September 23, 2020, 7:39am UTC](https://forums.unrealengine.com/t/how-to-prevent-text-entered-in-ueditabletextbox-from-triggering-keyboard-shortcuts-c/471793 "2020-09-23T07:39:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tplewe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tplewe](https://forums.unrealengine.com/u/tplewe)\
**Post date:** [September 23, 2020, 7:39am UTC](https://forums.unrealengine.com/t/how-to-prevent-text-entered-in-ueditabletextbox-from-triggering-keyboard-shortcuts-c/471793/1 "2020-09-23T07:39:07Z")

</div>

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);
```

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [September 23, 2020, 8:36pm UTC](https://forums.unrealengine.com/t/how-to-prevent-text-entered-in-ueditabletextbox-from-triggering-keyboard-shortcuts-c/471793/2 "2020-09-23T20:36:31Z")

</div>

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

> **[SEditableTextBox::FArguments::OnKeyDownHandler](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Slate/Widgets/Input/SEditableTextBox/FArguments/OnKeyDownHandler/)**
>
> Callback delegate to have first chance handling of the OnKeyDown event

Your binded event functiion need to look like this

> **[SEditableText::OnKeyDown](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Slate/Widgets/Input/SEditableText/OnKeyDown/)**
>
> Called after a key is pressed when this widget has focus (this event bubbles if not handled)

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 😛 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:

> **[UWidget::GetCachedWidget](https://docs.unrealengine.com/5.1/en-US/API/Runtime/UMG/Components/UWidget/GetCachedWidget/)**
>
> Gets the last created widget does not recreate the gc container for the widget if one is needed.

And bind the event in slate widget with this function

> **[SEditableText::SetOnKeyDownHandler](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Slate/Widgets/Input/SEditableText/SetOnKeyDownHandler/)**
>
> Sets the OnKeyDownHandler to provide first chance handling of the OnKeyDown event

Also here docummention on Slate

> **[Slate UI Programming](https://docs.unrealengine.com/5.1/en-US/slate-user-interface-programming-framework-for-unreal-engine/)**
>
> Program user interfaces with Unreal Engine's Slate framework.

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

---

<div class="post-metadata">

**Author:** ![tplewe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tplewe](https://forums.unrealengine.com/u/tplewe)\
**Post date:** [September 23, 2020, 10:07pm UTC](https://forums.unrealengine.com/t/how-to-prevent-text-entered-in-ueditabletextbox-from-triggering-keyboard-shortcuts-c/471793/3 "2020-09-23T22:07:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tplewe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tplewe](https://forums.unrealengine.com/u/tplewe)\
**Post date:** [September 23, 2020, 10:10pm UTC](https://forums.unrealengine.com/t/how-to-prevent-text-entered-in-ueditabletextbox-from-triggering-keyboard-shortcuts-c/471793/4 "2020-09-23T22:10:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tplewe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tplewe](https://forums.unrealengine.com/u/tplewe)\
**Post date:** [September 23, 2020, 10:12pm UTC](https://forums.unrealengine.com/t/how-to-prevent-text-entered-in-ueditabletextbox-from-triggering-keyboard-shortcuts-c/471793/5 "2020-09-23T22:12:57Z")

</div>

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));
```

---

<div class="post-metadata">

**Author:** ![tplewe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tplewe](https://forums.unrealengine.com/u/tplewe)\
**Post date:** [September 27, 2020, 5:54am UTC](https://forums.unrealengine.com/t/how-to-prevent-text-entered-in-ueditabletextbox-from-triggering-keyboard-shortcuts-c/471793/6 "2020-09-27T05:54:49Z")

</div>

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));
```

---

<div class="post-metadata">

**Author:** ![6r0m](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/6r0m/32/1194563_2.png) [@6r0m](https://forums.unrealengine.com/u/6r0m)\
**Post date:** [February 3, 2021, 1:28pm UTC](https://forums.unrealengine.com/t/how-to-prevent-text-entered-in-ueditabletextbox-from-triggering-keyboard-shortcuts-c/471793/7 "2021-02-03T13:28:43Z")

</div>

\*\*\*\* super knowing man as usual) Thank you very much! I used [the first method](https://answers.unrealengine.com/questions/85111/how-to-navigate-between-umg-widgets-with-tab-key.html)
