Every so often I have an app where I want to listen for global keyboard events and respond to them. I have had this code sitting around for a while and finally got it up to github, so anyone can use it as they see fit. It uses a few Win32 APIs to listen for the keyboard events and then fires events to let the caller know something happened. The Win32 calls are:

[DllImport("user32.dll")]
static extern short GetAsyncKeyState(int vKey);

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
   int dwExtraInfo);

Then I simply have a background worker continuously checking the GetAsyncKeyState of the keys that we are monitoring. When we hit a key we are monitoring, we fire the event, so the consumer knows the keys were pressed.

foreach (int iKey in watchCodes)
{
    short ret = GetAsyncKeyState(iKey);
    if (ret != 0)
    {
        keybd_event((byte)iKey, 0x45, KEYEVENTF_KEYUP, 0);
        this.KeyPressed((Keycode)iKey);
    }
}

It's pretty basic now, but it does provide some useful functionality if you need to globally monitor keystrokes. The way it is setup now, key combinations are not exactly easy to do, but that functionality could easily be added to the API.