📄 hotkeys.cs
字号:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;
namespace VoiceRecorder
{
public class HotKeys
{
#region "Enums"
//list of modifier keys to use
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8,
Modkeyup = 0x1000
}
#endregion
#region "DLL Imports"
[DllImport("coredll.dll", EntryPoint = "RegisterHotKey", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers Modifiers, int key);
[DllImport("coredll.dll", EntryPoint = "UnregisterHotKey", SetLastError = true)]
private static extern bool UnRegisterHotKey(IntPtr hWnd, int key);
#endregion
#region "Vars"
//message listener
private HotKeyMessageWindow wnd = null;
//public keypressed event, for the outside world to listen to
public delegate void KeyPressedEventHandler(Keys key);
private KeyPressedEventHandler KeyPressedEvent;
public event KeyPressedEventHandler KeyPressed
{
add
{
KeyPressedEvent = (KeyPressedEventHandler)System.Delegate.Combine(KeyPressedEvent, value);
}
remove
{
KeyPressedEvent = (KeyPressedEventHandler)System.Delegate.Remove(KeyPressedEvent, value);
}
}
#endregion
#region "Constructors"
public HotKeys()
{
wnd = new HotKeyMessageWindow(this);
}
#endregion
#region "Methods"
public void Register(System.Windows.Forms.Keys Key, KeyModifiers Modifier)
{
//register the key
RegisterHotKey(wnd.Hwnd, System.Convert.ToInt32(Key), Modifier, System.Convert.ToInt32(Key));
}
public void UnRegister(System.Windows.Forms.Keys Key)
{
//unregister any previous keys
UnRegisterHotKey(wnd.Hwnd, System.Convert.ToInt32(Key));
}
//handle the message listener's keypress
public void OnKeyPressed(Keys key)
{
//forward the keypress event to the outside world.
if (KeyPressedEvent != null)
KeyPressedEvent(key);
}
#endregion
#region "MessageWindow"
//internal message listener.
private class HotKeyMessageWindow : MessageWindow
{
private const long WM_HOTKEY = 0x312;
HotKeys parent;
public HotKeyMessageWindow(HotKeys h)
{
parent = h;
}
protected override void WndProc(ref Message msg)
{
//look at the message type
switch ((long)msg.Msg)
{
case WM_HOTKEY:
//it's a hot key, so raise the event
parent.OnKeyPressed((Keys)msg.WParam.ToInt32());
break;
default:
//all other messages go to the base class
base.WndProc(ref msg);
break;
}
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -