Question by
MalikDrako · Sep 16, 2021 at 06:19 AM ·
uiinputkeyboardkeypress
Capturing some key presses from a (TMP) InputField
Is there a way to capture some key presses and prevent them from propagating to a (TMP) InputField, but not most keys?
For example, while the field is selected I want to do something when the up or down arrow keys are pressed and prevent the field cursor from moving to the beginning or end. When the enter key is pressed, I want to perform an action first then allow the field to do its handling of the enter key.
I tried using an IUpdateSelectedHandler
, but the function wasn't being called even when I moved the behavior above the field behavior. When I came back to it (after closing Unity), the function was being called but the field I swapped the behavior order on wasn't responding to keyboard input.
public void OnUpdateSelected(BaseEventData eventData)
{
if (!selected)
return;
Event evt = new Event();
while (Event.PopEvent(evt))
{
if (evt.rawType == EventType.KeyDown)
{
bool consumed = false;
switch (evt.keyCode)
{
case KeyCode.UpArrow:
consumed = true;
// Handle up arrow
break;
case KeyCode.DownArrow:
consumed = true;
// Handle down arrow
break;
case KeyCode.KeypadEnter:
case KeyCode.Return:
// Handle enter
break;
}
if (consumed)
eventData.Use();
}
}
}
Comment