My program is quite large, and uses WPF, and I want to have a global shortcut key that uses 'R', with no modifiers.
There are many controls such as TextBox, ListBox, ComboBox, etc. that all use letters inside the control itself, which is fine - that's correct for me.
But - I want to keep that KeyDown event from bubbling up to the main window, where it would trigger the shortcut any time a user is typing the letter 'R' in a TextBox, for example.
Ideally, I would like to be able to do this without having to specify (and do if-then logic on) every instance/type of control that might receive normal alphabetical key presses (not just the TextBox controls, though they are the worst offenders).
How do I stop WPF KeyDown events from bubbling up from certain contained controls (such as TextBox)?
Asked
Active
Viewed 1.6k times
14

redroze
- 143
- 1
- 1
- 4
2 Answers
19
Simply check what the OriginalSource
is in your KeyDown
event handler on the Window:
private void Window_KeyDown(object sender, KeyEventArgs e) {
if(e.OriginalSource is TextBox || e.OriginalSource is DateTimePicker) //etc
{
e.Handled = true;
return;
}
}
Or if you are using InputBindings, experiment with setting e.Handled = true
in either the KeyDown
or the PreviewKeyDown
event on your Window, rather than the individual controls. In anyway, I think OriginalSource
is the key to your answer. (I swear that was not a pun).

Troels Larsen
- 4,462
- 2
- 34
- 54
-
2Setting `e.Handled` to `true` is definitely the right way to go. The first proposed solution doesn't actually prevent the bubbling. – Mathias Lykkegaard Lorenzen Jul 21 '14 at 21:37
-
@MathiasLykkegaardLorenzen I'm interested as how is it not preventing bubbling? – 123 456 789 0 Jul 21 '14 at 22:30
-
By "first proposed solution" I was referring to his own code, which basically just applies logic when `e.OriginalSource` is a `Window`. I wasn't referring to your code, @lll. In fact, I voted your code up. – Mathias Lykkegaard Lorenzen Jul 21 '14 at 22:46
-
This is not a bad idea -- however, it neglects any shortcut key that might be pressed while the Window is _not_ the primary focus, which is a common occurrence. – redroze Jul 22 '14 at 13:46
-
@redroze: Sorry, I don't understand what you mean by when the Window is not the primary focus? I don't expect you mean other windows. But if the KeyDown event happens somewhere within the Window, it will bubble to the handler, unless handled, right? – Troels Larsen Jul 22 '14 at 13:54
-
@TroelsLarsen It would bubble to this handler, but the "if" statement would then determine _not_ to execute any code. – redroze Jul 22 '14 at 14:01
-
@redroze: Ok, I am going to answer, but I am 99% sure I am misunderstanding you. Instead, invert the if, set `e.Handled = true` and return. – Troels Larsen Jul 22 '14 at 14:07
-
@TroelsLarsen: That is close... Unfortunately, there are other controls present that I want to be able to 'see' the shortcut -- such as when a PaneGroup has focus, 'R' should still activate the shortcut. – redroze Jul 22 '14 at 14:15
-
@redroze: Ok, then I'm afraid you'll either have to white- or blacklist which control types you want to allow the event for. But at least it will only be in one place. So if `OriginalSource` is a TextBox (and various other controls), handle it. Otherwise, let it bubble upwards to the Window. – Troels Larsen Jul 22 '14 at 14:23
-
@TroelsLarsen: ..which is what I have currently. I had hoped to find a way where I wouldn't have to specify a (lengthy) list of control types, but I suppose that's the price you pay for using an un-modified alphabetical character as a global shortcut key. I'm accepting your answer. – redroze Jul 22 '14 at 14:50
-
@redroze: I understand why you would avoid having to maintain that. You might be able to find some common trait among the controls (`Focusable == true` or perhaps `e.OriginalSource is Panel`) to help cut down on the list. – Troels Larsen Jul 22 '14 at 18:08
-
Not sure if I've misunderstood something about what OP wanted, but with this change in my code the handler gets called and is able to block the shortcut, but now the text doesn't appear in the text box _either_ seemingly because it stops the `TextInputEvent` being raised at all? I'm using `InputBindings` to handle the shortcuts so I can't check `OriginalSource` at that point. Perhaps needs to be a new question – T. Kiley May 20 '21 at 13:57
-
@T.Kiley I think that may well warrant a new question, yes. In this case (if I remember correctly - it's been 7 years) OP actually used a KeyDown event handler in his Window. – Troels Larsen May 22 '21 at 21:55
1
There is an event when you handle KeyDown
event and it should pass you a KeyEventArgs
. From there you can set the Handled
to true so that it won't bubble up.
Sample
private void TextBoxEx_KeyAction(object sender, KeyEventArgs e)
{
e.Handled = true;
}

123 456 789 0
- 10,565
- 4
- 43
- 72
-
3This will also prevent the TextBox from actually inputting Text, as it prevents the text composition event from being handled by the TextBox. – Troels Larsen Jul 21 '14 at 21:00
-
Um, no it won't unless you are **overriding** an event and not calling base.OnKeyPressed(e); that is `OnKeyPressed` then yes it won't handle inputting the text. – 123 456 789 0 Jul 21 '14 at 22:29
-
3@Ill: Then I must be misunderstanding what you are doing. Create an empty project with a `TextBox`, and add an `KeyDown` eventhandler with nothing but `e.Handled = true`. It will not accept any text. – Troels Larsen Jul 22 '14 at 05:30
-
-
1Also, I would have to add an event handler to each and every possible control (current and future) that might accept text input. – redroze Jul 22 '14 at 13:50