microsoft-ui-xaml: Gathering keyboard input doesn't work (winui 3)
Describe the bug
I simply can’t believe that this is an issue in 2022:
There’s simply no reliable way to capture keyboard in winui – I have no idea what to do.
I won’t reiterate what Gavin Williams already said in #3986 – we don’t have a CoreWindow in WinUI 3, and I don’t see any way to reliably capture keys.
Steps to reproduce the bug
I have a DataGrid and I want to capture keyboard input for it.
Obviously, since I don’t have access to CoreWindow, I tried to capture its KeyDown or PreviewKeyDown
Of course it doesn’t work.
Then I tried to capture the parent’s Keydown:
var grid = Parent as Grid;
Debug.Assert(grid != null);
grid.IsTabStop = true;
grid.IsHitTestVisible = true;
grid.Background = new SolidColorBrush(Colors.Transparent);
grid.PreviewKeyDown += DecentDataGrid_KeyDown;
This doesn’t work either. There’s simply no way to capture this. How am I supposed to create a half decent app when I can’t even handle an INS hotkey?
LATER EDIT: Turns out the issue with the datagrid not handling KeyDown happens when it’s placed on a pivot.
Expected behavior
- There should be a way to globally capture keyboard, like in UWP
- When capturing
KeyDown, this should work reliably (see below, when Datagrid is on a Pivot)
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.0.4
Windows app type
- UWP
- Win32
Device form factor
Desktop
Windows version
Windows 10 (21H1): Build 19043
Additional context
No response
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 3
- Comments: 37 (2 by maintainers)
I don’t use MAUI, but in desktop applications, the main window procedure does not receive keyboard messages if there are child controls which can take focus. For example if I test with Notepad and MS Spy++ to check messages, keyboard messages are received by the child Edit control because it has the focus by default on my Windows 10 OS
6/23/2024
And Microsoft still has no idea of this issue, well that sucks.
what I did for a fix is simply this:
Now let me explain what the above code does, firstly the
isTabStopmakes sure that my component can receive focus when the user presses the Tab key.After that, I added the
KeyDownmethod/listener event to the page which runs a certain function when the user clicks on the keyboard keys.Now the
Focusmethod was supposed to make sure the component becomes focused as soon as the page (not window in my case) loads but it does not work that way for unknown reasons (well this is Microsoft we are talking about).As for the
LostFocusassignment method, I am simply saying as soon as the page loses its focus, give it back a new focus so the keyboard clicks event can work fine, and funny enough the “Focus(FocusState.Programmatic)” method works well in theLostFocusmethod/callback/listener but not when I call it on the page init.While I can’t tell if this will be a fix for some people I guess this can give you some certain idea for a workaround to this.
Note: the Microsoft docs and IDE intelligence can both help and break you at the same time, it took me forever to realize that WinUi3 does not have a CoreWindow after I saw it was returning null.
Edit: I noticed if you give time for the app to load even after the
InitializeComponentmethod you can call theFocusmethod and it will work fine or you can call it on theLoadedassign method and it will work fine.I hope this helps.
@applefanbois I am sorry, I missed the part about WebView2 in the initial postings on this issue. I was responding to the point:
This is not a point that I necessarily disagree with, but the fact remains that there is no provision for it currently in Xaml and its work would be on the level of a Feature to implement it. I am only surmising (since I wasn’t here at the time), but I guess that that wasn’t built into Xaml because it originated in UWP and there was a Core Window available that could provide that capability. However, when moving this from UWP to desktop, the new windowing infrastructure does not provide that and hence there is no easy way to do it in Xaml. Again, I don’t disagree that it would be useful, but at this point it would be a feature request. We have had a number of discussions about achieving this capability, but there is no work currently planned to do so.
As to the Webview issues (I am still not seeing it in the original posts, but can response to it). For the most part from my testing today, things are working as expected, although not necessarily as we want. If you have a webview in your scene and focus is anywhere other than the Webview, then keys event routing works as designed. The issue is that WebView, as I am sure you are aware, is an open source component and so we (Microsoft) do not have as much control over it as we did when we were using IE. WebView is currently really bad at cooperating with its host; It is mostly just a black box that Xaml pushes data (like urls) into and then it does its own thing. Hence, if the Webview has focus, the key input will go (as it should) to the Webview first. But the WebView doesn’t tell Xaml whether or not it handled it. We have similar issues with scroll chaining and pointer events as well. Ideally, WebView would be able to tell its host whether it did anything with an input and whether the host should try to do something. Currently that doesn’t happen, but we do have some people who have been looking at this. Now, if you are saying that WebView2 is eating keyboard input when it doesn’t have the focus, that would definitely be a bug and if you provide a repro we can look at it.
@jtorjo Thanks for the repro application. I am not familiar with the community toolkit, so it helped let me look at it. The behavior you speak about is probably more about the controls than it is input. When keyboard input comes in, we attempt to deliver it first to the element with keyboard focus and then each ancestor above it until someone tells us they handled it.
So, when you first fire up you application keyboard focus is on the first pivot tab and any keyboard input would go first to the pivot control where your event handler fires. Since it does not mark the event as handle, we attempt to fire it on each ancestor as well (but they don’t do anything with it).
Once you select an item, you have move keyboard focus to that item. Keyboard input first goes to that item (which does nothing) then works its way up the tree where (because it is never marked as handled) it goes through both the DataGrid and the Pivot control event handlers.
Tapping on the “Tab Two” (as you probably now have guessed) moves keyboard focus back to the pivot before it changes tabs.
And so on. From input’s perspective, this is how it is supposed to work.
Is it unfortunate behavior (especially for this particular scenario), probably, but there are ways to work around it. I am not a controls person, but I suspect that you could listen for the selection changed event and force focus into the first item of the list whenever a tab changed. You could probably even listen to focus events and remember which element had focus when the tab was last active and set focus back into it.
Or you could certainly request this feature be added to the Pivot control itself, but again, I am not a controls person so I don’t know how that would fit into their plans or whether there would be a generic enough implementation that it would make sense to add it.
Can’t believe we are here because Microsoft forgot that a PC use a mouse and … a keyboard.
I got it going, thanks to Castorix and some digging around on Google. It needs some tidying up after being hacked together but seems to work ok.
A classic way in Win32 is with Hooks For example, in this test, a Beep is generated on [Ctrl + P] , which should work with the focus in any control :
with declarations :