title: C# sending input to a window not in focus
body: <p>Upfront: I am still fairly new to C#. I have done all the research I can up to the point I am at now. This is my first time asking for help online.</p>
  
  <p>I am trying to create a program in C# that sits in the background and receives input from a scanner. From what I have researched so far the scanner acts like a keyboard wedge. This is created as a console application. The program is supposed to compare the part number scanned with the previous part number that was scanned. If the new part number is different than the last then it sends an e-mail saying that the previous part number is completed and out for delivery and that the new part number is starting production. It works except for 2 issues:</p>
  
  <ol>
  <li>I tried using Hooks to intercept input. My resources where:
  <a href="https://blogs.msdn.microsoft.com/toub/2006/05/03/low-level-keyboard-hook-in-c/" rel="nofollow">https://blogs.msdn.microsoft.com/toub/2006/05/03/low-level-keyboard-hook-in-c/</a></li>
  </ol>
  
  <p><a href="http://null-byte.wonderhowto.com/how-to/create-simple-hidden-console-keylogger-c-sharp-0132757/" rel="nofollow">http://null-byte.wonderhowto.com/how-to/create-simple-hidden-console-keylogger-c-sharp-0132757/</a></p>
  
  <p>While the code I have works for sending input to the console in the background it prints one character per line as well as the issue the other issue I am having...</p>
  
  <ol start="2">
  <li>Either its capturing input to a blank console with one character per line or it's running the rest of my code without the functionality of the hooks.</li>
  </ol>
  
  <p>I have been working on this program for several days and I am at a loss. Any help would be greatly appreciated...</p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.Windows.Forms;
  using System.Configuration;
  using System.IO;
  using System.Net;
  using System.Net.Mail;
  using System.Runtime.InteropServices;
  using Outlook = Microsoft.Office.Interop.Outlook;
  using System.Windows.Input;
  
  namespace Test1
  {
  
      class Program
      {           
          private const int WH_KEYBOARD_LL = 13;
          private const int WM_KEYDOWN = 0x0100;
          public static LowLevelKeyboardProc _proc = HookCallBack;
          public static IntPtr _hookID = IntPtr.Zero;
  
  
  
      #region Import DLLs 
      [DllImport("user32.dll", CharSet = CharSet.Auto,SetLastError = true)]
      private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc Ipfn, IntPtr hMod, uint dwThreadId);
  
      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      [return: MarshalAs(UnmanagedType.Bool)]
      private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  
      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr IParam);
  
      [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      private static extern IntPtr GetModuleHandle(string IpModuleName);
  
      #endregion
  
      #region Key Tracker Delegates/Functions
  
      public static IntPtr SetHook(LowLevelKeyboardProc proc)
          {
              using (Process curProcess = Process.GetCurrentProcess())
              using (ProcessModule curModule = curProcess.MainModule)
              {
                  return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName),0);
              }
          }
  
      public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr IParam);   
  
          public static class PreviousDriver
          {
              public static string previousDriver{get;set;}
          }
      #endregion
  
      #region  Email functionality
          public static void CreateMail()
          {
              Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
  
              Microsoft.Office.Interop.Outlook.MailItem mailItem = (Outlook.MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
  
              mailItem.Subject="Driver Update";
              //mailItem.To = ; email here
              mailItem.Body = "Your " + PreviousDriver.previousDriver + "is finished and is out for delivery.\n\n";
              mailItem.Display(false);
              mailItem.Send();
  
          }
  
      #endregion
  
          public static class CurrentDriverCount
          {
              public static int currentDriverCount{get;set;}
          }
  
  
          #region Main
          public static void Main()
          {
  
          //var handle = GetConsoleWindow();
  
          //ShowWindow(handle, SW_HIDE);
  
          _hookID = SetHook(_proc);
  
          Application.Run();
  
          UnhookWindowsHookEx(_hookID);
  
          DriverStart();
  
          }
  
          #endregion
  
          #region DriverStart()
  
          public static void DriverStart()
          {
              //string PreviousDriver = null;
  
  
                  Console.WriteLine("What is your driver?\n");
  
  
                  string CurrentDriver = Console.ReadLine(); //Scan driver
  
  
                  Console.WriteLine("Your current driver is " + CurrentDriver + "\n");
  
                  if(CurrentDriver == PreviousDriver.previousDriver)
                  {
                      //When it's the same driver
  
                      Console.WriteLine("Your previous driver was " + PreviousDriver.previousDriver + "\n");
  
                      CurrentDriverCount.currentDriverCount +=1;
  
                      Console.WriteLine("Your driver count is " + CurrentDriverCount.currentDriverCount + ".\n");
  
                      DriverStart();
                  } else {
  
                      //When the driver is different
  
                      Console.WriteLine("Your " + PreviousDriver.previousDriver + "is finished and is out for delivery.\n\n" +
                                        "Your "+ CurrentDriver + "are starting now!\n");
  
                      CreateMail();
  
                      PreviousDriver.previousDriver = CurrentDriver;
  
                      CurrentDriverCount.currentDriverCount = 0;
                      CurrentDriverCount.currentDriverCount +=1;
  
                      Console.WriteLine("Your driver count is " + CurrentDriverCount.currentDriverCount + ".\n");
  
                      DriverStart();
  
                  }
          }
  
          #endregion
  
  
  
      private static IntPtr HookCallBack(int nCode, IntPtr wParam, IntPtr IParam)
      {
          if (nCode &gt;= 0 &amp;&amp; wParam == (IntPtr)WM_KEYDOWN)
          {
              int vkCode = Marshal.ReadInt32(IParam);
              Console.WriteLine((Keys)vkCode);
              StreamWriter sw = new StreamWriter(Application.StartupPath,true);
              sw.Write((Keys)vkCode);
              sw.Close();
  
  
          }
          return CallNextHookEx(_hookID, nCode, wParam, IParam);
      }
  
      }
  }
  </code></pre>