# Editable text: how to get the current caret position?

**URL:** <https://forums.unrealengine.com/t/editable-text-how-to-get-the-current-caret-position/415513>\
**Category:** UI\
**Tags:** cursor, unreal-engine, slate, question, UMG\
**Created:** [January 25, 2018, 1:03pm UTC](https://forums.unrealengine.com/t/editable-text-how-to-get-the-current-caret-position/415513 "2018-01-25T13:03:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_ccf720dd](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ccf720dd](https://forums.unrealengine.com/u/anonymous_user_ccf720dd)\
**Post date:** [January 25, 2018, 1:03pm UTC](https://forums.unrealengine.com/t/editable-text-how-to-get-the-current-caret-position/415513/1 "2018-01-25T13:03:02Z")

</div>

Hi,

Do you know whether threre is any reasonable way of getting the current caret position when positioned in an Editable Text? I mean, which character is it next to. It would be fine if there’s a way to do it that works for either the UEditableText (UMG) or the SEditableText (Slate), since the first one contains the second one. I’ve checked the code of both Editable Text and Editable Text Box classes and they seem to delegate the task of moving the caret position to the Layout, which on its turn delegates it depeer and depeer (the mentioned classes do not hold any variable about the current text position). That’s why it seems difficult to find a way to get the caret position.

This question has been asked a couple of times before in the recent years by other people with no response. If it’s not currently possible to do this, think of this as a feature request to make it available in future versions of the engine if you think it’s interesting enough.

Thank you.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ccf720dd](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ccf720dd](https://forums.unrealengine.com/u/anonymous_user_ccf720dd)\
**Post date:** [January 25, 2018, 4:29pm UTC](https://forums.unrealengine.com/t/editable-text-how-to-get-the-current-caret-position/415513/2 "2018-01-25T16:29:18Z")

</div>

It seems that there would be an actual way to do this in one line (with a UEditableTextBox\* TextBox, and similarly for a UEditableText\* Text):

```
ETextLocation CaretLocation = StaticCastSharedRef<SEditableTextBox>(TextBox->TakeWidget())->EditableText->EditableTextLayout->CursorInfo.GetCursorInteractionLocation();

```

But that won’t compile, since SEditableTextBox::EditableText, SEditableText::EditableTextLayout and SlateEditableTextLayout::CursorInfo are not public members, but private/protected without getter functions.

**Epic, would you find it reasonable to add public getters for these values in a future update?**

With the getter functions, it would be something like this (imagine that we are in a library C++ file, that you would expose to Blueprints if needed):

```
static ETextLocation GetCaretLocationForTextBox(UEditableTextBox& TextBox)
{

return StaticCastSharedRef<SEditableTextBox>(TextBox.TakeWidget())->GetEditableText()->GetEditableTextLayout()->GetCursorInfo().GetCursorInteractionLocation();
}

```

I think many people would find it useful, specially those working in virtual keyboards that need to work with editable text fields in code.

The workaround would be to subclass the three affected classes to add the needed getters and a UEditableTextBox subclass and manually set up the linking of the UMG text box widget with the Slate text box widget with the Slate text widget to get things working, which seems likely to lead to unexpected behavior or incompatibilities when the engine is updated and the parent classes get modified.

---

<div class="post-metadata">

**Author:** ![FridgeFace16](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FridgeFace16](https://forums.unrealengine.com/u/FridgeFace16)\
**Post date:** [June 21, 2021, 6:00am UTC](https://forums.unrealengine.com/t/editable-text-how-to-get-the-current-caret-position/415513/3 "2021-06-21T06:00:54Z")

</div>

Hi SomeRandomDev,

Some extensive looking into the engine source uncovered an event, that fires whenever the caret is moved, providing details of the line and offset of your caret/selected text.

It’s a protected member of SEditableText

```
virtual void OnCursorMoved(const FTextLocation& InLocation) override;
```

Overriding this in your slate subclass and caching the value should do the trick.

Also with this cached value you can pass it into useful functions such as

```
EditableTextLayout->IsAtEndOfDocument(const FTextLocation& Location);
EditableTextLayout->IsAtBeginningOfDocument(const FTextLocation& Location);
```

I imagine you no longer need this information, but figured I’d put this here since there aren’t any answers to be found elsewhere, and this post came up first on my search.

Hope this helps anyone else that struggles with this 🙂

---

<div class="post-metadata">

**Author:** ![kevinraz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kevinraz](https://forums.unrealengine.com/u/kevinraz)\
**Post date:** [September 2, 2022, 9:27pm UTC](https://forums.unrealengine.com/t/editable-text-how-to-get-the-current-caret-position/415513/4 "2022-09-02T21:27:20Z")

</div>

I just implemented something similar, and I thought I would share it with anyone looking to do something like this.

This enables two functions in your widget BP, get cursor position AND insert text at the cursor.

**Get cursor position in multiline editable text box**

Create a new C++ class extending from **UMultiLineEditableText**

**RM\_UMultiLineEditableText.h:**

```auto
#pragma once

#include "CoreMinimal.h"
#include "Components/EditableTextBox.h"
#include "Components/MultiLineEditableText.h"
#include "RM_MultiLineEditableText.generated.h"

/** Called when the cursor is moved within the text area */
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnCursorMovedRM, int32, InLineIndex, int32, InOffset);

/**
 * Editable text box widget
 */
UCLASS(meta=( DisplayName="Editable Text (Multi-Line) RM" ))
class TRANSPORTATIONTABLE_API URM_MultiLineEditableText : public UMultiLineEditableText
{
	/** Called when the cursor is moved within the text area */
	DECLARE_DELEGATE_OneParam( FOnCursorMoved, const FTextLocation& );
	
	GENERATED_BODY()

	/** Called when the cursor is moved within the text area */
	UPROPERTY(BlueprintAssignable)
	FOnCursorMovedRM OnCursorMoved;
	
	virtual TSharedRef<SWidget> RebuildWidget() override;

public:
	void HandleEditableTextCursorMoved(const FTextLocation& NewCursorPosition ) const;

	UFUNCTION(BlueprintCallable)
	void InsertTextAtCursor(FString InText) const;
};

```

**RM\_MultiLineEditableText.cpp:**

```auto
#include "Widgets/Text/SMultiLineEditableText.h"
#include "UI/RM_MultiLineEditableText.h"

TSharedRef<SWidget> URM_MultiLineEditableText::RebuildWidget()
{
	MyMultiLineEditableText = SNew(SMultiLineEditableText)
	.TextStyle(&WidgetStyle)
	.AllowContextMenu(AllowContextMenu)
	.IsReadOnly(bIsReadOnly)
	.OnCursorMoved(BIND_UOBJECT_DELEGATE(FOnCursorMoved, HandleEditableTextCursorMoved))
	.SelectAllTextWhenFocused(SelectAllTextWhenFocused)
	.ClearTextSelectionOnFocusLoss(ClearTextSelectionOnFocusLoss)
	.RevertTextOnEscape(RevertTextOnEscape)
	.ClearKeyboardFocusOnCommit(ClearKeyboardFocusOnCommit)
	.VirtualKeyboardOptions(VirtualKeyboardOptions)
	.VirtualKeyboardDismissAction(VirtualKeyboardDismissAction)
	.OnTextChanged(BIND_UOBJECT_DELEGATE(FOnTextChanged, HandleOnTextChanged))
	.OnTextCommitted(BIND_UOBJECT_DELEGATE(FOnTextCommitted, HandleOnTextCommitted))
	;

	return MyMultiLineEditableText.ToSharedRef();
}

void URM_MultiLineEditableText::HandleEditableTextCursorMoved(const FTextLocation& NewCursorPosition) const
{
	OnCursorMoved.Broadcast(NewCursorPosition.GetLineIndex(), NewCursorPosition.GetOffset());
}

void URM_MultiLineEditableText::InsertTextAtCursor(const FString InText) const
{
	MyMultiLineEditableText->InsertTextAtCursor(InText);
}

```

---

<div class="post-metadata">

**Author:** ![Monsieur\_Dupond](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Monsieur\_Dupond](https://forums.unrealengine.com/u/Monsieur_Dupond)\
**Post date:** [March 10, 2023, 11:07pm UTC](https://forums.unrealengine.com/t/editable-text-how-to-get-the-current-caret-position/415513/5 "2023-03-10T23:07:02Z")

</div>

Hey, your code works perfectly, I also managed to add the GetSelectedText() function thanks to it but I also want to add the GoTo() function but everytime I build it gives me this error:

```auto
Projects\GetSelectedText\Plugins\CommonCPPFunctions\Source\CommonCPPFunctions\Public\MDMultiLineEditableText.h(50): Error: Unable to find 'class', 'delegate', 'enum', or 'struct' with name 'FTextLocation'

```

.h

```auto
UFUNCTION(BlueprintCallable)
	void GoTo(const FTextLocation& NewLocation);

```

.cpp

```auto
void UMDMultiLineEditableText::GoTo(const FTextLocation& NewLocation)
{
	MyMultiLineEditableText->GoTo(NewLocation);
}

```

I don’t get the error when I remove the UFUNCTION() part. Thanks for you help

---

<div class="post-metadata">

**Author:** ![Chiefwhosm](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Chiefwhosm](https://forums.unrealengine.com/u/Chiefwhosm)\
**Post date:** [March 5, 2024, 8:52pm UTC](https://forums.unrealengine.com/t/editable-text-how-to-get-the-current-caret-position/415513/7 "2024-03-05T20:52:25Z")

</div>

An old thread (but the only one with this information I could find).  
Anyway, this was how I resolved setting the carets’ position within BP using @Monsieur_Dupond’s code.

In testing the widget loses focus after changing the caret location, so using a Set Keyboard Focus node on the widget in BP resolved this.

Also, since 5.1 some of @kevinraz’s code is deprecated, so here’s the full section replace:

```auto
MyMultiLineEditableText = SNew(SMultiLineEditableText)
.TextStyle(&WidgetStyle)
.AllowContextMenu(AllowContextMenu)
.IsReadOnly(GetIsReadOnly())
.OnCursorMoved(BIND_UOBJECT_DELEGATE(FOnCursorMoved, HandleEditableTextCursorMoved))
.SelectAllTextWhenFocused(GetSelectAllTextWhenFocused())
.ClearTextSelectionOnFocusLoss(GetClearTextSelectionOnFocusLoss())
.RevertTextOnEscape(GetRevertTextOnEscape())
.ClearKeyboardFocusOnCommit(GetClearKeyboardFocusOnCommit())
.VirtualKeyboardOptions(VirtualKeyboardOptions)
.VirtualKeyboardDismissAction(VirtualKeyboardDismissAction)
.OnTextChanged(BIND_UOBJECT_DELEGATE(FOnTextChanged, HandleOnTextChanged))
.OnTextCommitted(BIND_UOBJECT_DELEGATE(FOnTextCommitted, HandleOnTextCommitted))
;

```

.cpp

> //Monsieur\_Dupond  
> void UMDMultiLineEditableText::GoTo(const FTextLocation& NewLocation)  
> {  
> MyMultiLineEditableText-\>GoTo(NewLocation);  
> }
> 
> //Only way to use BP to then call the above function to make it switch to the new location.  
> void UMDMultiLineEditableText::NewCursorLocation(int32 LineIndex, int32 LineOffset)  
> {  
> MyMultiLineEditableText-\>GoTo(FTextLocation(LineIndex,LineOffset));  
> }

.h

> //Monsieur\_Dupond  
> void GoTo(const FTextLocation& NewLocation);
> 
> UFUNCTION(BlueprintCallable)  
> void NewCursorLocation(int32 LineIndex, int32 LineOffset);

I do not claim to be a C++ expert, but the above worked for me 🙂

---

<div class="post-metadata">

**Author:** ![Lonytas](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lonytas](https://forums.unrealengine.com/u/Lonytas)\
**Post date:** [July 21, 2024, 1:09pm UTC](https://forums.unrealengine.com/t/editable-text-how-to-get-the-current-caret-position/415513/8 "2024-07-21T13:09:53Z")

</div>

For future references if anyone else has the same issues and I had. Take a look in your “Projectname.Build.cs”. In there you will find:

```auto

// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string] { "Slate", "SlateCore" });

```

---

<div class="post-metadata">

**Author:** ![STRiFE.x](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/strife.x/32/969826_2.png) [@STRiFE.x](https://forums.unrealengine.com/u/STRiFE.x)\
**Post date:** [July 16, 2025, 8:12am UTC](https://forums.unrealengine.com/t/editable-text-how-to-get-the-current-caret-position/415513/9 "2025-07-16T08:12:20Z")

</div>

In case you want the caret position for a simple `SEditableTextBox`. The answer is **DONT** … Its all buried in the `SEditableText` and not accessible from the text box at all. Horribly designed stuff to be honest especially that `SMultiLineEditableTextBox` has the cursor move event exposed.

So, even if you want single line text use `SMultiLineEditableTextBox`, disable multi-line and react to `OnCursorMoved`.

```cpp
SNew(SMultiLineEditableTextBox)
.Text(INVTEXT("Hello world"))
.AllowMultiLine(false) // force single line
.OnCursorMoved(this, &SMyWidget::OnCursorMoved) // react to cursor move event

```

```cpp
void SMyWidget::OnCursorMoved(const FTextLocation& InTextLocation)
{
	UE_LOG(LogTemp, Log, TEXT("Cursor position: %d"), InTextLocation.Offset);
}

```
