0

I made a very simple Hook codes (I'm a beginner).

I opened Notepad and tested.

If I press ANY key it make a beep and printed itself.

Except "x" key, it is a terminator key.

Question :

I do not want to see "x" key printed. I just quit the program. What do I have to do ?

namespace HookingStudy
{
    class HookingClass          
    {
        private const int WM_KEYDOWN = 0x0100;
        private static LowLevelKeyboardProc _proc = hookCallBack;
        private static IntPtr _hookID = IntPtr.Zero;                    
        public static void Main()
        {
            Beep(1111, 222);
            _hookID = SetHook(_proc);
            Application.Run();
        }
        private static IntPtr hookCallBack(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if( nCode >= 0 && wParam == (IntPtr) WM_KEYDOWN )
            {
                int vkCode = Marshal.ReadInt32(lParam);
                if( vkCode.ToString() == "88" )                 //   88 ("x" key)
                {
                    Beep(7777, 222);
                    UnhookWindowsHookEx(_hookID);     
                    Process.GetCurrentProcess().Kill(); 
                }
                Beep(2222, 55);
            }
            return CallNextHookEx(_hookID, nCode, wParam, lParam);  
        }
        private static IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using( Process curProcess = Process.GetCurrentProcess() )
            using( ProcessModule curModule = curProcess.MainModule )
            {
                return SetWindowsHookEx(13, proc, GetModuleHandle(curModule.ModuleName), 0);
            }                   
        }
        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("KERNEL32.DLL")]                             
        extern public static void Beep(int freq, int dur);      
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
    }  
}
Jason
  • 361
  • 3
  • 15

1 Answers1

1

I do not want to see the terminator x printed at Notepad

Then do not call next hook in chain:

return CallNextHookEx(_hookID, nCode, wParam, lParam);

The idea of hooking it to install own handler prior existing handlers (afair from winapi). By intercepting (like you are doing it already) you are not only listening, but still invoking previous handlers with that call.

Try something like (untested):

if( vkCode == 88)
{
    ...
    return 0;
}
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • Thanks. I'll try it out. – Jason Nov 07 '16 at 13:14
  • Hmmm.. I added "return (IntPtr) 1;". It looks like not bad. But, The program itself is still running (behind). How can I quit it permanently ? Thanks. – Jason Nov 07 '16 at 13:17
  • *"program itself is still running"* - that's another problem. `Application.Run()` is this WPF? – Sinatr Nov 07 '16 at 13:18
  • No. Just plain Console ones. – Jason Nov 07 '16 at 13:19
  • I've clicked it. Thanks. So... can You please tell me the exact point that I put a "terminator line" in my codes ? – Jason Nov 07 '16 at 13:23
  • Put it instead of line where you try to kill current process. – Sinatr Nov 07 '16 at 13:25
  • Hmmm. That is the Question itself. I think I have to put it "return;" point. But, I have "return;" already there. If I change "return;" to "permanent quit code", it just make "x" typed. – Jason Nov 07 '16 at 13:27