Application KeyBoard Hook in C#.Net

May 12 2010 8:39 AM
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.

Answers (2)