1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
namespace Hook
{
public class Hook
{
#region Import DLL
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
[DllImport("user32")]
private static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);
[DllImport("user32")]
private static extern int GetKeyboardState(byte[] pbKeyState);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern short GetKeyState(int vKey);
#endregion
#region Delegates
private delegate int HookProc(int nCode, int wParam, IntPtr lParam);
#endregion
#region Structures
[StructLayout(LayoutKind.Sequential)]
private class KeyboardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
#endregion
#region Constantes
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x100;
private const int WM_KEYUP = 0x101;
private const int WM_SYSKEYDOWN = 0x104;
private const int WM_SYSKEYUP = 0x105;
private const byte VK_SHIFT = 0x10;
private const byte VK_CAPITAL = 0x14;
private const byte VK_NUMLOCK = 0x90;
#endregion
#region Variables
static Int32 _hook = 0;
#endregion
#region Hook Clavier
public void Start()
{
if (_hook == 0)
{
HookProc KeyboardHookProcedure = new HookProc(KeyboardHookProc);
_hook = SetWindowsHookEx(
WH_KEYBOARD_LL,
KeyboardHookProcedure,
Marshal.GetHINSTANCE(
Assembly.GetExecutingAssembly().GetModules()[0]),
0);
}
}
public void Stop()
{
if (_hook != 0)
{
UnhookWindowsHookEx(_hook);
}
}
public Boolean Activated
{
get
{
if (_hook == 0)
return (false);
return (true);
}
}
private KeyPressEventHandler _onKeyPress;
public event KeyPressEventHandler OnKeyPress
{
add
{
_onKeyPress += value;
}
remove
{
_onKeyPress -= value;
}
}
private KeyEventHandler _onKeyUp;
public event KeyEventHandler OnKeyUp
{
add
{
_onKeyUp += value;
}
remove
{
_onKeyUp -= value;
}
}
private KeyEventHandler _onKeyDown;
public event KeyEventHandler OnKeyDown
{
add
{
_onKeyDown += value;
}
remove
{
_onKeyDown -= value;
}
}
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
bool handled = false;
//On verifie si tous est ok
StreamWriter s = new StreamWriter("C:\\Coucou.txt", true);
s.Write("test");
if ((nCode >= 0) && (_onKeyDown != null || _onKeyUp != null || _onKeyPress != null))
{
//Remplissage de la structure KeyboardHookStruct a partir d'un pointeur
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
//KeyDown
if (_onKeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
{
Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
KeyEventArgs e = new KeyEventArgs(keyData);
MessageBox.Show(Convert.ToString(keyData));
_onKeyDown(this, e);
handled = handled || e.Handled;
}
// KeyPress
if (_onKeyPress != null && wParam == WM_KEYDOWN)
{
// Si la touche Shift est appuyée
bool isShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);
// Si la touche CapsLock est appuyée
bool isCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);
byte[] keyState = new byte[256];
GetKeyboardState(keyState);
byte[] inBuffer = new byte[2];
if (ToAscii(MyKeyboardHookStruct.vkCode,
MyKeyboardHookStruct.scanCode,
keyState,
inBuffer,
MyKeyboardHookStruct.flags) == 1)
{
char key = (char)inBuffer[0];
if ((isCapslock ^ isShift) && Char.IsLetter(key))
key = Char.ToUpper(key);
KeyPressEventArgs e = new KeyPressEventArgs(key);
_onKeyPress(this, e);
handled = handled || e.Handled;
}
}
// KeyUp
if (_onKeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
{
Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
KeyEventArgs e = new KeyEventArgs(keyData);
_onKeyUp(this, e);
handled = handled || e.Handled;
}
}
// si handled est a true, on ne transmet pas le message au destinataire
if (handled)
return 1;
else
return CallNextHookEx(_hook, nCode, wParam, lParam);
}
#endregion
}
} |
Partager