钩子函数 const int WH_KEYBOARD = 2; const int WH_KEYBOARD_LL = 13; const int WM_KEYDOWN = 0x100; const int WM_KEYUP = 0x101; const int WM_SYSKEYDOWN = 0x104; const int WM_SYSKEYUP = 0x105; const int VK_TAB = 0x9; const int VK_CONTROL = 0x11; const int VK_ESCAPE = 0x1b; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Timer timer1; static int hHook; //定义委托(钩子函数,用于回调) public delegate int HookProc(int code, IntPtr wparam, ref KeybHookStruct lparam); [DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern int GetKeyState(int key); [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern int SystemParametersInfo(int action, int uparam, int vparam); //Import for SetWindowsHookEx function. //Use this function to install thread-specific hook. [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, int hInstance, int threadId); //Import for UnhookWindowsHookEx. //Call this function to uninstall the hook. [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx(int idHook); //Import for CallNextHookEx. //Use this function to pass the hook information to next hook procedure in chain. [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, KeybHookStruct lparam); //获取窗体线程ID [DllImport("User32.dll",CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId(IntPtr hwnd, int ID); public struct KeybHookStruct { public int vkCode,scanCode,flags,time,dwExtraInfo; } private HookProc hookProc; private System.ComponentModel.IContainer components; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // this.hookProc = new HookProc(myhookproc); StartHook(); } //开始拦截 private void StartHook() { //安装钩子,拦截系统向Form2发出的消息 //hHook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, IntPtr.Zero ,GetWindowThreadProcessId(this.Handle,0)); hHook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, Marshal.GetHINSTANCE(typeof(Form1).Module).ToInt32(),0);
} //停止拦截 private bool StopHook() { return UnhookWindowsHookEx(hHook); } //钩子处理函数,在这里拦截消息并做处理 private int myhookproc(int code, IntPtr wparam, ref KeybHookStruct lparam) { if (code>=0) { //if (wparam.ToInt32()==WM_KEYDOWN || wparam.ToInt32()==WM_KEYUP || wparam.ToInt32()==WM_SYSKEYDOWN || wparam.ToInt32()==WM_SYSKEYUP) if (wparam.ToInt32()==WM_KEYDOWN) { // log.Debug("wparam.ToInt32()==WM_KEYDOWN"); // log.Debug("lparam.vkCode: " + lparam.vkCode); //Do it yourself. //When the key pressed, you should return 1; if (lparam.vkCode==27 && (GetKeyState(162)&0x80)==0x80) { // log.Debug("return"); return 1; } } } return CallNextHookEx(hHook, code, wparam, lparam); }具体的还可以上网查一些 详细的资料,这个只是一个简单的例子
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
提供参考一下:
窗体的keypreview设为true,在keypress事件里捕捉。
或者我不是同时按下,也不是同时放开呢?
所以建义用KeyPress事件比较保险
但是你是截取系统的呢,还是发送给你程序的?
const int WH_KEYBOARD_LL = 13; const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const int WM_SYSKEYDOWN = 0x104;
const int WM_SYSKEYUP = 0x105; const int VK_TAB = 0x9;
const int VK_CONTROL = 0x11;
const int VK_ESCAPE = 0x1b;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Timer timer1; static int hHook; //定义委托(钩子函数,用于回调)
public delegate int HookProc(int code, IntPtr wparam, ref KeybHookStruct lparam); [DllImport("user32.dll", CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern int GetKeyState(int key); [DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern int SystemParametersInfo(int action, int uparam, int vparam);
//Import for SetWindowsHookEx function.
//Use this function to install thread-specific hook.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
int hInstance, int threadId); //Import for UnhookWindowsHookEx.
//Call this function to uninstall the hook.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook); //Import for CallNextHookEx.
//Use this function to pass the hook information to next hook procedure in chain.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode,
IntPtr wParam, KeybHookStruct lparam);
//获取窗体线程ID
[DllImport("User32.dll",CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, int ID); public struct KeybHookStruct
{
public int vkCode,scanCode,flags,time,dwExtraInfo;
} private HookProc hookProc;
private System.ComponentModel.IContainer components; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
this.hookProc = new HookProc(myhookproc); StartHook();
}
//开始拦截
private void StartHook()
{
//安装钩子,拦截系统向Form2发出的消息
//hHook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, IntPtr.Zero ,GetWindowThreadProcessId(this.Handle,0));
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, Marshal.GetHINSTANCE(typeof(Form1).Module).ToInt32(),0);
}
//停止拦截
private bool StopHook()
{
return UnhookWindowsHookEx(hHook);
} //钩子处理函数,在这里拦截消息并做处理
private int myhookproc(int code, IntPtr wparam, ref KeybHookStruct lparam)
{ if (code>=0)
{
//if (wparam.ToInt32()==WM_KEYDOWN || wparam.ToInt32()==WM_KEYUP || wparam.ToInt32()==WM_SYSKEYDOWN || wparam.ToInt32()==WM_SYSKEYUP)
if (wparam.ToInt32()==WM_KEYDOWN)
{
// log.Debug("wparam.ToInt32()==WM_KEYDOWN");
// log.Debug("lparam.vkCode: " + lparam.vkCode);
//Do it yourself.
//When the key pressed, you should return 1;
if (lparam.vkCode==27 && (GetKeyState(162)&0x80)==0x80)
{
// log.Debug("return");
return 1;
}
}
} return CallNextHookEx(hHook, code, wparam, lparam);
}具体的还可以上网查一些 详细的资料,这个只是一个简单的例子