Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I'm still having a problem with my keyboard hook. It just isn't working. The debug method successfully outputs values, but I'm not sure what else I can do to trace where it's going wrong.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;

namespace GlobalHooks
{
    public class InterceptKeys
    {        
        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;

        private static IntPtr _hookID = IntPtr.Zero;
        private static String keysHooked = String.Empty;
        
        private static LowLevelHookProc keyboardHook;
       
        public delegate IntPtr LowLevelHookProc(int nCode, Int32 wParam, IntPtr lParam);
        public delegate void KeyboardHandleFunction(int vkCode);
        public static event KeyboardHandleFunction keyHookReturn;

        public InterceptKeys(KeyboardHandleFunction func)
        {
            keyHookReturn = func;
            keyboardHook = new LowLevelHookProc(this.HookCallback);
        }

        public static void debug()
        {
            Console.Write("\n[Success!] _hookID: "+_hookID);
            Console.Write("\n[Success!] keyboardProc: "+keyboardHook.ToString());
        }

        private IntPtr SetupHook(LowLevelHookProc keyProcess)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, keyProcess,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        public void Hook()
        {
            _hookID = SetupHook(keyboardHook);
            debug();
        }

        public void Unhook()
        {
            UnhookWindowsHookEx(_hookID);
        }


        public static void OnCallbackReturn(int nCode)
        {
            if (keyHookReturn != null)
            {
                keyHookReturn(nCode);
            }
            else
            {
                throw new Exception();
            }
        }

        private IntPtr HookCallback(int nCode, Int32 wParam, IntPtr lParam)
        {
            Console.WriteLine("Calledback");
            if (nCode >= 0 && wParam == WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                Console.WriteLine((Keys)vkCode);
                OnCallbackReturn(nCode);
            }
            return CallNextHookEx((int)_hookID, nCode, wParam, lParam);
        }
       
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelHookProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(int hhk, int nCode, int wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
    }
}
 

Posted

1 solution

What do you mean it doesn't work? I just tried compiling and running that code, and it does, in fact, intercept key presses in the system.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900