dcockerham
Topic Author
Posts: 3
Joined: 10 Apr 2021, 00:44

Setting IsFocused without setting IsFocusEngaged

10 Apr 2021, 01:09

I'm trying to set the focus on a TextBox without engaging the TextBox. The target is a console application, so engaging the TextBox brings up the virtual keyboard, which I don't want to do until the user hits the confirm button. I do want the focus to be on the TextBox when the screen appears, though, so I need to set it to focused somehow. However, I've been struggling with finding a way to focus on the TextBox without engaging it. Whether I use SetFocusAction in the xaml or MoveFocus in the cpp, the TextBox always seems to engage when focused on, with the only exception being when I actually use a Gamepad's directional inputs to manually navigate to the TextBox (which isn't a viable option for setting initial focus). Even if I immediately change the IsFocusEngaged property, like so...
<noesis:SetFocusAction />
<ei:ChangePropertyAction PropertyName="IsFocusEngaged" Value="False" />
...it seems to be too late to keep the virtual keyboard from coming up. Is there some way to set the Focus to the TextBox without also setting IsFocusEngaged to True?
 
User avatar
sfernandez
Site Admin
Posts: 2255
Joined: 22 Dec 2011, 19:20

Re: Setting IsFocused without setting IsFocusEngaged

12 Apr 2021, 10:34

By default controls that get focus are automatically engaged unless they are focused from a gamepad directional input.
To disable that behavior you can call in a control SetUsingFocusEngagement(true) before setting the focus.

I think we can extend our SetFocusAction to include an extra property to indicate if focus engagement should be activated:
void SetFocusAction::Invoke(Noesis::BaseComponent*)
{
    Noesis::UIElement* element = GetTarget();
    if (element != nullptr)
    {
        Noesis::Control* control = Noesis::DynamicCast<Noesis::Control*>(element);
        if (control != nullptr)
        {
            bool focusEngagement = GetValue<bool>(UsingFocusEngagementProperty);
            control->SetUsingFocusEngagement(focusEngagement);
        }

        element->Focus();
    }
}
<TextBox>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
      <noesis:SetFocusAction UsingFocusEngagement="True"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</TextBox>
Will this be useful for you?
 
User avatar
sfernandez
Site Admin
Posts: 2255
Joined: 22 Dec 2011, 19:20

Re: Setting IsFocused without setting IsFocusEngaged

12 Apr 2021, 14:09

Thinking a bit more about this we are going to expose a new Focus() method in the UIElement to indicate if control should be engaged or not when getting the focus, this way the code will be clearer:
void SetFocusAction::Invoke(Noesis::BaseComponent*)
{
    Noesis::UIElement* element = GetTarget();
    if (element != nullptr)
    {
        bool engage = GetValue<bool>(EngageProperty);
        element->Focus(engage);
    }
}
<TextBox>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
      <noesis:SetFocusAction Engage="False"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</TextBox>
 
dcockerham
Topic Author
Posts: 3
Joined: 10 Apr 2021, 00:44

Re: Setting IsFocused without setting IsFocusEngaged

12 Apr 2021, 23:54

Thanks, this is exactly what I was looking for! I think the proposed extension of SetFocusAction would be quite useful and convenient, though for the time being using SetUsingFocusEngagement(true) is getting the job done well enough.
 
User avatar
sfernandez
Site Admin
Posts: 2255
Joined: 22 Dec 2011, 19:20

Re: Setting IsFocused without setting IsFocusEngaged

14 Apr 2021, 11:44

Ok, thanks for the feedback, we already created that extension for the next release.

Who is online

Users browsing this forum: Google [Bot] and 1 guest