Results 1 to 1 of 1
  1. #1
    Kantanomo's Avatar
    Join Date
    May 2010
    Gender
    male
    Posts
    32
    Reputation
    10
    Thanks
    12
    My Mood
    Psychedelic

    Basic Keyboard Hooks

    First off this is obviously my first post on MPGH.net and i wanna post something actually contributory.

    To create a Basic Keyboard hook you are going to need to understand basic C# and .NET basics.

    To start make a new Windows Form project and name it whatever you want. Then go into the Forms Code and add the following namespaces
    Code:
    using System.IO;
    using System****ntime.InteropServices;
    using Microsoft.Win32;
    using System.Diagnostics;
    Now that you have those you want to add a new struct above your main form class.

    Code:
        [StructLayout(LayoutKind.Sequential)]
        public struct KeyHook
        {
            public Keys key;
            public int Code;
            public int flags;
            public int time;
            public IntPtr extra;
        }
    Now that you have a new struct you want to go into your forms class and add the following Fields.
    Code:
            private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern bool UnhookWindowsHookEx(IntPtr hook);
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
            [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr GetModuleHandle(string name);
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern short GetAsyncKeyState(Keys key);
            private IntPtr ptrHook;
            private LowLevelKeyboardProc objKeyboardProcess;
    Now in your forms class add the following void

    Code:
            private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
            {
                if (nCode >= 0)
                {
                    KeyHook objKeyInfo = (KeyHook)Marshal.PtrToStructure(lp, typeof(KeyHook));
                }
                //this.TopMost = true;
                return CallNextHookEx(ptrHook, nCode, wp, lp);
            }
    now into your forms main void default (Form1()) or

    Code:
                ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule; 
                objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
                ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
    Now go to your Forms Designer.cs and add the name spaces
    Code:
    using System;
    then in the designers Desposing() bool replace it with
    Code:
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
    
                    components.Dispose();
                }
                if (ptrHook != IntPtr.Zero)
                {
                    UnhookWindowsHookEx(ptrHook);
                    ptrHook = IntPtr.Zero;
                }
                base.Dispose(disposing);
            }

    Now we have added the code to catch the keybaord hooks to use this to do functions or other stuff we go to the CaptureKey void

    To Disable a Key's input you add the code

    Code:
            private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
            {
                if (nCode >= 0)
                {
                    KeyHook objKeyInfo = (KeyHook)Marshal.PtrToStructure(lp, typeof(KeyHook));
                    if (objKeyInfo.key == /*Key to disable*/) 
                        return (IntPtr)1;
                }
                //this.TopMost = true;
                return CallNextHookEx(ptrHook, nCode, wp, lp);
            }
    To have it do a function do

    Code:
            private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
            {
                if (nCode >= 0)
                {
                    KeyHook objKeyInfo = (KeyHook)Marshal.PtrToStructure(lp, typeof(KeyHook));
                    //if (objKeyInfo.key == /*Key*/)
                          Application.Exit();
                }
                //this.TopMost = true;
                return CallNextHookEx(ptrHook, nCode, wp, lp);
            }
    That is basic Keyboard Hooks, You can do many things with this code.

    -Kantanomo leaving it short and sweet

  2. The Following 2 Users Say Thank You to Kantanomo For This Useful Post:

    cruciimperio (01-06-2012),sheilyn (09-28-2015)