chwar / XRUI

Unity Extension for Responsive UI in XR
https://chwar.github.io/xrui/
MIT License
21 stars 2 forks source link

System Keyboard support? #3

Open kamend opened 3 months ago

kamend commented 3 months ago

Not exactly an issue, but did you manage to get UIToolkit Input Fields working with the System Keyboard?

chwar commented 3 months ago

Hi! Yes, they are working mostly out of the box (at least for Android). I have also been successful with a Meta Quest 2 and a Hololens. Depending on the device and the scenario, you can manipulate the keyboard's behaviour. You can do this with Unity's TouchScreenKeyboard.

Here's an example:

private TextField _username;
private TouchScreenKeyboard _keyboard;

_username = _container.GetXRUIVisualElement<TextField>("username");

// Open keyboard when getting focus
_username.RegisterCallback<FocusInEvent>((e) =>
{
    _keyboard = TouchScreenKeyboard.Open("");
    _keyboard.text = String.Empty;
    _keyboard.active = true;
});

// Close keyboard when losing text field focus 
_username.RegisterCallback<FocusOutEvent>((_) =>
{
    _keyboard.active = false;
});

// Update text field with keyboard value
_username.RegisterValueChangedCallback(evt =>
{
    if (_keyboard != null)
        _username.value = _keyboard.text;
});

Hope this helps :)