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
随机推荐
- sql server 2012 删除服务器名称
SQL Server 2008 R2及以前版本: http://blog.csdn.net/downmoon/article/details/5678468 SQL Server 2012: 删除这两 ...
- css 超详细文档
http://www.runoob.com/css/css-boxmodel.html
- 14款优秀的JavaScript调试工具大盘点
JavaScript是一种非常简单的语言,一般说来任何人都可以在几小时内掌握它的基本知识. 然而就像其他任何语言一样,JavaScript存在着一些可以轻易避免的常见错误和不好的做法.开发人员喜欢使用 ...
- ADO.NET实体数据模型中关于数据库字段默认值的处理
无论是Visual Studio 2010或者2013内置的ADO.NET实体数据模型都有一个小问题:数据库中有些字段已设置了默认值,但ADO.NET实体数据模型工具并不会自动进行设置. 这时需要手工 ...
- 区分重载(overload),覆盖(Override)和隐藏(hide)
重载overload,这个概念是大家熟知的.在同一可访问区内被声名的几个具有不同参数列的(参数的类型.个数.顺序不同)同名函数,程序会根据不同的参数列来确定具体调用哪个函数,这种机制就是重载.重载不关 ...
- DPI (深度报文检测) 关于DPI的学习笔记
关于DPI的学习笔记 先看一下定义 : DPI(Deep Packet Inspection)是一种基于数据包的深度检测技术,针对不同的网络应用层载荷(例如HTTP.DNS等)进行深度检测,通过对报文 ...
- FPGA中的时序分析(一)
谈及此部分,多多少少有一定的难度,笔者写下这篇文章,差不多是在学习FPGA一年之后的成果,尽管当时也是看过类似的文章,但是都没有引起笔者注意,笔者现在再对此知识进行梳理,也发现了有很多不少的收获.笔者 ...
- Android pid uid
PID:为Process Identifier, PID就是各进程的身份标识,程序一运行系统就会自动分配给进程一个独一无二的PID.进程中止后PID被系统回收,可能会被继续分配给新运行的程序,但是在a ...
- Ubuntu18.04 + cuda9.0+cudnn7.0
1 cannot find Toolkit in /usr/local/cuda-8.0 2017年05月27日 17:37:33 阅读数:2754 对于新版本ubuntukylin17.04安装C ...
- pandas 的数据结构(Series, DataFrame)
Pandas 讲解 Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的. Pandas 纳入了大量库和一些标 ...