Search code examples
c#wpfeventsevent-handlingcommandbinding

CommandBinding Ctrl + Spacebar


I handle commands inside a RoutedCommand class that implements RoutedUICommand. This would help me to block or override a command by checking their CanExecute and Execute if needed. I can override EditingCommand, ApplicationCommand, etc.. One of the command that I cannot even handle is Ctr + Spacebar. Is it a MediaCommand or some other types that I cannot find? I guess it is been handled somewhere else, and that's why I cannot control it.


Solution

  • You can create your own custom command or you can simply add new gesture for predefined command, e.g.:

    public Window1()
        {
            InitializeComponent();
            ApplicationCommands.Find.InputGestures.Add(new KeyGesture(Key.Space, ModifierKeys.Control));
            CommandBinding commandBinding = new CommandBinding(ApplicationCommands.Find, myCommandHandler);
            this.CommandBindings.Add(commandBinding);
        }
    
        private void myCommandHandler(object sender, ExecutedRoutedEventArgs args)
        {
            MessageBox.Show("Command invoked!");
        }