WinForm中实现HotKey
最近在写一个游戏辅助工具,来点Win变成的总结
主要用了RegisterHotKey;UnregisterHotKey;两个winAPI
以下代码来自stackoverflow新增了一个HotKeyManager类
public sealed class KeyboardHook : IDisposable
{
// Registers a hot key with Windows.
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
// Unregisters the hot key with Windows.
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id); /// <summary>
/// Represents the window that is used internally to get the messages.
/// </summary>
private class Window : NativeWindow, IDisposable
{
private static int WM_HOTKEY = 0x0312; public Window()
{
// create the handle for the window.
this.CreateHandle(new CreateParams());
} /// <summary>
/// Overridden to get the notifications.
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
base.WndProc(ref m); // check if we got a hot key pressed.
if (m.Msg == WM_HOTKEY)
{
// get the keys.
Keys key = (Keys)(((int)m.LParam >> ) & 0xFFFF);
ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF); // invoke the event to notify the parent.
if (KeyPressed != null)
KeyPressed(this, new KeyPressedEventArgs(modifier, key));
}
} public event EventHandler<KeyPressedEventArgs> KeyPressed; #region IDisposable Members public void Dispose()
{
this.DestroyHandle();
} #endregion
} private Window _window = new Window();
private int _currentId; public KeyboardHook()
{
// register the event of the inner native window.
_window.KeyPressed += delegate (object sender, KeyPressedEventArgs args)
{
if (KeyPressed != null)
KeyPressed(this, args);
};
} /// <summary>
/// Registers a hot key in the system.
/// </summary>
/// <param name="modifier">The modifiers that are associated with the hot key.</param>
/// <param name="key">The key itself that is associated with the hot key.</param>
public void RegisterHotKey(ModifierKeys modifier, Keys key)
{
// increment the counter.
_currentId = _currentId + ; // register the hot key.
if (!RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key))
throw new InvalidOperationException("Couldn’t register the hot key.");
} /// <summary>
/// A hot key has been pressed.
/// </summary>
public event EventHandler<KeyPressedEventArgs> KeyPressed; #region IDisposable Members public void Dispose()
{
// unregister all the registered hot keys.
for (int i = _currentId; i > ; i--)
{
UnregisterHotKey(_window.Handle, i);
} // dispose the inner native window.
_window.Dispose();
} #endregion
}
public class KeyPressedEventArgs : EventArgs
{
private ModifierKeys _modifier;
private Keys _key; internal KeyPressedEventArgs(ModifierKeys modifier, Keys key)
{
_modifier = modifier;
_key = key;
} public ModifierKeys Modifier
{
get { return _modifier; }
} public Keys Key
{
get { return _key; }
}
} [Flags]
public enum ModifierKeys : uint
{
None=,
Alt = ,
Control = ,
Shift = ,
Win =
}
下面是自己封装的一个类
public class KeysInfo {
public Keys Key { get; set; }
public Action<object, KeyPressedEventArgs> Action { get; set; }
}
public class HotKeyManager
{
private KeyboardHook keyhook = new KeyboardHook();
public List<KeysInfo> KeysInfos { get; set; }
public HotKeyManager(List<KeysInfo> List)
{
this.KeysInfos = List;
}
public void RegisterKey()
{
keyhook.KeyPressed +=new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
foreach (var item in KeysInfos)
{
keyhook.RegisterHotKey(0, item.Key);
}
}
private void hook_KeyPressed(object sender, KeyPressedEventArgs e)
{
foreach (var item in KeysInfos)
{
if (e.Key == item.Key)
{
item.Action(sender,e);
}
}
}
}
使用
private void MainForm_Load(object sender, EventArgs e)
{
LoadGridView();
HotKeyManager hotkey = new HotKeyManager(
new List<KeysInfo>()
{
new KeysInfo {
Key =Keys.Home,
Action =new Action<object, KeyPressedEventArgs>(
(object keySender, KeyPressedEventArgs eventArg)=>
{
if (eventArg.Key==Keys.Home)
{
this.Show();
}
})
},
new KeysInfo {
Key =Keys.End,
Action =new Action<object, KeyPressedEventArgs>(
(object keySender, KeyPressedEventArgs eventArg)=>
{
if (eventArg.Key==Keys.End)
{
this.Hide();
}
})
}
});
hotkey.RegisterKey();
}
WinForm中实现HotKey的更多相关文章
- 转载:WinForm中播放声音的三种方法
转载:WinForm中播放声音的三种方法 金刚 winForm 播放声音 本文是转载的文章.原文出处:http://blog.csdn.net/jijunwu/article/details/4753 ...
- C# Winform 中如何实现音乐播放和视频播放
C# Winform 中如何实现音乐播放和视频播放 namespace WindowsFormsApplication1 { public partial class Form2 : Form { ...
- 另一种在WINFORM中使用XNA的方法
之前在写化学分子模型制作程序的时候,使用一种方法,将WINFORM控件嵌入到XNA窗体中,从而实现了即使用WINFORM窗体控件又使用XNA.最近在写另一个物理运动学课件制作程序,同样使用XNA,但从 ...
- winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难
// winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...
- winform中ComboBox实现text和value,使显示和值分开,重写text和value属性
winform的ComboBox中只能赋值text,显示和值是一样的,很多时候不能满足根本需要,熟悉B/S开发的coder最常用的就是text和value分开的,而且web下DropDownList本 ...
- winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色
winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色 先设置奇数行颜色,这个有个自带的属性AlternatingRowsDefaultCellStyle ...
- winform中button点击后再点击其他控件致使button失去焦点,此时button出现黑色边线,去掉黑色边线的方法
winform中button点击后再点击其他控件致使button失去焦点,此时button出现黑色边线,去掉黑色边线的方法 button的FlatAppearence属性下,设置BorderSize= ...
- 【接上一篇】winform中dataGridView高度和宽度自适应填充完数据的高度和宽度,即dataGridView根据数据自适应大小
上一篇:winform中dataGridView高度自适应填充完数据的高度 winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度 ...
- C# WinForm 中 MessageBox的使用详解
1.C# WinForm 中 MessageBox的使用详解:http://www.cnblogs.com/bq-blog/archive/2012/07/27/2611810.html
随机推荐
- 首部讲Python爬虫电子书 Web Scraping with Python
首部python爬虫的电子书2015.6pdf<web scraping with python> http://pan.baidu.com/s/1jGL625g 可直接下载 waterm ...
- python将dict中的unicode打印成中文
import json a = {u'content': {u'address_detail': {u'province': u'\u5409\u6797\u7701', u'city': u'\u9 ...
- Spring cloud系列十四 分布式链路监控Spring Cloud Sleuth
1. 概述 Spring Cloud Sleuth实现对Spring cloud 分布式链路监控 本文介绍了和Sleuth相关的内容,主要内容如下: Spring Cloud Sleuth中的重要术语 ...
- win10 oracle11g彻底删除
参考以下两篇: 卸载oracle11g步骤: 1.首先关掉所有oracle的相关服务,然后找到oracle的卸载程序Universal Installer: 然后点击卸载产品,然后点击展开全部,将主 ...
- Building Apache Thrift on CentOS 6.5
Building Apache Thrift on CentOS 6.5 Starting with a minimal installation, the following steps are r ...
- jquery miniui 学习笔记
1.取组件值 传递form data,load发送 请求加载数据 <script type="text/JavaScript"> mini.parse(); // ...
- 《jdk10》删除javah.exe文件,在Android studio编译jni,使用jdk10生成头文件
今天在用“死丢丢”编译so包的时候,只要一输入"javah -jni..."的命令就会一直提示 'javah'不是内部命令或外部命令,也不是可运行的程序或批处理文件 找了很久才发现 ...
- [hadoop读书笔记]译者序
一.并行数据库系统 新一代高性能的数据库系统,是在MPP和集群并行计算环境的基础上建立的数据库系统. MPP:大规模并行处理计算机:Massive Parallel Processor.指的是一种处理 ...
- SQL Server 2012 books
SQL Server 2012 Introducing Microsoft SQL Server 2012 Microsoft SQL Server 2012 High-Performance T-S ...
- Servlet输出PDF文档方法
概述 Java Servlet 编程可以很方便地将 HTML 文件发送到客户端的 Web 浏览器.然而许多站点还允许访问非 HTML 格式的文档,包括 Adobe PDF.Microsoft Word ...