Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to get typed letter from native keyboard without input field?

Discussion in '2D' started by wamballa, Aug 28, 2022.

  1. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    Hi there,

    I have a game where I want to capture keyboard input as it's typed...it's a typing game. Type a letter A and something happens. So no text input field.

    On my PC I'm using this and it works great:

    void Update ()
    {
    foreach (char letter in Input.inputString)
    {
    wordManager.TypeLetter(letter);
    }
    }

    For iOS I've tried the following but doesn't seem to work.

    private void OnGUI()
    {
    Event e = Event.current;
    if (e.keyCode == KeyCode.Return)
    {
    returnPressed = true;
    }
    if (e.keyCode == KeyCode.Space)
    {
    spacePressed = true;
    }
    // do a bunch of bools for each keyboard button pressed?!
    }

    Any ideas on how I can get this working?

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Last I checked, I don't think Apple will let you make your own graphic keyboard: they want you to use the in-built system one that Unity brings up for you when you edit an InputField.

    There are ways to make that keyboard appear, I believe by using the MobileKeyboard API:

    https://docs.unity3d.com/Manual/MobileKeyboard.html

    Unity provides some level of integration and I haven't used it. It might be necessary for you to write some native iOS keyboard integration to get the exact behavior you want and still be compliant with Apple's policies.
     
  3. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    Thanks @Kurt-Dekker

    This is how I solved this one in the end:

    public class NativeKeyboardInput : MonoBehaviour
    {
    public TMP_InputField MyInputfield;

    void Start()
    {
    MyInputfield.onSubmit.AddListener(DoStuffWhenSubmitted);
    MyInputfield.onValueChanged.AddListener(DoStuffWhenValueChanged);
    MyInputfield.Select();
    }

    void DoStuffWhenSubmitted(string input)
    {
    // do stuff here
    }
    void DoStuffWhenValueChanged(string input)
    {
    if (input != "")
    {
    input = input.ToLower();
    char letter = input[0];
    wordManager.TypeLetter(letter); // send letter to a manager to do stuff with
    MyInputfield.text = "";
    }

    }
    private void Update()
    {
    if (!MyInputfield.isFocused)
    {
    print("LOST FOCUS");
    MyInputfield.Select();
    }
    }
    }
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Please use the dedicated forums available rather than the 2D forum. The UI and TMP forum is here.

    Also, so you know, when posting code please use code-tags.

    Thanks.
     
  5. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    Thanks and sorry