wpf 如何定义热键
来源:互联网 发布:free mobile java韩国 编辑:程序博客网 时间:2024/04/28 18:03
本文代码来自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; }
0 0
- wpf 如何定义热键
- 在WPF中如何注册热键(转)
- 如何在VC6中定义热键消息
- 如何实现C# button定义热键
- wpf 屏蔽热键
- WPF的热键实现
- WPF窗体程序中实现响应系统热键所用到的键值定义
- Delphi定义热键
- VC6.0定义热键
- [ahk]批量定义热键
- 如何在WPF中定义窗体模板
- WPF自制控件如何定义依赖属性
- WPF(MVVM) 热键快捷键设置
- 给网页页面定义热键
- delphi程序中定义热键
- delphi热键定义及应用
- 第三节、AHK热键定义
- 如何注册系统热键
- Android:ProgressBar 进度条
- c# 调用oracle 存储过程、方法简易例子
- java多线程系列——(1)写在前面
- 简单了解ADO.NET、ADO、OLE DB、ODBC
- 论压缩图片核心技术,几种常见使用方式
- wpf 如何定义热键
- 1671 Walking Ant【bfs】
- 序列化和反序列化二叉树
- 编程思想┊从实例谈面向对象编程(OOP)、工厂模式和重构
- 常用API
- Ubuntu系统中apt-get和aptitude的使用
- CasperJS 介绍以及Windows上安装介绍
- 黑马程序员————java基础之面向对象
- [USACO2015February,Bronze] Problem3.Cow Hopscotch(Bronze)