Hi All,
I am working on Application Keyboard hook in C#.Net.
I need to do some funcationlity when user presses "Esc" key in my application.
Iam able to capture "Esc" key in the application, but hookprocedure is calling two times when ever i press "Esc" button and in turns my functionality is calling two times.
Iam sure iam pressing "Esc" key only one time.
Please help me in this.
following is code snippet.
hook procedure
public static int KeyHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
//MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
if (nCode < 0)
{
}
else
{
if (wParam.ToInt64() == 27)
{
Form1 obj = new Form1();
obj.myfunction();
}
else
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
return 0;
}
Hook Initialization:
KeyHookProcedure = new HookProc(Form1.KeyHookProc);
hHook = SetWindowsHookEx(WH_KEYBOARD,
KeyHookProcedure,
(IntPtr)0,
AppDomain.GetCurrentThreadId());
Thanks and Regards,
Veeresh.
Application KeyBoard Hook in C#.Net
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Jaish MathewsRank #225 · 11K reputationPosted May 12, 2010, 9:12 AM EDT
Hi,
I don't know your exact task.But just asking that for handling ESC key why can't you use Key_Down event handler of the Form?
veeresh veeresh3 reputationPosted May 13, 2010, 12:29 AM EDT