microsoft / microsoft-ui-xaml

WinUI: a modern UI framework with a rich set of controls and styles to build dynamic and high-performing Windows applications.
MIT License
6.37k stars 683 forks source link

Keyboard entry is not working in XAML hosted MFC application. #9334

Open Ajith-GS opened 9 months ago

Ajith-GS commented 9 months ago

Describe the bug

The typed text from the keyboard is not reflecting in the textbox. The TextBox containing UserControl is hosted over the MFC dialog application with the help of XAML Islands APIs and by referring to the XAML Island sample application available in the following repo. https://github.com/microsoft/WindowsAppSDK-Samples/tree/main/Samples/Islands As a workaround, the issue can be fixed by setting the return value of the PreTransalateMessage function to FALSE.  We have faced the same issue in an ATL-based application as well. But the above workaround could not be applied in this case because the ATL dialogs do not have the PreTranslateMessage function.  What is the proper way to fix the above issue in both MFC and ATL dialogs? Any help is greatly appreciated. Thank you.

Steps to reproduce the bug

Sample application is attached. TestApp.zip

Expected behavior

Keyboard entry should reflect in TextBox control.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.4.5: 1.4.240411001

Windows version

Windows 10 (21H2): Build 19044

Additional context

No response

Bernd36 commented 4 months ago

I am experiencing related problems in an MFC MDI application. Text input into a TextBox on a Xaml Island works if the parent window is a CView derived class (CMFCWinUIAppView in the attached project), but it does not work when the parent window is a CDockablePane derived class (CClassView).

The KeyDown-Events of the page and text box are firing in both cases, but on the CDockablePane the text input does not appear in the the text box!

Screenshot:

MFCWinUIApp

Visual Studio Solution:

MFCWinUI.zip

@Ajith-GS : you mentioned a workaround with PreTranslateMessage. How does this work exactly? WM_KEYDOWN is not handled in ContentPreTranslateMessage, see MFCXamlIsland::PreTranslateMessage in my sample application, so ignoring the return value does not change anything, at least in my case.

By the way, the application throws an exception when trying to undock / dock the CDockablePane with the Xaml Island. Probably this is caused by a change of the parent window of CDockablePane. I tried to recreated the Island in this case, but this did not help (see CClassView::CheckParentWindow()).

If you want to reset the docking state, you have to delete the registry key Computer\HKEY_CURRENT_USER\Software\Local AppWizard-Generated Applications\MFCWinUIApp.