本文代码来自vt
#region 定义钩子 引用dll
//public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
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("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
#endregion
#region 定义键盘钩子变量
/// <summary>定义钩子句柄 </summary>
public static int hKeyBoardHook = 0;
/// <summary> 键盘委托实例 </summary>
HookProc KeyboardHookProcedure;
/// <summary> 普通按键消息 </summary>
private const int WM_KEYDOWN = 0x100;
/// <summary> 系统按键消息 </summary>
private const int WM_SYSKEYDOWN = 0x104;
/// <summary> 键盘常量 </summary>
public const int WH_KEYBOARD_LL = 13;
[StructLayout(LayoutKind.Sequential)]
/// <summary> 声明键盘钩子的封送结构类型 </summary>
public class KeyboardHookStruct
{
public int vkCode; //表示一个在1到254间的虚似键盘码
public int scanCode; //表示硬件扫描码
public int flags;
public int time;
public int dwExtraInfo;
}
#endregion
/// <summary>
/// 注册键盘钩子
/// </summary>
private void InstallKeyBoardHook()
{
if (hKeyBoardHook == 0)
{
this.keyList = this.GetHotKeyConfig();
//if (this.keyList.Length > 0 && this.keyList.Length == 3)
//{
// string[] key1 = this.keyList[0].Split('+');
// MessageBox.Show(key1.Length + "\n"
// + key1[0] + "\n" + key1[1] + "\n" + key1[2] + "\n" + key1[3] + "\n");
//}
KeyboardHookProcedure = new HookProc(KeyboardHookProc);//实例化委托
Process curProcess = Process.GetCurrentProcess();
ProcessModule curModule = curProcess.MainModule;
hKeyBoardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, GetModuleHandle(curModule.ModuleName), 0);
}
}
/// <summary>
/// 键盘Ctrl+C 为复制
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)
{
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
System.Windows.Forms.Keys keyData = (System.Windows.Forms.Keys)MyKeyboardHookStruct.vkCode;
if ((int)System.Windows.Forms.Control.ModifierKeys == (int)System.Windows.Forms.Keys.Control
//&& (int)System.Windows.Forms.Control.ModifierKeys == (int)System.Windows.Forms.Keys.Alt
//&& (int)System.Windows.Forms.Control.ModifierKeys == (int)System.Windows.Forms.Keys.Shift
&& keyData == System.Windows.Forms.Keys.Q)
{
if (this.Visibility == Visibility.Hidden)//主窗口隐藏
{
if (win_FloatTray.bTrayState == true)//托盘显示
{
//调用按钮动画
Thread thread_Left = new Thread(this.w_floatTray.Storyboards1);
thread_Left.Start();
//蜂鸣器
Thread thread = new Thread(Buzzer1);
thread.Start();
//托盘拍照
this.w_floatTray.CaptureImg_Tray(this.w_floatTray._listSelPlan[0].LBI,
this.w_floatTray._listSelPlan[0].IPI,
this.w_floatTray._listSelPlan[0].SI);
}
else
{
//呼出托盘
this.Dispatcher.BeginInvoke(new Action(() =>
{
ShowTray_HideMain();
}));
Thread thread = new Thread(Buzzer2);
thread.Start();
}
};
return 1;//return1为了屏蔽原来的按键,如果去掉,则原来的按键和新的按键都会模拟按。
}
if ((int)System.Windows.Forms.Control.ModifierKeys == (int)System.Windows.Forms.Keys.Control
//&& (int)System.Windows.Forms.Control.ModifierKeys == (int)System.Windows.Forms.Keys.Alt
//&& (int)System.Windows.Forms.Control.ModifierKeys == (int)System.Windows.Forms.Keys.Shift
&& keyData == System.Windows.Forms.Keys.W)
{
if (win_FloatTray.bTrayState == true)//托盘显示
{
//调用按钮动画
Thread thread_Top = new Thread(this.w_floatTray.Storyboards2);
thread_Top.Start();
//蜂鸣器
Thread thread = new Thread(Buzzer1);
thread.Start();
//托盘拍照
this.w_floatTray.CaptureImg_Tray(this.w_floatTray._listSelPlan[1].LBI,
this.w_floatTray._listSelPlan[1].IPI,
this.w_floatTray._listSelPlan[1].SI);
}
else
{
//呼出托盘
this.Dispatcher.BeginInvoke(new Action(() =>
{
ShowTray_HideMain();
}));
Thread thread = new Thread(Buzzer2);
thread.Start();
}
return 1;//return1为了屏蔽原来的按键,如果去掉,则原来的按键和新的按键都会模拟按。
}
if ((int)System.Windows.Forms.Control.ModifierKeys == (int)System.Windows.Forms.Keys.Control
//&& (int)System.Windows.Forms.Control.ModifierKeys == (int)System.Windows.Forms.Keys.Alt
//&& (int)System.Windows.Forms.Control.ModifierKeys == (int)System.Windows.Forms.Keys.Shift
&& keyData == System.Windows.Forms.Keys.E)
{
if (win_FloatTray.bTrayState == true)//托盘显示
{
//调用按钮动画
Thread thread_Right = new Thread(this.w_floatTray.Storyboards3);
thread_Right.Start();
//蜂鸣器
Thread thread = new Thread(Buzzer1);
thread.Start();
//托盘拍照
this.w_floatTray.CaptureImg_Tray(this.w_floatTray._listSelPlan[2].LBI,
this.w_floatTray._listSelPlan[2].IPI,
this.w_floatTray._listSelPlan[2].SI);
}
else
{
//呼出托盘
this.Dispatcher.BeginInvoke(new Action(() =>
{
ShowTray_HideMain();
}));
Thread thread = new Thread(Buzzer2);
thread.Start();
}
return 1;//return1为了屏蔽原来的按键,如果去掉,则原来的按键和新的按键都会模拟按。
}
}
return 0;
}