鼠标HOOK
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ceshi
{
public class MouseHook
{
public event MouseEventHandler onm ouseActivity;
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
~MouseHook()
{
this.Stop();
}
public void Start()
{
if (hMouseHook == 0)
{
this.MouseHookProcedure = new HookProc(this.MouseHookProc);
hMouseHook = SetWindowsHookEx(14, this.MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
if (hMouseHook == 0)
{
this.Stop();
throw new Exception("SetWindowsHookEx failed.");
}
}
}
public void Stop()
{
bool flag = true;
if (hMouseHook != 0)
{
flag = UnhookWindowsHookEx(hMouseHook);
hMouseHook = 0;
}
if (!flag)
{
throw new Exception("UnhookWindowsHookEx failed.");
}
}
private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode >= 0 && this.OnMouseActivity != null)
{
MouseButtons button = MouseButtons.None;
int clicks = 0;
switch (wParam)
{
case 513:
button = MouseButtons.Left;
clicks = 1;
break;
case 514:
button = MouseButtons.Left;
clicks = 1;
break;
case 515:
button = MouseButtons.Left;
clicks = 2;
break;
case 516:
button = MouseButtons.Right;
clicks = 1;
break;
case 517:
button = MouseButtons.Right;
clicks = 1;
break;
case 518:
button = MouseButtons.Right;
clicks = 2;
break;
}
MouseHookStruct mouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
MouseEventArgs e = new MouseEventArgs(button, clicks, mouseHookStruct.pt.x, mouseHookStruct.pt.y, 0);
this.OnMouseActivity(this, e);
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
private const int WM_MOUSEMOVE = 512;
private const int WM_LBUTTONDOWN = 513;
private const int WM_RBUTTONDOWN = 516;
private const int WM_MBUTTONDOWN = 519;
private const int WM_LBUTTONUP = 514;
private const int WM_RBUTTONUP = 517;
private const int WM_MBUTTONUP = 520;
private const int WM_LBUTTONDBLCLK = 515;
private const int WM_RBUTTONDBLCLK = 518;
private const int WM_MBUTTONDBLCLK = 521;
private static int hMouseHook;
public const int WH_MOUSE_LL = 14;
private HookProc MouseHookProcedure;
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hWnd;
public int wHitTestCode;
public int dwExtraInfo;
}
public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
}
}
键盘HOOK
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ceshi
{
public class KeyboardHook
{
public event KeyEventHandler OnKeyDownEvent;
public event KeyEventHandler OnKeyUpEvent;
public event KeyPressEventHandler OnKeyPressEvent;
public void SetHook()
{
this.KeyboardHookDelegate = new Win32Api.HookProc(this.KeyboardHookProc);
IntPtr moduleHandle = Win32Api.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
this.hHook = Win32Api.SetWindowsHookEx(13, this.KeyboardHookDelegate, moduleHandle, 0);
}
public void UnHook()
{
Win32Api.UnhookWindowsHookEx(this.hHook);
}
private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode >= 0 && (this.OnKeyDownEvent != null || this.OnKeyUpEvent != null || this.OnKeyPressEvent != null))
{
Win32Api.KeyboardHookStruct keyboardHookStruct = (Win32Api.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.KeyboardHookStruct));
Keys vkCode = (Keys)keyboardHookStruct.vkCode;
if ((this.OnKeyDownEvent != null || this.OnKeyPressEvent != null) && (wParam == 256 || wParam == 260) && this.IsCtrlAltShiftKeys(vkCode) && this.preKeysList.IndexOf(vkCode) == -1)
{
this.preKeysList.Add(vkCode);
}
if (this.OnKeyDownEvent != null && (wParam == 256 || wParam == 260))
{
KeyEventArgs e = new KeyEventArgs(this.GetDownKeys(vkCode));
this.OnKeyDownEvent(this, e);
}
if (this.OnKeyPressEvent != null && wParam == 256)
{
byte[] array = new byte[256];
Win32Api.GetKeyboardState(array);
byte[] array2 = new byte[2];
if (Win32Api.ToAscii(keyboardHookStruct.vkCode, keyboardHookStruct.scanCode, array, array2, keyboardHookStruct.flags) == 1)
{
KeyPressEventArgs e2 = new KeyPressEventArgs((char)array2[0]);
this.OnKeyPressEvent(this, e2);
}
}
if ((this.OnKeyDownEvent != null || this.OnKeyPressEvent != null) && (wParam == 257 || wParam == 261) && this.IsCtrlAltShiftKeys(vkCode))
{
for (int i = this.preKeysList.Count - 1; i >= 0; i--)
{
if (this.preKeysList[i] == vkCode)
{
this.preKeysList.RemoveAt(i);
}
}
}
if (this.OnKeyUpEvent != null && (wParam == 257 || wParam == 261))
{
KeyEventArgs e3 = new KeyEventArgs(this.GetDownKeys(vkCode));
this.OnKeyUpEvent(this, e3);
}
}
return Win32Api.CallNextHookEx(this.hHook, nCode, wParam, lParam);
}
private Keys GetDownKeys(Keys key)
{
Keys keys = Keys.None;
foreach (Keys keys2 in this.preKeysList)
{
if (keys2 == Keys.LControlKey || keys2 == Keys.RControlKey)
{
keys |= Keys.Control;
}
if (keys2 == Keys.LMenu || keys2 == Keys.RMenu)
{
keys |= Keys.Alt;
}
if (keys2 == Keys.LShiftKey || keys2 == Keys.RShiftKey)
{
keys |= Keys.Shift;
}
}
return keys | key;
}
private bool IsCtrlAltShiftKeys(Keys key)
{
return key == Keys.LControlKey || key == Keys.RControlKey || key == Keys.LMenu || key == Keys.RMenu || key == Keys.LShiftKey || key == Keys.RShiftKey;
}
private int hHook;
private Win32Api.HookProc KeyboardHookDelegate;
private List<Keys> preKeysList = new List<Keys>();
}
}
标签:wParam,const,鼠标,C#,private,Keys,hook,int,public From: https://www.cnblogs.com/guangzhiruijie/p/17647660.html