How do I programmatically pass a capital letter to an input field?

If I have an input field then I can pass text to it using

inputField.ProcessEvent(Event.KeyboardEvent("a"));

This will insert an ‘a’ at the current caret position. However if I use

inputField.ProcessEvent(Event.KeyboardEvent("A"));

An ‘a’ is still inserted rather than an ‘A’. How do I get it to pass ‘A’?

I’ve tried:

inputField.ProcessEvent(Event.KeyboardEvent("#a"));

Which according to the docs should pass the shift key allong with the ‘a’. It doesn’t error, but nothing is passed through. Debug.Log(Event.KeyboardEvent(“#a”)) shows the shift key modifier, but the key code is \0

I found luck (albeit using tmp pro input field) in this

foreach(char c in s)
{
      Event ev = Event.KeyboardEvent("t"); //placeholder
      ev.character = c; //set the actual char
      inputTextField.ProcessEvent(ev);
}
inputTextField.ForceLabelUpdate();

Where s is whatever string you’re trying to add (On my keyboard I have .com)
Then if some values aren’t passing, make sure tmp pro inputfield content type is set to standard.

Hello, i have tested this code for making sure it was upper letter and even if its was printing that i was a capital leter it didnt worked out.

    char a = 'a';
    char c = char.ToUpper(a);
    string b = c.ToString();
    Debug.Log(char.IsUpper(b[0]));
    input.ProcessEvent(Event.KeyboardEvent(b)));

so i would suggest using the text property instead, is there any reason you can not use it?

    input.text = "A";

using that propery will “remove” the place holder so wait to assign it when needed.

The trouble with using the text property is that it won’t take into account the position of the caret. I could work that out and rebuild the text. But then I expect I’ll run into a problem of moving the caret to the correct position.It seems like there should be a better way to do it.

I’m fairly sure this is caused by a bug in Event.KeyboardEvent as I can’t see any code in the unity inputfield.cs source to change the case of the character. It seems to be relying on it being correct in Event.KeyboardEvent. But I’m a unity noob so I’m not sure.

Anyhow, I’ve made a workaround by extending inputfield.cs as per below. (More details on extending inputfield here.)

First, this is the calling code:

Event keyboardEvent = Event.KeyboardEvent(character);
keyboardEvent.modifiers = EventModifiers.Shift;
inputFiledWithShift.ProcessEvent(keyboardEvent);
inputFiledWithShift.ForceLabelUpdate();

This is the extended inputField. All this just to add three lines about two thirds of the way down .

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class InputFieldWithoutKeyboard : InputField
{

    public new void ProcessEvent(Event e)
    {
        KeyPressed(e);
    }

    protected new EditState KeyPressed(Event evt)
    {
        var currentEventModifiers = evt.modifiers;
        RuntimePlatform rp = Application.platform;
        bool isMac = (rp == RuntimePlatform.OSXEditor || rp == RuntimePlatform.OSXPlayer);
        bool ctrl = isMac ? (currentEventModifiers & EventModifiers.Command) != 0 : (currentEventModifiers & EventModifiers.Control) != 0;
        bool shift = (currentEventModifiers & EventModifiers.Shift) != 0;
        bool alt = (currentEventModifiers & EventModifiers.Alt) != 0;
        bool ctrlOnly = ctrl && !alt && !shift;

        switch (evt.keyCode)
        {
            case KeyCode.Backspace:
                {
                    base.KeyPressed(evt);
                    return EditState.Continue;
                }

            case KeyCode.Delete:
                {
                    base.KeyPressed(evt);
                    return EditState.Continue;
                }

            case KeyCode.Home:
                {
                    MoveTextStart(shift);
                    return EditState.Continue;
                }

            case KeyCode.End:
                {
                    MoveTextEnd(shift);
                    return EditState.Continue;
                }

            // Select All
            case KeyCode.A:
                {
                    if (ctrlOnly)
                    {
                        SelectAll();
                        return EditState.Continue;
                    }
                    break;
                }

            // Copy
            case KeyCode.C:
                {
                    base.KeyPressed(evt);
                    break;
                }

            // Paste
            case KeyCode.V:
                {
                    base.KeyPressed(evt);
                    break;
                }

            // Cut
            case KeyCode.X:
                {
                    base.KeyPressed(evt);
                    break;
                }

            case KeyCode.LeftArrow:
                {
                    base.KeyPressed(evt);
                    return EditState.Continue;
                }

            case KeyCode.RightArrow:
                {
                    base.KeyPressed(evt);
                    return EditState.Continue;
                }

            case KeyCode.UpArrow:
                {
                    base.KeyPressed(evt);
                    return EditState.Continue;
                }

            case KeyCode.DownArrow:
                {
                    base.KeyPressed(evt);
                    return EditState.Continue;
                }

            // Submit
            case KeyCode.Return:
            case KeyCode.KeypadEnter:
                {
                    if (lineType != LineType.MultiLineNewline)
                    {
                        return EditState.Finish;
                    }
                    break;
                }

            case KeyCode.Escape:
                {
                    base.KeyPressed(evt);
                    return EditState.Finish;
                }

        }

        char c = evt.character;

        // !!!! The whole purpose of this overridden function is to add uppercase here !!!!
        if (evt.modifiers == EventModifiers.Shift)
        {
            c = c.ToString().ToUpper()[0];
        }

        // Dont allow return chars or tabulator key to be entered into single line fields.
        if (!multiLine && (c == '	' || c == '\r' || c == 10))
            return EditState.Continue;

        // Convert carriage return and end-of-text characters to newline.
        if (c == '\r' || (int)c == 3)
            c = '

';

        if (IsValidChar(c))
        {
            Append(c);
        }

        if (c == 0)
        {
            if (Input.compositionString.Length > 0)
            {
                UpdateLabel();
            }
        }
        return EditState.Continue;
    }

    private bool IsValidChar(char c)
    {
        // Delete key on mac
        if ((int)c == 127)
            return false;
        // Accept newline and tab
        if (c == '	' || c == '

')
return true;

        return m_TextComponent.font.HasCharacter(c);
    }
}