.NET技术交流群 199281001 .欢迎加入。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Collections; /// <summary>
/// 直接构造类实例即可注册
/// 自动完成注销
/// 注意注册时会抛出异常
/// </summary>
public class HotKey
//注册系统热键类
//热键会随着程序结束自动解除,不会写入注册表
{
#region Member int KeyId; //热键编号
IntPtr Handle; //窗体句柄
Window window; //热键所在窗体
uint Controlkey; //热键控制键
uint Key; //热键主键 public delegate void OnHotkeyEventHandeler(); //热键事件委托
public event OnHotkeyEventHandeler OnHotKey = null; //热键事件 static Hashtable KeyPair = new Hashtable(); //热键哈希表 private const int WM_HOTKEY = 0x0312; // 热键消息编号 public enum KeyFlags //控制键编码
{
MOD_ALT = 0x1,
MOD_CONTROL = 0x2,
MOD_SHIFT = 0x4,
MOD_WIN = 0x8,
MOD_CONTROLALT = 0x2 + 0x1
} #endregion /// <summary>
/// 构造函数
/// </summary>
/// <param name="win">注册窗体</param>
/// <param name="control">控制键</param>
/// <param name="key">主键</param>
public HotKey(Window win, HotKey.KeyFlags control, Keys key)
//构造函数,注册热键
{ Handle = new WindowInteropHelper(win).Handle;
window = win;
Controlkey = (uint)control;
Key = (uint)key;
KeyId = (int)Controlkey + (int)Key * ;
if (HotKey.KeyPair.ContainsKey(KeyId))
{
throw new Exception("热键已经被注册!");
} //注册热键
if (false == HotKey.RegisterHotKey(Handle, KeyId, Controlkey, Key))
{
throw new Exception("热键注册失败!");
}
if (HotKey.KeyPair.Count == )
{
//消息挂钩只能连接一次!!
if (false == InstallHotKeyHook(this))
{
throw new Exception("消息挂钩连接失败!");
}
} //添加这个热键索引
HotKey.KeyPair.Add(KeyId, this); } #region core [System.Runtime.InteropServices.DllImport("user32")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint controlKey, uint virtualKey); [System.Runtime.InteropServices.DllImport("user32")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id); static private bool InstallHotKeyHook(HotKey hk)
//安装热键处理挂钩
{
if (hk.window == null || hk.Handle == IntPtr.Zero)
return false; //获得消息源
System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(hk.Handle);
if (source == null) return false; //挂接事件
source.AddHook(HotKey.HotKeyHook);
return true;
} static private IntPtr HotKeyHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
//热键处理过程
{
if (msg == WM_HOTKEY)
{
HotKey hk = (HotKey)HotKey.KeyPair[(int)wParam];
if (hk.OnHotKey != null) hk.OnHotKey();
}
return IntPtr.Zero;
} ~HotKey()
//析构函数,解除热键
{
HotKey.UnregisterHotKey(Handle, KeyId);
} #endregion
}

注册和卸载钩子

调用:

 1     private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
2 {
3 HotKey hot = new HotKey(this,HotKey.KeyFlags.MOD_CONTROLALT,Keys.M);
4 hot.OnHotKey += hot_OnHotKey;
5 }
6
7 void hot_OnHotKey()
8 {
9 MessageBox.Show("我是按ctrl+alt+m 进来的!");
10 }

WPF 注册全局快捷键的更多相关文章

  1. WPF之全局快捷键

    目录 1.WPF快捷键实现方式 2.全局快捷键设置界面 3.Windows API调用 4.注册全局快捷键 5.快捷键触发 WPF快捷键实现方式 WPF快捷键实现主要有自定义快捷键命令和全局快捷键两种 ...

  2. WPF设置全局快捷键

    转自:http://www.cnblogs.com/atskyline/archive/2012/09/20/2694878.html 第一步 引入到Winows API 偷懒直接写在类里 1: [D ...

  3. 在WPF中使用全局快捷键

    今天写一个小程序中使用到了全局快捷键,找到了我之前写的文章在c#中使用全局快捷键翻了一下,发现它是WinForm版本的,而我现在大部分写WPF程序了,便将其翻译了为WPF版本的了. static cl ...

  4. 在Form Load中设置showInTaskBar =false 或 隐藏窗口 this.Hide()时会导致注册的全局快捷键无效

    在Form Load中设置showInTaskBar =false   或 隐藏窗口 this.Hide() 会导致注册的全局快捷键无效.  反正是其中一个,有点记不清了. 在Form Shown中s ...

  5. C#Winform 注册使用全局快捷键详解

    C#.NET Winform 注册使用全局快捷键详解 借助于全局快捷键,用户可以在任何地方操控程序,触发对应的功能.但 WinForms 框架并没有提供全局快捷键的功能.想要实现全局快捷键需要跟 Wi ...

  6. Delphi 中的全局快捷键+给指定窗体发送按键

    [背景] 公司做视频影像采集,平时采集图像的时候都需要打开采集窗口,然后需要开着采集窗口来进行图像采集.同事问我能不能做一个全局快捷键,哪怕我没有操作也可以采集图像.说干就干,一直想做全局快捷键了,网 ...

  7. JavaFx全局快捷键实现(Kotlin)

    原文地址: JavaFx全局快捷键实现(Kotlin) | Stars-One的杂货小窝 最近整款工具需要用到全局快捷键,搜集了下网上的资料,发现有个JIntellitype库可以用来实现全局快捷键, ...

  8. 在 WinForms 项目中使用全局快捷键

    借助于全局快捷键,用户可以在任何地方操控程序,触发对应的功能.但 WinForms 框架并没有提供全局快捷键的功能.想要实现全局快捷键需要跟 Windows API 打交道.本文就交你如何使用 Win ...

  9. WPF设置全局字体和字体嵌入

    原文:WPF设置全局字体和字体嵌入 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/CLeopard/article/details/40590373 ...

随机推荐

  1. IBatis.net介绍

    IBatis.net介绍 IBatis.net 是2001年发起的开源项目,它是一个轻量级的ORM框架,现在IBatisNET已经是属于Apache下的一个子项目了,最新版本是1.6.2. 官方网站: ...

  2. Linux常用指令---tar | zip (解压缩)

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用.gzip不仅可以用 ...

  3. web.xml配置文件

    一.web.xml里面的标签 <display-name> <context-param> <listener> <filter> 和 <filt ...

  4. SQL 语句大全

    转载:http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html 经典SQL语句大全 一.基础 1.说明:创建数据库 CREATE ...

  5. Thrift搭建分布式微服务(三)

    第一篇 <连接配置> 第二篇 <连接池> 第三篇 标准通信 一.TCP的连接是无状态的,怎样知道我的请求被服务端接受并且正确执行了呢? 我的解决方案是使用自己定义的标准输入输出 ...

  6. 9、面向对象以及winform的简单运用(输入输出流、图像的上传和读取)

    一.输入输出流 1.概念: 输入输出流主要用于保存.读取文件,其内容保存在内存中. 2.使用方法: using System.IO; //System.IO 命名空间包含允许读写文件和数据流的类型以及 ...

  7. PS转换图片——我教你

    将图片转换为web格式所有格式,选png8 或者gif 16位

  8. java多线程-BlockingQueue

    BlockingQueue简介 ArrayBlockingQueue:基于数组实现的一个阻塞队列,在创建ArrayBlockingQueue对象时必须制定容量大小.并且可以指定公平性与非公平性,默认情 ...

  9. 利用less监视模式实时预览样式刷新浏览器

    [前言]此处介绍的方法只是我个人的用法,相信大家有更好更简洁的方式. 上次写到利用LiveReload解放F5.而且LiveReload可以编辑sass/less/stylus.但是可惜发现LiveR ...

  10. Oracle创建表格报ORA-00906:缺失左括号错误解决办法

    来源于:http://www.linuxidc.com/Linux/2013-06/85297.htm 解决办法: create table myTable(id number(5,2),name v ...