在winform中设置快捷键
#region 快捷键设置 [DllImport("user32.dll", SetLastError = true)] public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options Keys vk // virtual-key code ); [DllImport("user32.dll", SetLastError = true)] public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window int id // hot key identifier ); [Flags()] public enum KeyModifiers { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8 } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { UnregisterHotKey(Handle, 100);//卸载快捷键 //注销Id号为101的热键设定 UnregisterHotKey(Handle, 101); } protected override void WndProc(ref Message m)//监视Windows消息 { const int WM_HOTKEY = 0x0312;//按快捷键 switch (m.Msg) { case WM_HOTKEY: switch (m.WParam.ToInt32()) { case 100: //按下的是Left ProcessHotkeyLeft(); //此处填写快捷键响应代码 break; case 101: //按下的是Right ProcessHotkeyRight(); //此处填写快捷键响应代码 break; case 102: //按下的是Up ProcessHotkeyUp(); //此处填写快捷键响应代码 break; case 103: //按下的是Up ProcessHotkeyDown(); //此处填写快捷键响应代码 break; } break; } base.WndProc(ref m); } void ProcessHotkeyLeft() { } void ProcessHotkeyRight() { } void ProcessHotkeyUp() { } void ProcessHotkeyDown() { } #endregion
在窗体Load时注册快捷键
//设置快捷键 Clipboard.Clear();//先清空剪贴板防止剪贴板里面先复制了其他内容 RegisterHotKey(Handle, 100, 0, Keys.Left); RegisterHotKey(Handle, 101, 0, Keys.Right); RegisterHotKey(Handle, 102, 0, Keys.Up); RegisterHotKey(Handle, 103, 0, Keys.Down); RegisterHotKey(Handle, 109, KeyModifiers.Shift, Keys.S);//注册Shift+S快捷键
直接把代码都写出来了?
周末过来围观支持一下嘿嘿