title: Background keyboard shortcuts that do not propagate to windows
body: <p>I made an application that needs to handle specific keyboard presses even when the window is not active, now I have this and it works great; however the handled keys are still being propagated to windows (not sure if my app is handling them first, so I may be backwards about that).</p>
  
  <p>Is there any way to have it so my app can handle the key presses I want but not have the keys be sent to the current application/windows.</p>
  
  <p>EX) I have my app open in the background monitoring the number pad for presses, every time I press a number key on the number pad I want to add that number to a text box for display purposes. Now I have chrome open and have the cursor in the address bar, I want to be able to press the number keys while having my app handle them but not having them show up in chromes address bar.</p>
  
  <p>Thanks.</p>
  
  <p>This is basically a very simplistic key logger.</p>
  
  <p>EDIT) 
  Keyboard Hook</p>
  
  <pre><code>#endregion
  using System;
  using System.Text;
  using System.Collections.Generic;
  using System.Runtime.InteropServices;
  using System.Windows.Forms;   
  
  public class GlobalKeyboardHook
  {
  [DllImport("user32.dll")]
  static extern int CallNextHookEx(IntPtr hhk, int code, int wParam, ref keyBoardHookStruct lParam);
  [DllImport("user32.dll")]
  static extern IntPtr SetWindowsHookEx(int idHook, LLKeyboardHook callback, IntPtr hInstance, uint theardID);
  [DllImport("user32.dll")]
  static extern bool UnhookWindowsHookEx(IntPtr hInstance);
  [DllImport("kernel32.dll")]
  static extern IntPtr LoadLibrary(string lpFileName);
  
  public delegate int LLKeyboardHook(int Code, int wParam, ref keyBoardHookStruct lParam);
  
  public struct keyBoardHookStruct
  {
      public int vkCode;
      public int scanCode;
      public int flags;
      public int time;
      public int dwExtraInfo;
  }
  
  const int WH_KEYBOARD_LL = 13;
  const int WM_KEYDOWN = 0x0100;
  const int WM_KEYUP = 0x0101;
  const int WM_SYSKEYDOWN = 0x0104;
  const int WM_SYSKEYUP = 0x0105;
  
  LLKeyboardHook llkh;
  public List&lt;Keys&gt; HookedKeys = new List&lt;Keys&gt;();
  
  IntPtr Hook = IntPtr.Zero;
  
  public event KeyEventHandler KeyDown;
  public event KeyEventHandler KeyUp;
  
  public GlobalKeyboardHook()
  {
      llkh = new LLKeyboardHook(HookProc);
      hook();
  }
  ~GlobalKeyboardHook()
  { unhook(); }
  
  public void hook()
  {
      IntPtr hInstance = LoadLibrary("User32");
      Hook = SetWindowsHookEx(WH_KEYBOARD_LL, llkh, hInstance, 0);
  }
  
  public void unhook()
  {
      UnhookWindowsHookEx(Hook);
  }
  
  public int HookProc(int Code, int wParam, ref keyBoardHookStruct lParam)
  {
      if (Code &gt;= 0)
      {
          Keys key = (Keys)lParam.vkCode;
          if (HookedKeys.Contains(key))
          {
              KeyEventArgs kArg = new KeyEventArgs(key);
              if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) &amp;&amp; (KeyDown != null))
                  KeyDown(this, kArg);
              else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) &amp;&amp; (KeyUp != null))
                  KeyUp(this, kArg);
              if (kArg.Handled)
                  return 1;
          }
      }
      return CallNextHookEx(Hook, Code, wParam, ref lParam);
  }
  
  }
  </code></pre>
  
  <p>Usage of GlobalKeyboardHook</p>
  
  <pre><code>GlobalKeyboardHook gHook;
  private void Form1_Load(object sender, EventArgs e)
  {
     gHook = new GlobalKeyboardHook(); // Create a new GlobalKeyboardHook
     // Declare a KeyDown Event
     gHook.KeyDown += new KeyEventHandler(gHook_KeyDown);
     // Add the keys you want to hook to the HookedKeys list
     foreach (Keys key in Enum.GetValues(typeof(Keys)))
         gHook.HookedKeys.Add(key);
  }
  
  // Handle the KeyDown Event
  public void gHook_KeyDown(object sender, KeyEventArgs e)
  {
     textBox1.Text += ((char)e.KeyValue).ToString();
  }
  
  private void button1_Click(object sender, EventArgs e)
  {
     gHook.hook();
  }
  
  private void button2_Click(object sender, EventArgs e)
  {
     gHook.unhook();
  }
  
  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  {
     gHook.unhook();
  }
  </code></pre>