基线 SVN r240
SVN-Revision: r240
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Forms;
|
||||
using static WindowsFormsApplication1.Win32Api;
|
||||
namespace WindowsFormsApplication1
|
||||
{
|
||||
/**
|
||||
* c#钩子程序 键盘监控(拦截系统组合键)
|
||||
* */
|
||||
public class Win32Api
|
||||
{
|
||||
#region 常数和结构
|
||||
public const int WM_KEYDOWN = 0x100;
|
||||
|
||||
public const int WM_KEYUP = 0x101;
|
||||
|
||||
public const int WM_SYSKEYDOWN = 0x104;
|
||||
|
||||
public const int WM_SYSKEYUP = 0x105;
|
||||
|
||||
public const int WH_KEYBOARD_LL = 13;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)] //声明键盘钩子的封送结构类型
|
||||
|
||||
public class KeyboardHookStruct
|
||||
{
|
||||
public int vkCode; //表示一个在1到254间的虚似键盘码
|
||||
public int scanCode; //表示硬件扫描码
|
||||
public int flags;
|
||||
public int time;
|
||||
public int dwExtraInfo;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region Api
|
||||
|
||||
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
|
||||
|
||||
//安装钩子的函数
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
||||
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
|
||||
|
||||
//卸下钩子的函数
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
||||
public static extern bool UnhookWindowsHookEx(int idHook);
|
||||
|
||||
//下一个钩挂的函数
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
||||
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
|
||||
|
||||
[DllImport("user32")]
|
||||
public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);
|
||||
|
||||
[DllImport("user32")]
|
||||
public static extern int GetKeyboardState(byte[] pbKeyState);
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
||||
public static extern IntPtr GetModuleHandle(string lpModuleName);
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public class KeyboardHook
|
||||
{
|
||||
int hHook;
|
||||
|
||||
Win32Api.HookProc KeyboardHookDelegate;
|
||||
|
||||
public event KeyEventHandler OnKeyDownEvent;
|
||||
|
||||
public event KeyEventHandler OnKeyUpEvent;
|
||||
|
||||
public event KeyPressEventHandler OnKeyPressEvent;
|
||||
|
||||
/// <summary>
|
||||
/// 正在执行回车事件中
|
||||
/// </summary>
|
||||
public bool IsExecute = false;
|
||||
|
||||
public KeyboardHook() { }
|
||||
|
||||
/**
|
||||
* 设置钩子
|
||||
* */
|
||||
public void SetHook()
|
||||
{
|
||||
KeyboardHookDelegate = new Win32Api.HookProc(KeyboardHookProc);
|
||||
Process cProcess = Process.GetCurrentProcess();
|
||||
ProcessModule cModule = cProcess.MainModule;
|
||||
var mh = Win32Api.GetModuleHandle(cModule.ModuleName);
|
||||
hHook = Win32Api.SetWindowsHookEx(Win32Api.WH_KEYBOARD_LL, KeyboardHookDelegate, mh, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 卸载钩子
|
||||
* */
|
||||
public void UnHook()
|
||||
{
|
||||
Win32Api.UnhookWindowsHookEx(hHook);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截按键
|
||||
* nCode参数是钩子代码,钩子子程使用这个参数来确定任务,这个参数的值依赖于Hook类型。
|
||||
* wParam和lParam参数包含了消息信息,我们可以从中提取需要的信息。
|
||||
* */
|
||||
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
|
||||
{
|
||||
|
||||
if (nCode >= 0)
|
||||
{
|
||||
KeyboardHookStruct kbh = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
|
||||
|
||||
if (kbh.vkCode == 13&& IsExecute)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user