Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Showing Window from WinForms app doesn't allow text input #16046

Closed
pauldumais opened this issue Jun 17, 2024 · 4 comments
Closed

Showing Window from WinForms app doesn't allow text input #16046

pauldumais opened this issue Jun 17, 2024 · 4 comments

Comments

@pauldumais
Copy link

pauldumais commented Jun 17, 2024

Describe the bug

When a WinForms app shows an Avalonia Window with a textbox on it, the textbox does not accept keyboard input. The mouse clicks work and we can right click on the textbox to copy and paste, simply keyboard input does not work.

        var button = new System.Windows.Forms.Button();
        button.Text = "Show Window";
        button.Click += (sender, args) =>
        {
            var window = new AvaloniaTestWindow();
            window.Show();
        };
        this.Controls.Add(button);

Wrapping a UserControl in a Windows Form is a temporary workaround, but it would be nice to use native Avalonia Window objects. What's interesting is that certain keys work, such a Del, Backspace, Insert, Home, End, ctrl+v but none of the character keys work.

To Reproduce

  1. Create a WinForms app
  2. Add Avalonia nugets
  3. Create Avalonia Window with TextBox
  4. Create a button on the winforms app to Show() the Avalonia Window
  5. Run the app, the TextBox on the Avalonia Window does not accept any keyboard input.

Expected behavior

The TextBox should accept input.

Avalonia version

11.0.10

OS

Windows

Additional context

No response

@thevortexcloud
Copy link
Contributor

thevortexcloud commented Jun 18, 2024

I have seen this before, not sure what causes it though. Interestingly, it works fine if you just shove everything into the Winform interop control and use a winform window. Although you do have to deal with winforms swallowing certain keypresses by default instead of passing them to the Avalonia control. Obviously using a winform window when trying to move away from winforms is not ideal.

@pauldumais
Copy link
Author

I'm digging into the issue in the Avalonia source code, it looks like WM_KEYDOWN events are being fired but not WM_CHAR events, this issue appears to be very similar to the behaviour of this issue:
#785

The resolution for that issue was to give focus to the window after it was shown, so it may be a similar situation with the new avalonia child window not receiving proper focus when being opened from a winforms app.

@pauldumais
Copy link
Author

Digging down further, it appears the issue is with the WinForms message loop, when running inside a WinForms application the message loop is handled by the WinForms Application class, while when running inside an Avalonia window it uses it's own Win32DispatcherImpl properly routing events. The WinForms Application message loop is looking for the Control.FromChildHandle() of the Window and it is not finding the Control to route the WM_CHAR event to. When running inside an Avalonia Interop hosting control all works fine as it finds the parent Form window from Control.FromChildHandle().

@pauldumais
Copy link
Author

I think I found a solution, it looks like the event loop was not started properly for the Avalonia window because it was launched from a WinForms app context relying on the WinForms event loop which was looking for a top level Windows.Form.Control to dispatch the WM_CHAR events to. The solution was to force the Avalonia Event loop to start with the new Window, so instead of Window.Show() I would call this method, this causes the Window to be displayed but also for the Avalonia Event loop to start:

Application.Current.Run(avaloniaWindow);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants