2
votes

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.

1
You may want to look into Windows hooks. They'll be more useful for your purposes than trying to catch system events. I can find and post some links to hooking documentation if you wantrmn36
You should look at the documentation on key down, key down is for keys that are going to "displayed".Hogan
You might try also handling key up and key press to cover the "waterfront"Paul Sasik
Sounds like at a high level, you're working on a key logger (if that's the actual intended purpose or not, is a different story) you might want to search how to build a keylogger in c# as a basis of where to start. As rmn36 stated, it will require windows hooks to do what you're looking to do.user2366842
You can't get non-system keys from this event. Horribly underspecified, but the basic message is that you can't break basic keyboard usage. You'll only ever get Tab when your UI has less than 2 focusable controls. You'll only get Enter when the focus isn't on a control that uses it, like a Button. My crystal ball says that you're looking for the CharacterReceived event instead.Hans Passant

1 Answers

0
votes

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