title: Using a low-level keyboard hook to change keyboard characters
body: <p>I'm creating a custom keyboard layout. As the beginning step, I want to have the user press a key, have my keyboard hook intercept it, and output a different key of my choosing. </p>
  
  <p>I found this keyboard hook code, which I'm trying to slightly modify for my purposes:
  <a href="http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx</a></p>
  
  <p>I've changed the relevant method to this:</p>
  
  <pre><code>private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  {
      if (nCode &gt;= 0 &amp;&amp; wParam == (IntPtr)WM_KEYDOWN)
      {
          KBDLLHOOKSTRUCT replacementKey = new KBDLLHOOKSTRUCT();
          Marshal.PtrToStructure(lParam, replacementKey);
          replacementKey.vkCode = 90; // char 'Z'
          Marshal.StructureToPtr(replacementKey, lParam, true);
      }
      return CallNextHookEx(_hookID, nCode, wParam, lParam);
  }
  </code></pre>
  
  <p>I want it to declare a new KBD structure object, copy the KBD structure supplied by the keyboard hook into it, modify my object's vkCode to use a different character, and then overwrite the supplied object with my modified version. This should hopefully keep everything the same except for the fact that it writes a different character.</p>
  
  <p>Unfortunately, it's not working. The original keyboard character is typed. The Visual Studio output pane also gets a <code>A first chance exception of type 'System.ArgumentException' occurred in MirrorBoard.exe</code> error. </p>
  
  <p>What can I do here to intercept the keyboard hook and replace it with a character of my choosing?</p>
  
  <p>Thanks!</p>