Roms6
Topic Author
Posts: 8
Joined: 10 Feb 2015, 18:53

Focus, the right way?

27 Dec 2015, 00:40

Hello guys,

I've been in trouble recently when looking for using keyboard events with NoesisGUI. Basically, I have a main menu with 3 buttons. This is from my MainMenuView UserControl :
public void OnPostInit()
    {
        Grid = (Grid) Parent;
        _settingsView = (SettingsView) Grid.FindName("settingsView");

        _stackPanel = (StackPanel) FindName("menu");
        _start = (Button)_stackPanel.FindName("start");
        _settings = (Button)_stackPanel.FindName("settings");
        _quit = (Button)_stackPanel.FindName("quit");

        _start.Click += StartOnClick;
        _settings.Click += SettingsOnClick;
        _quit.Click += QuitOnClick;
        _quit.KeyDown += QuitOnKeyDown;

        _quit.Focus();
    }
It seems I'm misunderstanding how focus is handled, because _quit.Focus() doesn't focus this button. QuitOnKeyDown is only fired if I click this button then I press a key.
 
User avatar
sfernandez
Site Admin
Posts: 1927
Joined: 22 Dec 2011, 19:20

Re: Focus, the right way?

28 Dec 2015, 11:22

Hi,

When OnPostInit is called the control is not yet attached to the UI tree so it cannot be focused. You should do that logic on the Loaded event, when control is attached to the tree and ready to be shown:
public class MainMenuView: UserControl
{
  public MainMenuView()
  {
    Loaded += MainMenuView_Loaded;
  }
  ...
  void MainMenuView_Loaded(object sender, RoutedEventArgs e)
  {
    _quit.Focus();
  }
} 
Anyway, I found a bug in our code that removes any focused element when application is started. If that is your case, please modify the NoesisGUIPanel.cs script as follows (line 460) while we fix this problem:
bool _appStart = true;
void OnApplicationFocus(bool focused)
{
  if (_uiRenderer != null)
  {
    if (!NoesisGUISystem.SoftwareKeyboardManager.IsOpen)
    {
      if (focused)
      {
        if (!_appStart)
        {
          _appStart = false;
          _uiRenderer.Activate();
        }
      }
      else
      {
        _uiRenderer.Deactivate();
      }
    }
  }
}
 
Roms6
Topic Author
Posts: 8
Joined: 10 Feb 2015, 18:53

Re: Focus, the right way?

28 Dec 2015, 17:22

Thanks for the explanation. It worked perfectly after applying the fix you provided :)

Who is online

Users browsing this forum: No registered users and 2 guests