简体   繁体   中英

KeyDown event in C# for system keys

I'm trying to capture any KeyDown event regardless of what item has the focus. I have the following code in my constructor:

Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;

And my function here:

void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
    //Stuff in here
}

This works fine for regular keys, (arrows, letters) but does not receive events for special keys (like Enter or Tab ).

How do I intercept the event for the Enter or Tab keypress and keep the system from handling it? I need to get the event all the time, regardless of what object has the focus.

The process for handling keys is quite complex. KeyDown is actually quite late in the process and does not include "control" keys. To get those keys you need to override IsInputKey (for Tab and Control keys) or ProcessDialogKey (for arrow keys). See some excellent documentation here https://msdn.microsoft.com/en-us/library/vstudio/ms171535%28v=vs.100%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM