I have an application that has menu keyboard shortcuts and text boxes. I want the keyboard shortcuts to be disabled when the text box has focus, but I can't figure out an easy way to do this. I could handle the text box's PreviewKeyDown event, but sending a KeyDown event doesn't cause the TextInput event to trigger so I'd have to manually trigger the TextInput event myself, and I'd have to make sure that every text box overrides the PreviewKeyDown event and creates a TextInput event.
Is this the only way suppress menu keyboard shortcuts when a textbox has focus or is there another way that is less error prone?
EDIT:
Here's how I'm adding the keyboard shortcut:
var kgc = new NuiWpfCore.Input.UnrestrictedKeyGestureConverter(); // allows gestures without modifier keys
var result = kgc.ConvertFromString(s) as NuiWpfCore.Input.UnrestrictedKeyGesture;
m_KeyBinding = new KeyBinding();
m_KeyBinding.Command = KeyBindingCommand;
m_KeyBinding.Modifiers = result.Modifiers;
m_KeyBinding.Key = result.Key;
m_Parent.InputBindings.Add(m_KeyBinding); // m_Parent is of type UIElement
Can you provide more inputs as in HOW are you registering the Keyboard shortcuts? Using
KeyBinding
? If so it already needsCommand
specified. So in the Canexecute of the command return false if the textbox is in focus.This will disable keyboard shortcuts. Some source ocde from your side might be useful.
EDIT
SO now that you have
KeyBinding
usingKeyBindingCommand
which look like aRoutedCommand
to me. If so do command bindings havingCanExecute
function.In the
CanExecute
handler....CanExecutedRoutedArgs
may / may not be correct...The code above is only for illustration.