xamlwinui-3winuiwindows-app-sdkwinui-xaml

How to prevent the touch keyboard in Windows from opening for the Touch machines?


I am building a custom keyboard for my WinUI3 application for Touch machines. I couldn't prevent the default Touch Keyboard from opening when a textbox receives focus. I tried the following approach where I have overridden the OnCreateAutomationPeer

protected override AutomationPeer OnCreateAutomationPeer()
{
    return new FrameworkElementAutomationPeer(this);
}

But this is not working?. I don't want to change any regitry keys to disable it because it will have the effect on entire apps not just my app. Any other way to do this?


Solution

  • Using the CoreInputView.PrimaryViewShowing event works on my end:

    <ToggleSwitch
        x:Name="TouchKeyboardToggleSwitch"
        IsOn="True"
        OffContent="Hide"
        OnContent="Show" />
    <TextBox />
    
    public SomePage()
    {
        InitializeComponent();
        CoreInputView.GetForCurrentView().PrimaryViewShowing += PrimaryViewShowing;
    }
    
    private void PrimaryViewShowing(CoreInputView sender, CoreInputViewShowingEventArgs args)
    {
        if (TouchKeyboardToggleSwitch.IsOn)
            return;
    
        _ = args.TryCancel();
    }