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
随机推荐
- border-radius背景色超出圆角问题解决
span{ display: block; background: #f4f4f4; color: #333; font-size: 14px; -webkit-border-radius: 20px ...
- PCL法线估计
平面的法线是垂直于它的单位向量.在点云的表面的法线被定义为垂直于与点云表面相切的平面的向量.表面法线也可以计算点云中一点的法线,被认为是一种十分重要的性质.常常在被使用在很多计算机视觉的应用里面,比如 ...
- java将doc文件转换为pdf文件的三种方法
http://feifei.im/archives/93 —————————————————————————————————————————————— 项目要用到doc转pdf的功能,一番google ...
- Eclipse下进行SVN提交时报“svn: 过期”错误的解决办法
http://www.thinksaas.cn/group/topic/105323/ ———————————————————————————————————————————————————————— ...
- SwipeBackLayout侧滑返回显示桌面
SwipeBackLayout是一个很好的类库,它可以让Android实现类似iOS系统的右滑返回效果,但是很多用户在使用官方提供的Demo会发现,可能出现黑屏或者返回只是看到桌面背景而没有看到上一个 ...
- 客户端通过HTTP协议与服务端交换数据
客户端(包括浏览器)通过HTTP协议与服务端交换数据的描述 发起请求 header 键值对中的key大小写不敏感 Accept: application/json Content-Type: ...
- ubuntu/debian安装mysql遇到的问题及解决方法_1.dpkg中mysql-server-5.5 (configure)时出错 mysql-server-5.5 E: Sub-process /usr/bin/dpkg returned an error code (1)
我的debian7之前安装了mysql-server,是通过apt安装的,后来我卸载掉, 然后用whereis mysql查找, 把所有关于mysql的目录删除掉,包括带mysqld的目录及文件. 重 ...
- 自学QT之qss教程
这篇文章来自于QT的帮助文档,你要是看了最新版的,会发现讲解得更棒.如果你的英文不是那么好,或者说看着头疼,那还是来看此篇吧. 在此之前说一个帮助文档的特别用法,绝不仅仅是搜单词,QT的文档非常强大的 ...
- idea+maven无法自动加载jar包
没有配置maven的环境变量所致 执行mvn -version进行检测
- pandas 的算术运算和数据对齐
pandas 还有一个重要的功能,就是他可以对不同索引的对象进行算数运算.对象相加, 如果存在不同的索引对,则结果的索引就是该索引对的并集. 先来个例子 Series In [33]: s1 = Se ...