onClick button, post preset text to InputField.

First thing: I’m new. So I’m trying to build a virtual keyboard in Unity. I’m starting with one key, and one InputField.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class KeyboardScript : MonoBehaviour {
InputField iField;

public void onClick() {
string key = "k";
iField.text(key);
}
}

Here’s how I interpret the code I wrote:
using UnityEngine.UI allows me to reference UI GameObjects.
InputField is the name of my input field and I let the computer know it is an iField.
This code is a component of my button which is called K, and so onClick() means when button K is clicked.
Let string called key equal letter k.
Let the text inside InputField equal k.

My error says "The member ‘UnityEngine.UI.InputField.Text’ cannot be used as method or delegate.

I appreciate any advice. I have looked at the Unity manuals and other things, I just need a human’s help because I don’t know what I’m looking at.
Thanks.

Yikes, you’re attempting a very difficult task for a new developer.

When a key is pressed, what should happen is that if there is an extended selection in the field, the selected text is replaced with the new text associated with that key. And if there isn’t an extended selection, then the new text should be inserted at the caret position (which should then move over by the length of the text).

So, you would need to look at InputField’s selectionAnchorPosition and selectionFocusPosition and/or caretPosition, then replace the corresponding range of its text, stuff this back into iField.text, and update the selection anchor/focus and caret. (All this could be wrapped up in a simple “selectedText” getter/setter… somebody should submit that to Unity as a feature request, maybe with working code, as it’s silly to make us work this hard for it!)

Anyway, it’s quite a daunting first task… would you consider tackling something different? Working with UI stuff means understanding Unity events; indeed, for many things (like simply setting text based on a button clicked) you don’t need to write any code at all. So maybe you’d enjoy working through my fun video tutorial on Unity events?

1 Like

…Of course another way to tackle the custom onscreen keyboard problem would be to construct a fake key Event, and pass it to KeyPressed. Then everything should happen exactly as if a real key were pressed.

1 Like

Thank you for the reply! I will look at your tutorial videos and learn more!