I had written this and it seems to work
void AddTask(KeyDownEvent e)
{
if (e.keyCode == KeyCode.Return) AddTask();
}
I had written this and it seems to work
void AddTask(KeyDownEvent e)
{
if (e.keyCode == KeyCode.Return) AddTask();
}
Yep, that works. It’s nice and simple too!
You could also try modifying things to work with the new input system if you like. That can take a little bit of effort but isn’t super difficult. In general, I like to try and use the new input system whenever I can these days, but throwing that into the course was a little off topic.
Like everything in Unity, there’s always more than one way to do pretty much anything!
Even quicker…
taskText.RegisterValueChangedCallback((evt) =>
{
AddTask();
})
Unfortunately, while that trick works flawlessly for TextField, IntegerField and FloatField, it does not work at all on Slider or SliderInt as neither one exposes IsDelayed.
I found that using RegisterValueChangeCallback
will also add the task when you click away from the text field, which isn’t always what you want. Explicitly listening for the return key gives you some added flexibility and means you can be a bit more explicit about when you submit.
However, RegisterValueChangeCallback
will be the perfect option for when we come to searching for tasks in our list, so we will see it in action later in the course
I hadn’t thought of that last part. In the InventoryItemEditor, I make extensive use of the RegisterValueChangeCallback
because generally in a property, you do want to commit the field once the control leaves focus.