Hitting enter on button does not refocus

In the Add new task section, if I enter a name, then use tab to mark the button and hit enter the task is created as intended and the focus is once again set on the name. But the “edit” part of the focus is not active. That is, I can’t immedietely start writing a new name.

I’ve tried clicking the button as normal and that works as intended with the editfield marked and ready to go.

Things I’ve tried to make it work without any success

  • Set a time duration, since a post mentioned it could have to do with unity still processing the enter key, and wait to run the Focus afterwards
  • Call the setfocus twice

The registration (Note I have an extra field with input)

_taskText = _container.Q<TextField>("TaskName");
_taskText.RegisterCallback<KeyDownEvent>(AddTask);
_taskSize = _container.Q<IntegerField>("TaskSize");

The AddTask method

private void AddTask()
{
  if (string.IsNullOrEmpty(_taskText.value))
    return;
  AddTaskItem(_taskText.value, _taskSize.value);
  SaveTask(_taskText.value, _taskSize.value);
  _taskText.value = "";
  _taskText.Focus();
}

Try this:

_taskText.ElementAt(0).Focus()
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms