首先在网上找到注册系统热键的方法
注册系统热键
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Utility.Form
{
public class HotKey
{
/// <summary>
/// 注册系统热键
/// </summary>
/// <example>注册后,按如下方法重写WndProc方法,写入快捷键的作用
/// <code>
/// protected override void WndProc(ref Message m)
///{
/// switch (m.Msg)
/// {
/// case 0x0312: //表示按快捷键
/// switch (m.WParam.ToInt32())
/// {
/// case 100:
/// //do something
/// break;
/// default:
/// break;
/// }
/// break;
/// }
/// base.WndProc(ref m);
///}
/// </code>
/// </example>
/// <param name="hWnd">Handle</param>
/// <param name="id">ID,不重复即可</param>
/// <param name="fsModifiers"></param>
/// <param name="vk"></param>
/// <returns>如果函数执行成功,返回值不为0。如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。</returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定义热键的窗口的句柄
int id, //定义热键ID(不能与其它ID重复)
KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
Keys vk //定义热键的内容
);
/// <summary>
/// 取消注册系统热键
/// </summary>
/// <param name="hWnd">Handle</param>
/// <param name="id">ID</param>
/// <returns>取消是否成功</returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
);
/// <summary>
/// 定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
/// </summary>
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}
}
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Utility.Form
{
public class HotKey
{
/// <summary>
/// 注册系统热键
/// </summary>
/// <example>注册后,按如下方法重写WndProc方法,写入快捷键的作用
/// <code>
/// protected override void WndProc(ref Message m)
///{
/// switch (m.Msg)
/// {
/// case 0x0312: //表示按快捷键
/// switch (m.WParam.ToInt32())
/// {
/// case 100:
/// //do something
/// break;
/// default:
/// break;
/// }
/// break;
/// }
/// base.WndProc(ref m);
///}
/// </code>
/// </example>
/// <param name="hWnd">Handle</param>
/// <param name="id">ID,不重复即可</param>
/// <param name="fsModifiers"></param>
/// <param name="vk"></param>
/// <returns>如果函数执行成功,返回值不为0。如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。</returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定义热键的窗口的句柄
int id, //定义热键ID(不能与其它ID重复)
KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
Keys vk //定义热键的内容
);
/// <summary>
/// 取消注册系统热键
/// </summary>
/// <param name="hWnd">Handle</param>
/// <param name="id">ID</param>
/// <returns>取消是否成功</returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
);
/// <summary>
/// 定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
/// </summary>
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}
}
这个在网上一搜,到处都是,我在窗体show时注册热键
代码
Utility.Form.HotKey.RegisterHotKey(Handle, 101, Utility.Form.HotKey.KeyModifiers.None, Keys.F1);
Utility.Form.HotKey.RegisterHotKey(Handle, 102, Utility.Form.HotKey.KeyModifiers.None, Keys.F2);
Utility.Form.HotKey.RegisterHotKey(Handle, 103, Utility.Form.HotKey.KeyModifiers.Alt, Keys.A);
Utility.Form.HotKey.RegisterHotKey(Handle, 102, Utility.Form.HotKey.KeyModifiers.None, Keys.F2);
Utility.Form.HotKey.RegisterHotKey(Handle, 103, Utility.Form.HotKey.KeyModifiers.Alt, Keys.A);
并且窗体重写WndProc方法
代码
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case 0x0312: //表示按快捷键
switch (m.WParam.ToInt32())
{
case 101:
//...
break;
case 102:
//...
break;
case 103:
//...
break;
default:
break;
}
break;
}
}
{
base.WndProc(ref m);
switch (m.Msg)
{
case 0x0312: //表示按快捷键
switch (m.WParam.ToInt32())
{
case 101:
//...
break;
case 102:
//...
break;
case 103:
//...
break;
default:
break;
}
break;
}
}
发现已经可以使用了,但是F1,F2这些热键是系统中也要用的,一旦你的程序启动,发现其它程序的这些快捷键已经不起作用了。所以应该在窗体有焦点时才注册,离开焦点时要取消注册,也就是说在Form的Activated方法里注册,而在Deactivate时取消注册就OK了。