private void load(object sender, RoutedEventArgs e)
{
//System.Windows.MessageBox.Show("L:l");.
IntPtr Handle = new WindowInteropHelper(this).Handle;
//注册热键Shift+h,Id号为103。
HotKey.RegisterHotKey(Handle, 103, HotKey.KeyModifiers.Ctrl| HotKey.KeyModifiers.Alt, System.Windows.Forms.Keys.H);
//注册热键Shift+M,Id号为104。
HotKey.RegisterHotKey(Handle, 104, HotKey.KeyModifiers.Ctrl| HotKey.KeyModifiers.Alt, System.Windows.Forms.Keys.M);
RegisterHotKey();
}
private void RegisterHotKey()
{
IntPtr hwnd = new WindowInteropHelper(this).Handle;
//获得消息源
System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(hwnd);
source.AddHook(HotKeyHook);
}
private IntPtr HotKeyHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
//热键处理过程
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
if (msg == WM_HOTKEY)
{
switch (wParam.ToInt32())
{
case 103:
//此处填写快捷键响应代码
break;
case 104:
//ctrl+shift+m响应事件
break;
}
}
return IntPtr.Zero;
}
HotKey.cs
using System;
using System.Runtime.InteropServices;
namespace enter
{
class HotKey
{
//如果函数执行成功,返回值不为0。
//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定义热键的窗口的句柄
int id, //定义热键ID(不能与其它ID重复)
KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
System.Windows.Forms.Keys vk //定义热键的内容
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
);
//定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}
}
这富文本编辑器好难用。。。。上面的代码块去不掉了。。