By testing, sometimes pressing Tab key will not trigger CoreWindow.KeyDown
event handler when there are some controls such as Button
can get focus in a page. You could try to add a UIElement.KeyDown event or a UIElement.PreviewKeyDown event to a page(such as MainPage
) in xaml file.
Update:
When you use CoreWindow.KeyDown
event and there are controls which could get focus in your page, pressing Tab key will let the focus step into a tab sequence instead of triggering the CoreWindow.KeyDown
event. The CoreWindow.KeyDown
event could be triggered when Tab key is pressed and the focus locates at the last control which could get focus.
If you want CoreWindow.KeyDown
event to be triggered when Tab key is pressed, you could set TabNavigation
as Once
in your page. If you want save the Tab key’s feature that stepping into tab sequence, we still suggest you use UIElement.KeyDown
event or UIElement.PreviewKeyDown
event.
For example:
Window.Current.Content.KeyDown += Content_KeyDown;
Update:
Window.Current.Content.KeyDown
event is a routed event. About routed event, you could refer to the document. A routed event is an event that is potentially passed on (routed) from a child object to each of its successive parent objects in an object tree.
In your scenario, you could monitor the value of e.OriginalSource
and you could view that when you step into the last control which could get focus by pressing Tab key the KeyDown
event will be triggered twice. In the second trigger, the value of e.OriginalSource
could be Windows.UI.Xaml.Controls.Frame
( may be different, subject to your observation in the second trigger). That’s because the routed KeyDown
event need to bubble to its parent object at this time. You could add some code to identify the second trigger.
For example:
private void Content_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.OriginalSource.ToString()!= "Windows.UI.Xaml.Controls.Frame")
{
//Just be fired once here
}
}
Note, you could try to use code e.Handled = true;
to stop the routed behavior referring to here. And, if you use Page.KeyDown
event, there is no situation that KeyDown event be triggered twice.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…