Focus UiCanvas TextInput element

Question

How can I focus UiCanvas TextInput element in C++?

Description

I can load it, get InputElement, and even set text to input. But how can I focus this element so I don’t need to mouse click it? Thanks in advance for help, or tip. Here is my code fragment:


// load canvas
static ::AZ::EntityId canvasEntity;
EBUS_EVENT_RESULT(canvasEntity, UiCanvasManagerBus, LoadCanvas, "UI/path/to/name.uicanvas");
// get input element
::AZ::Entity* inputElement = nullptr;
EBUS_EVENT_ID_RESULT(inputElement, canvasEntity, UiCanvasBus, FindElementByHierarchicalName, "root/path/to/input");
if(inputElement){
// set example text
EBUS_EVENT_ID(inputElement->GetId(), UiTextInputBus, SetText, "Hello, World!");
}

Related

Also, the text is set indeed, but it does not show up until I click on input by mouse.

  • Related: (#59888) UiTextInputBus strange behavior
    For some reason, the text not showing up is not an issue using Lua. Example code (setting script on TextInput element):

local script = {Propeteries = {}};
function script:OnActivate(x)
local input = tostring(UiCanvasRefBus.Broadcast.GetCanvas());
local inputText = UiTextInputBus.Broadcast.GetTextEntity(input);
UiTextBus.Event.SetText(inputText, "Hello, World!");
end;
function script:OnDeactivate() end;
return script;

Workaround

Once applying fix #59938 and rebuilding LyShine, I am using ugly workaround with HandleAutoActivation and ForceActiveInteractable.

	EBUS_EVENT_ID(inputElement->GetId(), UiInteractableBus, HandleAutoActivation);
EBUS_EVENT_ID(canvasEntity, UiCanvasBus, ForceActiveInteractable, inputElement->GetId(), true, [&]{
::AZ::Vector2 mousePos;
EBUS_EVENT_ID_RESULT(mousePos, canvasEntity, UiCanvasBus, GetMousePosition);
return mousePos;
}());

Issue

public ForceActiveInteractable would be simply replaced by SetHoverInteractable and SetActiveInteractable, but they are private, so I can only call ForceActiveInteractable, but I need to GetMousePosition just to pass it back even if I am not changing m_lastMousePosition.

Request

Adding Focus public method, that makes passed element both “active” and “interactible” on demand would be highly appreciated, until i am requesting for something that already exists. In last case, I am sorry for inconvenience.

Thanks! Glad to see that :slight_smile:

Hey @Benio – apologies for the delayed response. Thanks for compiling this post in this format :smiley:

I’ve added this to our list for further action - will update this thread as soon as I can! Also, getting assistance on the original question –

It’s been a long time since following up on this post. I’m going to check and see if SetText for TextInput is working properly now. I’ll also see if there’s a new way of focusing the input without having to mouse press.

Internal Tracking: LY-96911

Hi @Benio,

There isn’t existing functionality for this in that there isn’t an exposed bus function for this. HandleAutoActivation in UiTextInputComponent is close to this and could be extended/have a similar function written to handle this. We’ll look at this as a future addition. Thanks!

Thank you for the update, I will take a look to find the workaround of this.

@Soeda Hi, I wanted to let you know we have a fix for this that will be coming out in a future lumberyard release. There is a new function called ForceFocusInteractable that combines the functionality of ForceActiveInteractable and HandleAutoActivation but doesn’t require a mouse position. If you feel this is still a priority for you we build a patch for you and send it over!

It sounds fit to my requirements, but my project have been using LY 1.21.2 and scheduled to upgrade to 1.23 in next month.
Thus, no need to hurry, but it will be helpful if you will send me a patch for 1.23 sometime next month.

Thank you.