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
随机推荐
- Android Studio 学习笔记(1)
最近从Eclipse转到Android Studio IDE,很多东西需要学习,本文是个记录. 项目结构 在Anroid Studio 中,一个Project 包括多个Module,每个Module下 ...
- office2016选择性安装
office2016在安装的时候并不像之前的版本有选择性的安装,安装器会安装一个office全家桶.那么如何自主选择安装自己需要的工具呢? 微软在下载中心中提供了Office2016部署工具(Offi ...
- MFC中的句柄
1.引出句柄 CDC问题:1.CDC dc;dc.LineTo(point);无法运行 2.CDC *dc=GetDC();dc->LineTo(point);就可以运行了 MFC中有大量的句柄 ...
- linux下安装jre运行环境
上官网下载安装文件:点击打开链接 文件名:jre-8u65-linux-x64.gz安装步骤1.解压tar -xzvf jre-8u65-linux-x64.gz2.将解压后的文件放到/usr/lib ...
- linux 卸载mysql
RPM包安装方式的MySQL卸载 1: 检查是否安装了MySQL组件. [root@DB-Server init.d]# rpm -qa | grep -i mysql MySQL-devel-5 ...
- C++ 面向对象 类成员函数this指针
每个类成员函数都只涉及一个对象, 即调用它的对象. 但有时候方法可能涉及到两个对象, 在这种情况下需要使用C++ 的 this 指针 假设将方法命名为topval(), 则函数调用stock1.top ...
- 大型网站的SEO引爆点
网站越大,SEO服务做起来就轻松,因为大型网站都有很好的执行团队,你只需要找准他们网站的SEO爆破点,就能够迅速获得非常理想的SEO效果.本文将结合我最近两年的几个经典案例:腾讯拍拍.金山爱词霸.中青 ...
- VUE系列二:vue基础
一.组件嵌套 1. 新建一个组件Users.vue <template> <div class="users"> <ul> <li v-f ...
- CI框架 -- 自动加载资源
CodeIgniter 的"自动加载"特性可以允许系统每次运行时自动初始化类库.辅助函数和模型. 如果你需要在整个应用程序中全局使用某些资源,为方便起见可以考虑自动加载它们. 支持 ...
- chrome 下改动 agent 的方法
前言 这篇文章和 tiankonguse 的个人站点里的文章保持同步. 非常早之前,在 chrome 下改动 agent 的方法是使用 chrome 插件. 后来 chrome 的某一个版本号中自带这 ...