2
votes

In my UWP (Xamarin Forms) app I have a barcode scanner which fills in text into Entries with every scan. The problem is that after I do a scan the keyboard pops up as the entry gets focused.

I was wondering if there is a way I can hide the soft keyboard without I have to bind to the Entry's FOCUSED property to manually set the entry to unfocused state. Is there a way I can tell the OS to hide the keyboard? I am not sure if this is possible.

1

1 Answers

2
votes

You'll need to create a dependency service and listen to the event that is called when the keyboard appears. You can set up the dependency service like this:

IKeyboard.cs (in your PCL project):

public interface IKeyboard
{
    event EventHandler<EventArgs> KeyboardShowing;
    event EventHandler<EventArgs> KeyboardHiding;
    void HideKeyboard();
}

Keyboard_UWP.cs (in your UWP project):

public class Keyboard_UWP : IKeyboard
{
    private InputPane _inputPane;
    public event EventHandler<double> KeyboardShowing;
    public event EventHandler<EventArgs> KeyboardHiding;

    public KeyboardVisibility_UWP()
    {
        _inputPane = InputPane.GetForCurrentView();
        _inputPane.Showing += OnInputPaneShowing;
        _inputPane.Hiding += OnInputPaneHiding;
    }

    private void OnInputPaneShowing(InputPane sender, InputPaneVisibilityEventArgs args)
    {
        KeyboardShowing?.Invoke(this, null);
    }

    private void OnInputPaneHiding(InputPane sender, InputPaneVisibilityEventArgs args)
    {
        KeyboardHiding?.Invoke(this, null);
    }

    public void HideKeyboard()
    {
        _inputPane.TryHide();
    }
}

Then in your PCL, you can listen to when it shows:

DependencyService.Get<IKeyboard>().KeyboardShowing += OnKeyboardShowing();

private void OnKeyboardShowing(object sender, EventArgs e)
{
    if (you want to hide the keyboard)
        DependencyService.Get<IKeyboard>().HideKeyboard();
}