cancel
Showing results for 
Search instead for 
Did you mean: 

How to Show and Hide the OVRVirtualKeyboard Building Block Prefab

sealfoss
Explorer

I'm attempting to show and hide the OVRVirtualKeyboard Building Block Prefab in Unity 3D. Setting the gameobject to active or inactive either doesn't work, or it will produce Null Reference errors, depending on when the operation is performed. Commands are obviously controlling it from outside of unity, how to I interact with them? Is there an easy or proper way to hide and show this asset on command? I have resorted to essentially gluing it to the back of the user's head when I want to hide it, and putting it elsewhere when I don't. Any advice is appreciated, thanks in advance.

3 REPLIES 3

srFawlty
Explorer

How do you reference the keyboard? GameObject.Find won't work if the keyboard is inactive (it took me like a day to realize it). What I do is to pass it like a public var and then loading it with the keyboard in the unity editor

....
    public OVRVirtualKeyboard virtualKeyboard;
....
    public void openKeyboard()
    {
        virtualKeyboard.gameObject.SetActive(true);
    }
....

Good luck! 

 

Fahruz
Expert Protege

The OVRVirtualKeyboard script has ShowKeyboard() HideKeyboard() methods, which are private, but they're called when the script is enabled/disabled. So maybe just enable/disable the script?

Tux_3do
Honored Guest

I used the VirtualKeyboard Buildingblock to add the Keyboard to View. The documentation still states to update the "Text Commit Field". This is deprecated! You should add the a `OVRVirtualKeyboardInputFieldTextHandler` to a (legacy) TextField (it does not work with TextmeshPro). Then you can set your TextField as `TextHandler` of your `VirtualKeyboard` Gameobject.

So what I did, I created a `CustomOVRVirtualKeyboardInputFieldTextHandler` inheriting from `OVRVirtualKeyboardInputFieldTextHandler`. This one handles switching your keyboards context.

 

using UnityEngine;
using UnityEngine.UI;

class CustomOVRVirtualKeyboardInputFieldTextHandler : OVRVirtualKeyboardInputFieldTextHandler
{
    public OVRVirtualKeyboard keyboard;
    
    public new void Start()
    {
        InputField = GetComponent<InputField>();
        keyboard.EnterEvent.AddListener(KeyboardEnter);
        base.Start();
    }
    public void Update()
    {
        if (IsFocused) {
            BindKeyboard();
        }
        // This works in debug mode. But when the game is built, the InputField loses focus on next frame, so the keyboard gets immediatly unbound. 
        //  Tested with Meta XR SDK v60
        // if (!IsFocused && (object)keyboard.TextHandler == this) {
        //     UnbindKeyboard();
        // }
    }

    protected void KeyboardEnter()
    {
        UnbindKeyboard();
    }

    protected void BindKeyboard()
    {
        keyboard.TextHandler = this;
        keyboard.gameObject.SetActive(true);
    }

    protected void UnbindKeyboard()
    {
        InputField.DeactivateInputField();
        keyboard.gameObject.SetActive(false);
        keyboard.TextHandler = null;
    }
}

 

 

There is one catch. I noticed that the TextField loses focus on the next frame after clicking it. So I had to remove the `UnbindKeyboard()` from the Update-Method, when it is not focused anymore.
This does not happen when I run the App in Debug Mode (Unity "Play"-Button) and use it on my Quest 3 (via Link Cable). This only happens when I use "Build and Run" in Unity. I did not test it when I copy the app the my Quest 3 via Sideload yet, but I do not expect any difference.

PS: The `InputField` property gets set automatically, as it should of course always be the same InputField the component was added to. But as this field is `private` in the base class, I did not manage to hide it in the Editor. Tips on this is appreciated.