Application-wide KeyBinding

Nov 24, 2014 at 3:38 PM
I currently have the following code in my MainWindow.xaml to execute a command when Ctrl+N is pressed. I am curious if there is something in SimpleMVVM that I can use that is simpler, more streamlined, etc. than what I am currently doing.

MainWindow.xaml
<Window x:Class="CallTracker.WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 ...
    <Window.InputBindings>
        <KeyBinding Command="{Binding NewCallCommand}" Modifiers="Control" Key="N"/>
    </Window.InputBindings>
 ...
</Window>
And I have the following in my MainPageViewModel.cs to handle the key press:
    public class MainPageViewModel : ViewModelBase<MainPageViewModel>
    {

        private readonly DelegateCommand newCallCommand;

        // Default ctor
        public MainPageViewModel()
        {
            newCallCommand = new DelegateCommand(NewCall);
        }


        public ICommand NewCallCommand { get { return newCallCommand; } }

        private void NewCall()
        {
            MessageBox.Show("NewCallCommand executed.");
        }
    }
Coordinator
Nov 25, 2014 at 1:58 PM
To me it looks like you're on the right track. For more info, this article seems like a good one to check out: http://www.shujaat.net/2011/03/wpf-binding-inputbindings-to-view-model.html.

Cheers,
Tony