Gathering keyboard input doesn't work (winui 3)
See original GitHub issueDescribe 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
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:13 (2 by maintainers)
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.
Can’t believe we are here because Microsoft forgot that a PC use a mouse and … a keyboard.