title: Double characters shown when typing special characters while logging keystrokes in c#
body: <p>I have an application that logs whatever the user press, but when I press special characters like <code>´</code> with <code>a</code>, to get <code>á</code>, I get <code>´´a</code>; same thing when I want to get <code>à</code>, then i get <code>``a</code>, so all special characters get typed twice and then the regular character get typed after. </p>
  
  <p>I have searched for ever and can't find anything really. But I have noticed that the problem is in the <code>ToAscii</code> method , without that the characters are typed correctly. </p>
  
  <pre><code>public string GetString(IntPtr lParam, int vCode)
  {
      try
      {
          bool shift = Keys.Shift == Control.ModifierKeys || Console.CapsLock;
  
          string value = ""; 
  
          KeyboardHookStruct MyKeyboardHookStruct = 
              (KeyboardHookStruct)Marshal.PtrToStructure(
                  lParam, typeof(KeyboardHookStruct));
  
          byte[] keyState = new byte[256];
          byte[] inBuffer = new byte[2];
  
          DllClass.GetKeyboardState(keyState);
  
          var ascii=
              DllClass.ToAscii(
                  MyKeyboardHookStruct.vkCode, 
                  MyKeyboardHookStruct.scanCode, 
                  keyState, inBuffer, MyKeyboardHookStruct.flags
                  );
  
          if (ascii == 1)
          {
              char key = (char)inBuffer[0];
  
              if ((shift) &amp;&amp; Char.IsLetter(key))
                  key = Char.ToUpper(key);
  
              value = key.ToString();
          }
  
          return value;
      }
      catch (Exception)
      {
          return "";
      }
  }
  </code></pre>
  
  <p>Am I missing something or doing something wrong? All other characters are working perfectly but it's the special characters that is coming as double chars. </p>
  
  <hr>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>Trying with <code>ToUnicode</code> instead.</p>
  
  <pre><code>[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
  public static extern int ToUnicode(
      uint virtualKey, uint scanCode, byte[] keyStates, 
      [MarshalAs(UnmanagedType.LPArray)] [Out] char[] chars, 
      int charMaxCount, uint flags);
  
  public string GetString(IntPtr lParam, int vCode)
  {
      try
      {
          bool shift = Keys.Shift == Control.ModifierKeys || Console.CapsLock;
  
          string value = ""; 
  
          KeyboardHookStruct MyKeyboardHookStruct = 
              (KeyboardHookStruct)Marshal.PtrToStructure(
                  lParam, typeof(KeyboardHookStruct));
  
          byte[] keyState = new byte[256];
          byte[] inBuffer = new byte[2];
  
          char[] chars = new char[2];
  
          DllClass.GetKeyboardState(keyState);
  
          int val = 0;
  
          val = ToUnicode(
                  (uint)MyKeyboardHookStruct.vkCode, 
                  (uint)MyKeyboardHookStruct.scanCode, 
                  keyState, chars, chars.Length, 0
                  );
  
          val = ToUnicode(
                  (uint)MyKeyboardHookStruct.vkCode, 
                  (uint)MyKeyboardHookStruct.scanCode, 
                  keyState, chars, chars.Length, 0
                  );
  
          if (val == 1)
          {
              char key = (char)chars[0];
  
              if ((shift) &amp;&amp; Char.IsLetter(key))
                  key = Char.ToUpper(key);
  
              value = key.ToString();
          }
  
          return value;
      }
      catch (Exception)
      {
          return "";
      }
  }
  </code></pre>
  
  <p>Someone PLEASE help me, I really need to figure this out <code>=/</code>. </p>
  
  <hr>
  
  <p><strong>EDIT:</strong> </p>
  
  <pre><code>int val = -1;
  
  if (IsDeadKey((uint)vCode))
  {
      while (val == -1)
      {
          val = ToUnicode(
                  (uint)MyKeyboardHookStruct.vkCode, 
                  (uint)MyKeyboardHookStruct.scanCode, 
                  keyState, chars, chars.Length, 0
                  );
      }
  }
  else
      val = ToUnicode(
              (uint)MyKeyboardHookStruct.vkCode, 
              (uint)MyKeyboardHookStruct.scanCode, 
              keyState, chars, chars.Length, 0
              );
  </code></pre>
  
  <p>So now I have tried calling the <code>ToAscii</code> or <code>ToUnicode</code> a couple of times to flush the real character but without success. Am I doing it wrong? </p>
  
  <p>Like for ASCII, first call for <code>´</code> I get <code>-1</code>, so I call it again, then I get <code>1</code>; and then I press like <code>a</code>, to get <code>á</code>, but then I only get <code>a</code>. Same thing if I use <code>ToUnicode</code> twice after each other, I get just <code>a</code> instead of <code>á</code>, and so on ... </p>