Forum Discussion

sealfoss's avatar
sealfoss
Explorer
2 years ago

How to Show and Hide the OVRVirtualKeyboard Building Block Prefab

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.

4 Replies

  • 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's avatar
    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's avatar
    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.

    • tranquanglam's avatar
      tranquanglam
      Honored Guest

      Hi, I am doing an ingame search function, there is a text input and whenever the user choose it, the keyboard should show up. I did the same as your advice, it's clearly that the documentation of Meta is deprecated.
      I tried 2 options, both are in your advice:

      1/ Add the "OVRVirtualKeyboardInputFieldTextHandler" component and assign the "InputField" for it. Then assign this component to OVRVirtualKeyboard.

      2/ Add a custom "CustomOVRVirtualKeyboardInputFieldTextHandler" to the InputField object itself. 

      I have configurated every settings: render model support: enabled, virtual key support: required. The scene also has an "Pointable Canvas Module" to ensure that it is interactable. But when I click the input field, it does not show the keyboard.
      Can you give me some advice on how to fix this ? Much appreciated