Repository: AvaloniaUI/Avalonia
File: src/Windows/Avalonia.Win32.Interoperability/WinForms/WinFormsAvaloniaControlHost.cs (C#)

Code before:
using WinFormsControl = System.Windows.Forms.Control;

namespace Avalonia.Win32.Interoperability;

/// <summary>
/// An element that allows you to host a Avalonia control on a Windows Forms page.
/// </summary>
[ToolboxItem(true)]
public class WinFormsAvaloniaControlHost : WinFormsControl
{
    private AvControl? _content;
    private EmbeddableControlRoot? _root;
    private IDisposable? _postProcessInputDisposable;

    private IntPtr WindowHandle => _root?.TryGetPlatformHandle()?.Handle ?? IntPtr.Zero;

    /// <summary>
    /// Initializes a new instance of the <see cref="WinFormsAvaloniaControlHost"/> class.
    /// </summary>
    public WinFormsAvaloniaControlHost()
    {
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);

        var inputManager = AvaloniaLocator.Current.GetRequiredService<IInputManager>();

        if(inputManager != null )
        {
            _postProcessInputDisposable = inputManager.PostProcess.Subscribe(new AnonymousObserver<RawInputEventArgs>(PostProcessInput));
        }
    }

    private void PostProcessInput(RawInputEventArgs args)
    {
        if (_root?.IsKeyboardFocusWithin == false)
            return;

        if(!args.Handled && args is RawTextInputEventArgs textArgs)
        {
            var text = textArgs.Text;

            if (!string.IsNullOrEmpty(text))
            {
                args.Handled = this.ProcessDialogChar(text[0]);
            }
        }
    }

    /// <summary>
    /// Gets or sets the Avalonia control hosted by the <see cref="WinFormsAvaloniaControlHost"/> element.
    /// </summary>
    public AvControl? Content

Review comment (suggestion):
`inputManager` can't be null here, `GetRequiredService()` would have thrown. Use `AvaloniaLocator.Current.GetService()` instead.

Code after:
using WinFormsControl = System.Windows.Forms.Control;

namespace Avalonia.Win32.Interoperability;

/// <summary>
/// An element that allows you to host a Avalonia control on a Windows Forms page.
/// </summary>
[ToolboxItem(true)]
public class WinFormsAvaloniaControlHost : WinFormsControl, IMessageFilter
{
    private AvControl? _content;
    private EmbeddableControlRoot? _root;
    private IDisposable? _postProcessInputDisposable;

    private IntPtr WindowHandle => _root?.TryGetPlatformHandle()?.Handle ?? IntPtr.Zero;

    /// <summary>
    /// Initializes a new instance of the <see cref="WinFormsAvaloniaControlHost"/> class.
    /// </summary>
    public WinFormsAvaloniaControlHost()
    {
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);

        var inputManager = AvaloniaLocator.Current.GetService<IInputManager>();

        if(inputManager != null )
        {
            _postProcessInputDisposable = inputManager.PostProcess.Subscribe(new AnonymousObserver<RawInputEventArgs>(PostProcessInput));
        }
    }

    private void PostProcessInput(RawInputEventArgs args)
    {
        if (_root?.IsKeyboardFocusWithin == false)
            return;

        if(!args.Handled && args is RawTextInputEventArgs textArgs)
        {
            var text = textArgs.Text;

            if (!string.IsNullOrEmpty(text))
            {
                args.Handled = this.ProcessDialogChar(text[0]);
            }
        }
    }

    /// <summary>
    /// Gets or sets the Avalonia control hosted by the <see cref="WinFormsAvaloniaControlHost"/> element.
    /// </summary>
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]