title: Stripping modifiers from Windows Form Keys
body: <p>I'm currently working on a project that deals with setting hotkeys to perform actions. I have the hotkey functionality working, but I don't want to allow hotkeys to be set if the key is composed only of modifiers. For example, <kbd>Ctrl</kbd> + <kbd>F</kbd> is a valid hotkey but <kbd>Ctrl</kbd> + <kbd>Alt</kbd> is not.</p>
  
  <p>I've tried several methods to strip the modifier keys off of the actual Keys enumeration object, then check it against Keys.None to determine whether or not it is composed of only modifier keys. However, in practice, this is not working out so well.</p>
  
  <p>I tried this method first:
  </p>
  
  <pre><code>private Keys StripModifiers(Keys Key)
  {
      return Key &amp; ~Keys.Modifiers;
  }
  </code></pre>
  
  <p>That didn't work, as my Ctrl key seems to be invoking the PreviewKeypress method with a KeyData property of LButton | ShiftKey | Control, which the Keys.Modifiers bitmask apparently does not fully catch.</p>
  
  <p>I tried writing my own, more complex one:
  </p>
  
  <pre><code>private Keys StripModifiers(Keys Key)
  {
      return Key &amp;
          ~Keys.Alt &amp;
          ~Keys.CapsLock &amp;
          ~Keys.Control &amp;
          ~Keys.ControlKey &amp;
          ~Keys.LControlKey &amp;
          ~Keys.LMenu &amp;
          ~Keys.LShiftKey &amp;
          ~Keys.LWin &amp;
          ~Keys.MButton &amp;
          ~Keys.Menu &amp;
          ~Keys.NumLock &amp;
          ~Keys.RButton &amp;
          ~Keys.RControlKey &amp;
          ~Keys.RMenu &amp;
          ~Keys.RShiftKey &amp;
          ~Keys.RWin &amp;
          ~Keys.Scroll &amp;
          ~Keys.Shift &amp;
          ~Keys.ShiftKey;
  }
  </code></pre>
  
  <p>However that didn't work either because pressing the A key, which simply invoked the event with the KeyData property of 65, reduces it to Keys.None, so it's too restrictive.</p>
  
  <p>I'm pretty much at a loss at this point, has anybody ever encountered or solved this issue before?</p>