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
Now that you have those you want to add a new struct above your main form class.
Now that you have a new struct you want to go into your forms class and add the following Fields.
Now in your forms class add the following void
now into your forms main void default (Form1()) or
Now go to your Forms Designer.cs and add the name spaces
then in the designers Desposing() bool replace it with
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
To have it do a function do
That is basic Keyboard Hooks, You can do many things with this code.
-Kantanomo leaving it short and sweet
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;
Code:
[StructLayout(LayoutKind.Sequential)]
public struct KeyHook
{
public Keys key;
public int Code;
public int flags;
public int time;
public IntPtr extra;
}
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;
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);
}
Code:
ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
Code:
using System;
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);
}
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);
}
-Kantanomo leaving it short and sweet