-1

I'm stuck with wpf commands and "non-trivial" hotkeys. I want to map "+" key to some command. Within i want it to keep working with any textbox. Below is the sample

Commands.cs

    public static class Commands
    {
        private static readonly ICommand _someCommand;

        static Commands()
        {
            _someCommand = new RoutedCommand("cmd", typeof(Commands), new InputGestureCollection { new KeyGesture(Key.OemPlus), new KeyGesture(Key.Add) });
        }

        public static ICommand SomeCommand
        {
            get { return _someCommand; }
        }
    }

MainWindow.xaml

<Window x:Class="WpfHotkeysTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfHotkeysTest="clr-namespace:WpfHotkeysTest"
        Title="MainWindow" Height="350" Width="525">
    <Window.CommandBindings>
        <CommandBinding Command="wpfHotkeysTest:Commands.SomeCommand" Executed="CommandBinding_OnExecuted"></CommandBinding>
    </Window.CommandBindings>
    <TextBox></TextBox>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void CommandBinding_OnExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        Debug.WriteLine("COMMAND! " + e.Source);
    }
}

The problem is when im focused on the textbox it does not handle pressed "+" key before command is executed. I want my key be displayed, but how do i achieve this in the best way?

UPD I dont want to execute command if the key was handled by the text box.

I know there is property CanExecuteRoutingEventArgs.ContinueRouting. But it executes both command and textbox handling

garf1eld
  • 104
  • 6
  • 1
    You could either implement the `CanExecuteRoutingEventHandler` for the command or manually check if any `TextBox` is focused, before executing the command. If any `TextBox` is focused, don't execute the command. – Stefan Over Aug 26 '14 at 12:19
  • Yes, I ended up with this solution so far. Thanks! – garf1eld Aug 26 '14 at 12:24
  • You should re-think about your use case. Do you really want to have only "+" as command trigger, rather than a combination with e.g. "ctrl"? What's the benefit of having one button pressed less? Is it really worth it? – Stefan Over Aug 26 '14 at 12:38
  • I agree ctrl++ would be better, but this question is about usability and customer wants it to be that way. – garf1eld Aug 26 '14 at 12:54

1 Answers1

0

The workaround I've found so far

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void CommandBinding_OnExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        Debug.WriteLine("COMMAND! " + e.Source);
    }

    private void CommandBinding_OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        if (IsEditableControlSelected())
        {
            e.ContinueRouting = true;
            return;
        }

        e.CanExecute = true;
    }

    private bool IsEditableControlSelected()
    {
        return Keyboard.FocusedElement is TextBox;
    }
}

This brings tears to my eyes, but at least this is better than nothing. Waiting for more solutions

garf1eld
  • 104
  • 6