I want to make a program that could run in the background and detect any key press even when the program is not in focus. It should be able to respond or at least record the key pressed even when others programs are running in foreground. Does anybody have any idea how i can do it
Thanks.

How can I detect KeyPress?
jcastiarena
Could you please give me a small sample application which illustrates this functionality I dont know how to implement this. can you anybody help me
Thanks
Vijay
Jeremy Thomas
Hook the OS and have it send the message to you :)
http://msdn.microsoft.com/library/default.asp url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/lowlevelkeyboardproc.asp
Also check:
SetWindowsHookEx
UnhookWindowsHookEx
LowLevelKeyboardProc
KeyboardProc would probably work too.
I hope this helps :)
Shaun Bedingfield
blogsb.blogspot.com
shaunbed@houston.rr.com
Nathan Booth
You need to use low level Windows hooks. But seriously, stealing keypresses or evesdropping on keypresses meant for other application isn't a good practice.
static int WH_KEYBOARD_LL = 13;
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern IntPtr GetModuleHandle(string moduleName);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, uint wParam, IntPtr lParam);
delegate IntPtr HookProc(int nCode, uint wParam, IntPtr lParam);
IntPtr LowLevelKeyboardProc(int nCode, uint wParam, IntPtr lParam)
{
// wParam will contain the virtual key code.
return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
}
And in your initializer add the following code
IntPtr hook = SetWindowsHookEx(WH_KEYBOARD_LL, new HookProc(LowLevelKeyboardProc), GetModuleHandle(null), 0);