How does keyboard input work?

Starting in 3D WebView v4.3, WebViewPrefab and CanvasWebViewPrefab automatically detect keyboard input by default. So, no additional code or configuration is needed to send input from the native hardware keyboard or on-screen Keyboard prefab to a webview. If you wish to handle keyboard input manually, please see the section below. For versions of 3D WebView prior to v4.3, keyboard input was handled manually like demonstrated in the demo scenes' scripts, like SimpleWebViewDemo.cs.

Limitations

  • 3D WebView's automatic handling of native hardware keyboard input currently relies on Unity's Legacy Input Manager, so it will be disabled if "Player Settings -> Active Input Handling" is set to "Input System Package". To use 3D WebView's native keyboard input handling, please set "Active Input Handling" to either "Input Manager" or "Both". I have a feature request to support the new Input System Package, and I will update this article when I add support for it in the future.

  • 3D WebView for Windows and macOS doesn't yet support IME for composing Chinese, Japanese, or Korean text.

Manually handling keyboard input

If you wish to handle keyboard input manually, you can do the following:

Together, these steps might look like this:

using UnityEngine;
using Vuplex.WebView;

class ManualKeyboardExample : MonoBehaviour {

    // Set these fields via the Editor.
    // https://support.vuplex.com/articles/how-to-reference-a-webview
    public WebViewPrefab webViewPrefab;
    public Keyboard keyboard;

    void Start() {

        // Or disable KeyboardEnabled via the Editor.
        webViewPrefab.KeyboardEnabled = false;

        // Send keys from the Keyboard prefab to the webview.
        keyboard.KeyPressed += (sender, eventArgs) => {
            webViewPrefab.WebView.SendKey(eventArgs.Value);
        };

        // Send keys from the native hardware keyboard to the webview.
        var nativeKeyboardListener = Vuplex.WebView.Internal.NativeKeyboardListener.Instantiate();
        nativeKeyboardListener.KeyDownReceived += (sender, eventArgs) => {
            var webViewWithKeyDown = webViewPrefab.WebView as IWithKeyDownAndUp;
            if (webViewWithKeyDown != null) {
                webViewWithKeyDown.KeyDown(eventArgs.Key, eventArgs.Modifiers);
            } else {
                webViewPrefab.WebView.SendKey(eventArgs.Key);
            }
        };
        nativeKeyboardListener.KeyUpReceived += (sender, eventArgs) => {
            var webViewWithKeyUp = webViewPrefab.WebView as IWithKeyDownAndUp;
            webViewWithKeyUp?.KeyUp(eventArgs.Key, eventArgs.Modifiers);
        };
    }
}