title: How to bind keyboard input command to main window?
body: <p>I am relatively new to WPF Binding, and having a difficulty in binding a <code>ICommand</code> implemented Class which is to detect user keyboard input in the window? At first I think I need to bind it to the Window on XAML, but, it appears that Window doesn't have <code>Command</code>.</p>
  
  <p><strong>This is my ViewModel Class</strong></p>
  
  <pre><code>public class NoteBoxViewModel
  {
      public MusicalNotation MusicalNotation { get; set; }
      public ICommand KeyboardHotkeyCommand { get; private set; }
      public bool IsSelected { get; set; }
  
      public NoteBoxViewModel()
      {
          MusicalNotation = new MusicalNotation();
          KeyboardHotkeyCommand = new KeyboardHotkeyCommand(this);
          IsSelected = true;
      }
  }
  </code></pre>
  
  <p><strong>This is my KeyboardHotkeyCommand Class</strong></p>
  
  <pre><code>public class KeyboardHotkeyCommand : ICommand
  {
      public event EventHandler CanExecuteChanged
      {
          add { CommandManager.RequerySuggested += value; }
          remove { CommandManager.RequerySuggested -= value; }
      }
  
      private NoteBoxViewModel _vm;
  
      public KeyboardHotkeyCommand(NoteBoxViewModel vm)
      {
          _vm = vm;
      }
  
      public bool CanExecute(object parameter)
      {
          return _vm.IsSelected;
      }
  
      public void Execute(object parameter)
      {
          if (Keyboard.IsKeyDown(Key.OemPeriod))
          {
              _vm.MusicalNotation.Note = Notes.Blank;
          }
          else if (Keyboard.IsKeyDown(Key.D0))
          {
              _vm.MusicalNotation.Note = Notes.Rest;
          }
          else if (Keyboard.IsKeyDown(Key.D1))
          {
              _vm.MusicalNotation.Note = Notes.N1;
          }
          else if (Keyboard.IsKeyDown(Key.D2))
          {
              _vm.MusicalNotation.Note = Notes.N2;
          }
          else if (Keyboard.IsKeyDown(Key.D3))
          {
              _vm.MusicalNotation.Note = Notes.N3;
          }
          else if (Keyboard.IsKeyDown(Key.D4))
          {
              _vm.MusicalNotation.Note = Notes.N4;
          }
          else if (Keyboard.IsKeyDown(Key.D5))
          {
              _vm.MusicalNotation.Note = Notes.N5;
          }
          else if (Keyboard.IsKeyDown(Key.D6))
          {
              _vm.MusicalNotation.Note = Notes.N6;
          }
          else if (Keyboard.IsKeyDown(Key.D7))
          {
              _vm.MusicalNotation.Note = Notes.N7;
          }
          else if (Keyboard.Modifiers == ModifierKeys.Control &amp;&amp; Keyboard.IsKeyDown(Key.OemPlus))
          {
              _vm.MusicalNotation.Octave++;
          }
          else if (Keyboard.Modifiers == ModifierKeys.Control &amp;&amp; Keyboard.IsKeyDown(Key.OemMinus))
          {
              _vm.MusicalNotation.Octave--;
          }
      }
  }
  </code></pre>
  
  <p><strong>This is my Notes Enumeration</strong></p>
  
  <pre><code>public enum Notes
  {
      N1,
      N2,
      N3,
      N4,
      N5,
      N6,
      N7,
      Rest,
      Blank
  }
  </code></pre>
  
  <p>Questions :</p>
  
  <ol>
  <li>How to bind <code>KeyboardHotkeyCommand</code> to my XAML Class so I can detect user input (<strong>input does not going to <code>Textbox</code> or any sort of text editor first</strong>)? I also tried to bind it to <code>Window_IsKeyDown</code> (it's a bad practice, I know) but it failed afterall. Is it possible to achieve it without <code>event</code>?</li>
  <li>In my <code>KeyboardHotkeyCommand</code>, there's an event named <code>CanExecuteChanged</code>. I filled it with exact direction with what is told here : <a href="http://blog.lab49.com/archives/2650" rel="nofollow">http://blog.lab49.com/archives/2650</a>. But I have no idea why it's filled that way. Can anyone explain to me?</li>
  <li>I also studied the tutorial here : <a href="http://reedcopsey.com/series/windows-forms-to-mvvm/" rel="nofollow">http://reedcopsey.com/series/windows-forms-to-mvvm/</a> and it seems that he can directly instantiate the <code>ICommand</code> with <code>ActionCommand</code>, but I can't find any in my Visual Studio. Anyone knows why?</li>
  </ol>
  
  <p>Note :</p>
  
  <ol>
  <li>Even though I'm new to MVVM (just knew it yesterday), but I want to use as much as MVVM pattern as possible, so, I would like to avoid to use events (if that's possible, of course), since I read that it's not a good MVVM practice.</li>
  <li>I think, whatever my <code>MusicalNotation</code> class look like, it'll not affect the solution of this problem at all, since it's just a 'Model'. But if it's needed, I am 100% willing to show you as well.</li>
  </ol>