代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
全局快捷键
作者:
asion
/ 发布于
2011/4/26
/
786
<div><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Collections.Generic; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Runtime.InteropServices; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Windows.Forms; </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)"> Hotkey { </span><span style="color: rgb(0,128,0)">/*</span><span style="color: rgb(0,128,0)">注:快捷键是通过消息触发的,因此要重载WndProc函数, * 在里面添加对快捷键回调消息的处理方法Hotkey.ProcessHotKey(m)。 * 注意,回调函数(要执行的方法,动作等)不加括号! // protected override void WndProc(ref Message m) // { // base.WndProc(ref m); // Hotkey.ProcessHotKey(m); // } </span><span style="color: rgb(0,128,0)">*/</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> 系统api</span><span style="color: rgb(0,0,0)"> [DllImport(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">user32.dll</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">)] [</span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)">: MarshalAs(UnmanagedType.Bool)] </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">extern</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> RegisterHotKey(IntPtr hWnd, </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> id, HotkeyModifiers fsModifiers, Keys vk); [DllImport(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">user32.dll</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">)] </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">extern</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> UnregisterHotKey(IntPtr hWnd, </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> id); </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 注册快捷键 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="hWnd"></span><span style="color: rgb(0,128,0)">持有快捷键窗口的句柄</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="fsModifiers"></span><span style="color: rgb(0,128,0)">组合键</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="vk"></span><span style="color: rgb(0,128,0)">快捷键的虚拟键码</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="callBack"></span><span style="color: rgb(0,128,0)">回调函数</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> Regist(IntPtr hWnd, HotkeyModifiers fsModifiers, Keys vk, HotKeyCallBackHanlder callBack) { </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> id </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> keyid</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">RegisterHotKey(hWnd, id, fsModifiers, vk)) </span><span style="color: rgb(0,0,255)">throw</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Exception(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">regist hotkey fail.</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); keymap[id] </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> callBack; } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 注销快捷键 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="hWnd"></span><span style="color: rgb(0,128,0)">持有快捷键窗口的句柄</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="callBack"></span><span style="color: rgb(0,128,0)">回调函数</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack) { </span><span style="color: rgb(0,0,255)">foreach</span><span style="color: rgb(0,0,0)"> (KeyValuePair</span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)">, HotKeyCallBackHanlder</span><span style="color: rgb(0,0,0)">></span><span style="color: rgb(0,0,0)"> var </span><span style="color: rgb(0,0,255)">in</span><span style="color: rgb(0,0,0)"> keymap) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (var.Value </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> callBack) UnregisterHotKey(hWnd, var.Key); } } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 快捷键消息处理 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> ProcessHotKey(System.Windows.Forms.Message m) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (m.Msg </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> WM_HOTKEY) { </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> id </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> m.WParam.ToInt32(); HotKeyCallBackHanlder callback; </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (keymap.TryGetValue(id, </span><span style="color: rgb(0,0,255)">out</span><span style="color: rgb(0,0,0)"> callback)) { callback(); } } } </span><span style="color: rgb(0,0,255)">const</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> WM_HOTKEY </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0x312</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> keyid </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">10</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> Dictionary</span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)">, HotKeyCallBackHanlder</span><span style="color: rgb(0,0,0)">></span><span style="color: rgb(0,0,0)"> keymap </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Dictionary</span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)">, HotKeyCallBackHanlder</span><span style="color: rgb(0,0,0)">></span><span style="color: rgb(0,0,0)">(); </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">delegate</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> HotKeyCallBackHanlder(); } </span><span style="color: rgb(0,0,255)">enum</span><span style="color: rgb(0,0,0)"> HotkeyModifiers { MOD_ALT </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0x1</span><span style="color: rgb(0,0,0)">, MOD_CONTROL </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0x2</span><span style="color: rgb(0,0,0)">, MOD_SHIFT </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0x4</span><span style="color: rgb(0,0,0)">, MOD_WIN </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0x8</span><span style="color: rgb(0,0,0)"> } </span><span style="color: rgb(0,128,0)">/*</span><span style="color: rgb(0,128,0)">附:虚拟键值表附:常用模拟键的键值对照表。 键盘键与虚拟键码对照表 字母和数字键 数字小键盘的键 功能键 其它键 键 键码 键 键码 键 键码 键 键码 A 65 0 96 F1 112 Backspace 8 B 66 1 97 F2 113 Tab 9 C 67 2 98 F3 114 Clear 12 D 68 3 99 F4 115 Enter 13 E 69 4 100 F5 116 Shift 16 F 70 5 101 F6 117 Control 17 G 71 6 102 F7 118 Alt 18 H 72 7 103 F8 119 Caps Lock 20 I 73 8 104 F9 120 Esc 27 J 74 9 105 F10 121 Spacebar 32 K 75 * 106 F11 122 Page Up 33 L 76 + 107 F12 123 Page Down 34 M 77 Enter 108 -- -- End 35 N 78 - 109 -- -- Home 36 O 79 . 110 -- -- Left Arrow 37 P 80 / 111 -- -- Up Arrow 38 Q 81 -- -- -- -- Right Arrow 39 R 82 -- -- -- -- Down Arrow 40 S 83 -- -- -- -- Insert 45 T 84 -- -- -- -- Delete 46 U 85 -- -- -- -- Help 47 V 86 -- -- -- -- Num Lock 144 W 87 X 88 Y 89 Z 90 0 48 1 49 2 50 3 51 4 52 5 53 6 54 7 55 8 56 9 57 </span><span style="color: rgb(0,128,0)">*/</span></div>
试试其它关键字
全局快捷键
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
asion
贡献的其它代码
(
10
)
.
只允许运行一个窗体实例
.
C#可移动按钮“相关代码”
.
XML操作演练
.
创建快捷方式
.
自定义不规则窗体和控件
.
自定义后缀名
.
自动粘合桌面边缘
.
设置窗体效果AnimateWindow
.
全局快捷键
.
模拟QQ振屏功能
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3