Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

uwp - Window.Current.CoreWindow KeyDown Event won't fire for "Tab" key Pressed in c#?

what I have Tried is ?

Window.Current.CoreWindow.KeyDown += keydown;
private void keydown(CoreWindow sender, KeyEventArgs args)
{
   //Printing entered key
}

I'm developing UWP application.In my app Window.Current.CoreWindow keydown event fired for all the keys in keyboard except "Tab" key.

I don't know why the event don't fire for that specific key?I want to do some actions while "Tab" key is pressed. Anyone know, how to fire the event when "Tab" key is pressed ?

question from:https://stackoverflow.com/questions/65932496/window-current-corewindow-keydown-event-wont-fire-for-tab-key-pressed-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...