Well, with the “new” UI system uGUI we have a little issue. The InputField class does not have a event / callback when a key is pressed. However it actually has a protected method “KeyPressed” which is called when a key event happens. Unfortunately it is not marked virtual so we can not override it in our own subclass. That’s a really bad shortcomming of Unity’s class design and others have already noticed that 3 years ago.
Your only option here would be to create a subclass and override the only alternative we have which is OnUpdateSelected. It actually grabs the queued events from Unity’s event system and processes them with the KeyPressed method. Since we want our own handler inside that loop your only option is to copy the whole content of that method into your overridden method and add your special handling right in place.
Though regardless of this workaround I would recommend you file a bug report pointing out that KeyPressed should be a virtual method.
Anyways here’s an example implementation I quickly implemented:
// InputFieldUndo.cs
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class InputFieldUndo : InputField
{
protected Stack<Action> undoStack = new Stack<Action>();
protected Stack<Action> redoStack = new Stack<Action>();
protected Event m_ProcessingEvent = new Event();
public override void OnUpdateSelected(BaseEventData eventData)
{
if (!isFocused)
return;
bool consumedEvent = false;
while (Event.PopEvent(m_ProcessingEvent))
{
if (m_ProcessingEvent.rawType == EventType.KeyDown)
{
consumedEvent = true;
if (ProcessUndo(m_ProcessingEvent))
{
var shouldContinue = KeyPressed(m_ProcessingEvent);
if (shouldContinue == EditState.Finish)
{
DeactivateInputField();
break;
}
}
}
switch (m_ProcessingEvent.type)
{
case EventType.ValidateCommand:
case EventType.ExecuteCommand:
switch (m_ProcessingEvent.commandName)
{
case "SelectAll":
SelectAll();
consumedEvent = true;
break;
}
break;
}
}
if (consumedEvent)
UpdateLabel();
eventData.Use();
}
private bool ProcessUndo(Event e)
{
if (e.keyCode == KeyCode.U && e.control)
{
if (undoStack.Count > 0)
{
StackPush(redoStack);
undoStack.Pop()();
}
return false;
}
else if (e.keyCode == KeyCode.R && e.control)
{
if (redoStack.Count > 0)
{
StackPush(undoStack);
redoStack.Pop()();
}
return false;
}
else if (ShouldRecord(e))
{
redoStack.Clear();
StackPush(undoStack);
}
return true;
}
// restrict undo recording to relevant key presses
private bool ShouldRecord(Event e)
{
switch (e.keyCode)
{
case KeyCode.None:
case KeyCode.LeftShift:
case KeyCode.RightShift:
case KeyCode.LeftControl:
case KeyCode.RightControl:
case KeyCode.LeftAlt:
case KeyCode.RightAlt:
return false;
}
return true;
}
private void StackPush(Stack<Action> stack)
{
var oldText = text;
var oldPos = caretPosition;
stack.Push(() =>
{
text = oldText;
caretPosition = oldPos;
});
}
}
Of course you have to use this class instead of the old InputField class. The easiest way to replace the class is:
select your InputField gameobject in the inspector
switch the inspector to debug mode through the top right context menu
drag our new script file onto the “script” field of the old InputField component
switch the inspector back to normal mode
That way the InputField will keep all its current settings and references but the actual class is replaced. The alternative way is to manually remove the oldInputField component, add the InputFieldUndo component and setup all the required references and settings. Unfortunately we can’t easily add our new input field to the create UI menu.
Note I had to apply a “filter” for what keycodes actually trigger a “queueing action”. Otherwise it would queue way too many undo actions because Unity had some keydown events with “KeyCode.None” (usually happens for char events). If you want to exclude other keys as well you can add them to the “ShouldRecord” method.