using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace SysFuns
{
///
/// 功能键
///
[Flags()]
public enum KeyModifiers
{
///
/// 无
///
None = 0,
///
/// Alt键
///
Alt = 1,
///
/// Ctrl键
///
Ctrl = 2,
///
/// Alt+Ctrl键
///
AltAndCtrl = 3,
///
/// Shift键
///
Shift = 4,
///
/// Shift+Alt键
///
AltAndShift = 5,
///
/// Shift+Ctrl键
///
CtrlAndShift = 6,
///
/// Alt+Shift+Ctrl键
///
AltAndCtrlAndShift = 7,
///
/// Win键
///
WindowsKey = 8
}
///
/// 热键类
///
public class WinHotReg : System.Windows.Forms.IMessageFilter
{
//如果函数执行成功,返回值不为0。
//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
[DllImport("user32.dll", SetLastError = true)]
private static extern bool RegisterHotKey(
IntPtr hWnd, //要定义热键的窗口的句柄
int id, //定义热键ID(不能与其它ID重复)
KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
Keys vk //定义热键的内容
);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
);
readonly IntPtr handle;
///
/// 热键类
///
///
public WinHotReg(IntPtr _handle)
{
handle = _handle;
System.Windows.Forms.Application.AddMessageFilter(this);
}
///
/// 热键注销
///
~WinHotReg()
{
UnHotKey();
System.Windows.Forms.Application.RemoveMessageFilter(this);
}
///
/// 热键是否有效
///
public bool Enabled { get; set; } = true;
///
/// 释放热键
///
public void Free()
{
UnHotKey();
System.Windows.Forms.Application.RemoveMessageFilter(this);
}
readonly List list_hotkey = new List();
///
/// 获取热键Tag
///
///
///
public string GetTag(int HotId)
{
var index = list_hotkey.FindIndex(a => a.id == HotId);
return index >= 0 ? list_hotkey[index].Tag : "";
}
///
/// 获取热键id
///
///
///
public int GetHotId(string Tag)
{
var index= list_hotkey.FindIndex(a => a.Tag == Tag);
return index >= 0 ? list_hotkey[index].id : -1;
}
///
/// 获取热键id
///
///
///
///
public int GetHotId(KeyModifiers fsModifiers, Keys vk)
{
var index = list_hotkey.FindIndex(a => a.fsModifiers == fsModifiers && a.vk == vk);
return index >= 0 ? list_hotkey[index].id : -1;
}
///
/// 判断热键id是否存在
///
///
///
public bool IsExist(int hotId)
{
var index = list_hotkey.FindIndex(a => a.id == hotId);
return index >= 0;
}
///
/// 判断热键Tag是否存在
///
///
///
public bool IsExist(string Tag)
{
var index = list_hotkey.FindIndex(a => a.Tag == Tag);
return index >= 0;
}
///
/// 判断热键是否存在
///
///
///
///
public bool IsExist(KeyModifiers fsModifiers, Keys vk)
{
var index = list_hotkey.FindIndex(a => a.fsModifiers == fsModifiers && a.vk==vk);
return index >= 0;
}
///
/// 注册热键
///
///
///
///
///
public bool RegHotKey(int id, KeyModifiers fsModifiers, Keys vk)
{
bool result = RegisterHotKey(handle, id, fsModifiers, vk);
if (result)
{
list_hotkey.Add(new HotKeyType(id, fsModifiers, vk));
}
return result;
}
///
/// 注册热键
///
///
///
///
///
public bool RegHotKey(int id, int fsModifiers, Keys vk)
{
return RegHotKey(id,"", fsModifiers, vk);
}
///
/// 注册热键
///
///
///
///
///
///
public bool RegHotKey(int id,string Tag, int fsModifiers, Keys vk)
{
if (!Enum.IsDefined(typeof(KeyModifiers), fsModifiers))
{
return false;
}
bool result = RegisterHotKey(handle, id, (KeyModifiers)fsModifiers, vk);
if (result)
{
list_hotkey.Add(new HotKeyType(id, Tag, (KeyModifiers)fsModifiers, vk));
}
return result;
}
///
/// 获取热键数量
///
public int Count { get { return list_hotkey.Count; } }
///
/// 删除热键
///
///
///
public bool UnHotKey(int id)
{
bool result = UnregisterHotKey(handle, id);
if (result)
{
var index = list_hotkey.FindIndex(a => a.id == id);
if (index >= 0) { list_hotkey.RemoveAt(index); }
}
return result;
}
///
/// 删除所有热键
///
public void UnHotKey()
{
for (int i = list_hotkey.Count - 1; i >= 0; i--)
{
HotKeyType item = (HotKeyType)list_hotkey[i];
bool result = UnregisterHotKey(handle, item.id);
if (result)
{
list_hotkey.RemoveAt(i);
}
}
}
///
/// 删除所有热键
///
public void Clear()
{
UnHotKey();
}
///
/// 热键事件
///
///
public delegate void HotkeyEventHandler(int HotKeyID);
///
/// 当使用热键时发生的事件
///
public event HotkeyEventHandler OnHotkey;
///
/// 热键消息过滤
///
///
///
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
if (m.Msg == 0x312)
{
if (OnHotkey != null)
{
if (!Enabled) { return true; }
for (int i = list_hotkey.Count - 1; i >= 0; i--)
{
HotKeyType item = (HotKeyType)list_hotkey[i];
if ((UInt32)m.WParam == item.id)
{
OnHotkey((int)m.WParam);
return true;
}
//OnHotkey((int)m.WParam);
}
//OnHotkey((int)m.WParam);
}
}
return false;
}
}
///
/// 热键类型
///
public class HotKeyType
{
///
/// 热键id
///
public int id;
///
/// 热键Tag
///
public string Tag;
///
/// 功能键
///
public KeyModifiers fsModifiers;
///
/// 普通键
///
public Keys vk;
///
/// 热键类型
///
///
///
///
public HotKeyType(int id, KeyModifiers fsModifiers, Keys vk)
{
this.id = id;
this.fsModifiers = fsModifiers;
this.vk = vk;
}
///
/// 热键类型
///
///
///
///
///
public HotKeyType(int id,string Tag, KeyModifiers fsModifiers, Keys vk)
{
this.id = id;
this.Tag = Tag;
this.fsModifiers = fsModifiers;
this.vk = vk;
}
///
/// 热键类型
///
public HotKeyType()
{
}
}
}