C# BackupSupport 1.4.4 (管理快捷键)

原创 2018年01月26日 15:26:37

问题描述

善用快捷键可以使平时一些频繁的电脑操作变得便捷,Windows操作系统和大多数编辑软件都有十分丰富的快捷键,熟练使用这些快捷键能提高工作效率。
但是当你要自定义快捷键来进行一些操作的时候,系统环境就显得不这么友好了。这个问题有很多解决方案,但是大多数方案不能便捷地启用禁用自定义快捷键,也就是管理起来太麻烦。
于是我打算自己写一个程式来管理自定义快捷键,这个功能嵌入到了BackupSupport里。

解决方案

从结论开始讲,就是BackupSupport里多了两个设置视图。

1.HotKey Bind 设置视图

这里写图片描述

这个视图用于自定义快捷键

右键添加或删除快捷键
这里写图片描述

Id-快捷键的ID这里写图片描述

Keys-快捷键的触发键值这里写图片描述

Command-触发后执行的操作
这里写图片描述

Enable-是否启用这个快捷键

设置完成后按Apply应用

按下快捷键后主要是调用了BackupSupport的其他功能,也可以打开文件文件夹或网页,

2.HotKey Disabled 设置视图

这里写图片描述

这个视图是用来禁用系统热键,通过操作注册表来禁用Explorer的Win+快捷键。
BackupSupport这工具之前的功能调用快捷键都是Win+A Win+Q这类的,在Win7系统下运行没有什么问题,但是Win10系统把这些快捷键都占用了。考虑到这种情况还是把禁用方案放在这里。

下面是源码
HotKey.cs 主要用于注册注销和描述快捷键

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ZTools {
    /// <summary>
    /// 系统热键
    /// </summary>
    public class HotKey {
        //[DllImport]
        /// <summary>
        /// 注册热键
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="id"></param>
        /// <param name="fsModifiers"></param>
        /// <param name="vk"></param>
        /// <returns></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                     //定义热键的内容
        );

        public static bool RegisterHotKey(IntPtr hWnd, RegisterInfo info) {
            return RegisterHotKey(hWnd, info.Id, info.KeyCode.FsModifiers, info.KeyCode.VK);
        }

        /// <summary>
        /// 注销热键
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
            IntPtr hWnd,                //要取消热键的窗口的句柄
            int id                      //要取消热键的ID
        );

        public static bool UnregisterHotKey(IntPtr hWnd, RegisterInfo info) {
            return UnregisterHotKey(hWnd, info.Id);
        }

        //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
        [Flags()]
        public enum KeyModifiers {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4,
            WindowsKey = 8
        }

        public class RegisterInfo {
            private HotKey.KeyCode keyCode = new KeyCode();
            public HotKey.KeyCode KeyCode {
                get {
                    return this.keyCode;
                }
                set {
                    this.keyCode = value;
                }
            }

            [ZDataColumn("KeyCode")]
            public string KeyCodeDescription {
                get {
                    return this.KeyCode.ToString();
                }
                set {
                    this.KeyCode = KeyCode.Create(value);
                }
            }

            private int id;
            [ZDataColumn("ID")]
            public int Id {
                get {
                    return id;
                }
                set {
                    id = value;
                }
            }
            private bool enable;
            [ZDataColumn("Enable")]
            public bool Enable {
                get {
                    return enable;
                }
                set {
                    enable = value;
                }
            }

            private string command = "";
            public string Command {
                get {
                    return this.command;
                }
                set {
                    if (value == null) {
                        this.command = "";
                    } else {
                        this.command = value;
                    }
                }
            }

            public override string ToString() {
                string format = "{0}\t{1}\t{2}\t{3}";
                return StringProcesser.Format(format, this.Id.ToString(), this.KeyCodeDescription, this.Command, this.Enable.ToString());
            }

            public void Explan(string content) {
                List<string> resultList = ListProcesser.SplitString(content, "\t");
                try {
                    this.Id = NumericConvertTools.ConvertToInteger(resultList[0]);
                    this.KeyCodeDescription = resultList[1];
                    this.Command = resultList[2];
                    this.Enable = NumericConvertTools.ConvertToBoolean(resultList[3]);
                } catch (Exception) {

                }
            }

            public static HotKey.RegisterInfo Create(string content) {
                HotKey.RegisterInfo result = new RegisterInfo();
                result.Explan(content);
                return result;
            }
        }

        public class KeyCode {
            public static List<Keys> FuncKeyList = new List<Keys>();
            public static List<string> FuncKeyNameList = new List<string>();

            private static void AddFuncKey(Keys key) {
                if (FuncKeyList.Contains(key) == false) {
                    FuncKeyList.Add(key);
                    FuncKeyNameList.Add(EnumTools.GetName(key));
                }
            }

            static KeyCode() {
                AddFuncKey(Keys.A);
                AddFuncKey(Keys.B);
                AddFuncKey(Keys.C);
                AddFuncKey(Keys.D);
                AddFuncKey(Keys.E);
                AddFuncKey(Keys.F);
                AddFuncKey(Keys.G);
                AddFuncKey(Keys.H);
                AddFuncKey(Keys.I);
                AddFuncKey(Keys.J);
                AddFuncKey(Keys.K);
                AddFuncKey(Keys.L);
                AddFuncKey(Keys.M);
                AddFuncKey(Keys.N);
                AddFuncKey(Keys.O);
                AddFuncKey(Keys.P);
                AddFuncKey(Keys.Q);
                AddFuncKey(Keys.R);
                AddFuncKey(Keys.S);
                AddFuncKey(Keys.T);
                AddFuncKey(Keys.U);
                AddFuncKey(Keys.V);
                AddFuncKey(Keys.W);
                AddFuncKey(Keys.X);
                AddFuncKey(Keys.Y);
                AddFuncKey(Keys.Z);

                AddFuncKey(Keys.Up);
                AddFuncKey(Keys.Down);
                AddFuncKey(Keys.Left);
                AddFuncKey(Keys.Right);

                AddFuncKey(Keys.Home);
                AddFuncKey(Keys.End);
                AddFuncKey(Keys.PageUp);
                AddFuncKey(Keys.PageDown);

                AddFuncKey(Keys.Delete);

                AddFuncKey(Keys.D0);
                AddFuncKey(Keys.D1);
                AddFuncKey(Keys.D2);
                AddFuncKey(Keys.D3);
                AddFuncKey(Keys.D4);
                AddFuncKey(Keys.D5);
                AddFuncKey(Keys.D6);
                AddFuncKey(Keys.D7);
                AddFuncKey(Keys.D8);
                AddFuncKey(Keys.D9);

                AddFuncKey(Keys.F1);
                AddFuncKey(Keys.F2);
                AddFuncKey(Keys.F3);
                AddFuncKey(Keys.F4);
                AddFuncKey(Keys.F5);
                AddFuncKey(Keys.F6);
                AddFuncKey(Keys.F7);
                AddFuncKey(Keys.F8);
                AddFuncKey(Keys.F9);
                AddFuncKey(Keys.F10);
                AddFuncKey(Keys.F11);
                AddFuncKey(Keys.F12);
                AddFuncKey(Keys.F13);
                AddFuncKey(Keys.F14);
                AddFuncKey(Keys.F15);
                AddFuncKey(Keys.F16);
                AddFuncKey(Keys.F17);
                AddFuncKey(Keys.F18);
                AddFuncKey(Keys.F19);
                AddFuncKey(Keys.F20);
                AddFuncKey(Keys.F21);
                AddFuncKey(Keys.F22);
                AddFuncKey(Keys.F23);
                AddFuncKey(Keys.F24);

                AddFuncKey(Keys.NumPad0);
                AddFuncKey(Keys.NumPad1);
                AddFuncKey(Keys.NumPad2);
                AddFuncKey(Keys.NumPad3);
                AddFuncKey(Keys.NumPad4);
                AddFuncKey(Keys.NumPad5);
                AddFuncKey(Keys.NumPad6);
                AddFuncKey(Keys.NumPad7);
                AddFuncKey(Keys.NumPad8);
                AddFuncKey(Keys.NumPad9);

            }


            public bool PressWin {
                get {
                    return this.KeyDescriptor.PressWin;
                }
                set {
                    this.KeyDescriptor.PressWin = value;
                }
            }
            public bool PressAlt {
                get {
                    return this.KeyDescriptor.PressAlt;
                }
                set {
                    this.KeyDescriptor.PressAlt = value;
                }
            }
            public bool PressCtrl {
                get {
                    return this.KeyDescriptor.PressCtrl;
                }
                set {
                    this.KeyDescriptor.PressCtrl = value;
                }
            }
            public bool PressShift {
                get {
                    return this.KeyDescriptor.PressShift;
                }
                set {
                    this.KeyDescriptor.PressShift = value;
                }
            }

            public KeyboardInputTools.KeyDescriptor KeyDescriptor = new KeyboardInputTools.KeyDescriptor();

            public KeyCode() {
                PressWin = false;
                PressAlt = false;
                PressCtrl = false;
                PressShift = false;
            }

            public HotKey.KeyModifiers FsModifiers {
                get {
                    HotKey.KeyModifiers result = HotKey.KeyModifiers.None;
                    if (PressWin) {
                        result |= HotKey.KeyModifiers.WindowsKey;
                    }
                    if (PressAlt) {
                        result |= HotKey.KeyModifiers.Alt;
                    }
                    if (PressCtrl) {
                        result |= HotKey.KeyModifiers.Ctrl;
                    }
                    if (PressShift) {
                        result |= HotKey.KeyModifiers.Shift;
                    }
                    return result;
                }
            }

            public Keys VK {
                get {
                    return EnumTools.GetValue<Keys>(this.KeyDescriptor.KeyCode);
                }
                set {
                    this.KeyDescriptor.KeyCode = EnumTools.GetName<Keys>(value);
                }
            }

            public override string ToString() {
                return this.KeyDescriptor.ToString();
            }

            public static HotKey.KeyCode Create(string content) {
                HotKey.KeyCode keyCode = new KeyCode();
                keyCode.Explan(content);
                return keyCode;
            }

            public void Explan(string content) {
                this.KeyDescriptor.Explan(content);
            }

        }
    }
}

KeyboardInputTools.cs 这里主要用于描述和解释按键键码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ZTools {
    /// <summary>
    /// 键盘输入模拟工具
    /// 主要包装了SendInupt和键盘输入相关的功能
    /// </summary>
    public class KeyboardInputTools {

        [DllImport("user32.dll")]
        public static extern UInt32 SendInput(UInt32 nInputs, INPUT[] pInputs, int cbSize);

        [DllImport("kernel32.dll")]
        public static extern int GetTickCount();

        [StructLayout(LayoutKind.Explicit)]
        public struct INPUT {
            [FieldOffset(0)]
            public Int32 type;
            [FieldOffset(4)]
            public KEYBDINPUT ki;
            [FieldOffset(4)]
            public MOUSEINPUT mi;
            [FieldOffset(4)]
            public HARDWAREINPUT hi;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct MOUSEINPUT {
            public Int32 dx;
            public Int32 dy;
            public Int32 mouseData;
            public Int32 dwFlags;
            public Int32 time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct KEYBDINPUT {
            public Int16 wVk;
            public Int16 wScan;
            public Int32 dwFlags;
            public Int32 time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct HARDWAREINPUT {
            public Int32 uMsg;
            public Int16 wParamL;
            public Int16 wParamH;
        }

        public const int INPUT_MOUSE = 0x0000;
        public const int INPUT_KEYBOARD = 0x0001;
        public const int INPUT_HARDWARE = 0x0002;

        public const int KEYEVENTF_EXTENDEDKEY = 0x0001;
        public const int KEYEVENTF_KEYUP = 0x0002;

        /// <summary>
        /// 获取所有的Keys(按键代码和修饰符)
        /// </summary>
        /// <returns></returns>
        public static List<Keys> GetAllKeys() {
            return EnumTools.GetValues<Keys>();
        }

        /// <summary>
        /// 获取所有的Keys的名称
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllKeysName() {
            return EnumTools.GetNames<Keys>();
        }

        /// <summary>
        /// SendInput包装
        /// </summary>
        /// <param name="keysContent">键码或修饰符的名称</param>
        /// <param name="flags">操作标志 指定操作类型</param>
        /// <returns>执行结果</returns>
        public static uint SendInput(string keysContent, int flags = 0) {
            Keys keys = EnumTools.GetValueIgnoreCase<Keys>(keysContent);
            return SendInput(keys, flags);
        }

        /// <summary>
        /// SendInput包装
        /// </summary>
        /// <param name="keys">键码或修饰符</param>
        /// <param name="flags">操作标志 指定操作类型</param>
        /// <returns>执行结果</returns>
        public static uint SendInput(Keys keys, int flags = 0) {
            INPUT[] input = new INPUT[1];
            input[0] = new INPUT();
            input[0].type = INPUT_KEYBOARD;
            input[0].ki.wVk = (short)keys;
            input[0].ki.dwFlags = flags;
            return SendInput(1, input, Marshal.SizeOf(input[0]));
        }

        /// <summary>
        /// SendInput包装
        /// </summary>
        /// <param name="input">一组Input值</param>
        /// <param name="len">Input数组的长度</param>
        /// <returns></returns>
        public static uint SendInput(INPUT[] input, uint len) {
            return SendInput(len, input, Marshal.SizeOf(input[0]));
        }

        /// <summary>
        /// SendInput包装
        /// </summary>
        /// <param name="input">一组Input值</param>
        /// <returns></returns>
        public static uint SendInput(INPUT[] input) {
            return SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]));
        }

        /// <summary>
        /// SendInput包装
        /// </summary>
        /// <param name="inputlist">Input值列表</param>
        /// <returns></returns>
        public static uint SendInput(List<INPUT> inputlist) {
            INPUT[] input = inputlist.ToArray();
            return SendInput(input);
        }

        /// <summary>
        /// SendInput包装
        /// 键盘按键按下
        /// </summary>
        /// <param name="keys">键值或修饰符</param>
        /// <returns></returns>
        public static uint SendKeyDown(Keys keys) {
            return SendInput(keys, 0);
        }

        /// <summary>
        /// SendInput包装
        /// 键盘按键按下
        /// </summary>
        /// <param name="keyContent">键值或修饰符的名称</param>
        /// <returns></returns>
        public static uint SendKeyDown(string keyContent) {
            return SendInput(keyContent, 0);
        }

        /// <summary>
        /// SendInput包装
        /// 键盘按键松开(弹起)
        /// </summary>
        /// <param name="keys">键值或修饰符</param>
        /// <returns></returns>
        public static uint SendKeyUp(Keys keys) {
            return SendInput(keys, 2);
        }

        /// <summary>
        /// SendInput包装
        /// 键盘按键松开(弹起)
        /// </summary>
        /// <param name="keyContent">键值或修饰符的名称</param>
        /// <returns></returns>
        public static uint SendKeyUp(string keyContent) {
            return SendInput(keyContent, 2);
        }

        /// <summary>
        /// SendInput包装
        /// 键盘按键点击(按下并弹起)
        /// </summary>
        /// <param name="keys">键值或修饰符</param>
        /// <returns></returns>
        public static uint SendKeyClick(Keys keys) {
            uint result = SendInput(keys, 0);
            return SendInput(keys, 2);
        }

        /// <summary>
        /// SendInput包装
        /// 键盘按键点击(按下并弹起)
        /// </summary>
        /// <param name="keyContent">键值或修饰符的名称</param>
        /// <returns></returns>
        public static uint SendKeyClick(string keyContent) {
            uint result = SendInput(keyContent, 0);
            return SendInput(keyContent, 2);
        }

        /// <summary>
        /// SendInput包装
        /// 发送键码(SendKeys)
        /// </summary>
        /// <param name="keyCode">描述键码的字符串</param>
        public static void SendKeyCode(string keyCode) {
            KeyDescriptor temp = new KeyDescriptor(keyCode);
            SendKeyCode(temp);
        }

        /// <summary>
        /// SendInput包装
        /// 发送键码(SendKeys)
        /// </summary>
        /// <param name="keyCode">键码</param>
        public static void SendKeyCode(KeyDescriptor keyCode) {
            SendCombinationKey(keyCode.KeyCode, keyCode.PressShift, keyCode.PressAlt, keyCode.PressCtrl, keyCode.PressWin);
        }

        /// <summary>
        /// 发送复制按键指令 Ctrl + C 
        /// </summary>
        public static void SendCopy() {
            KeyboardInputTools.SendKeyDown(Keys.ControlKey);
            KeyboardInputTools.SendKeyClick(Keys.C);
            KeyboardInputTools.SendKeyUp(Keys.ControlKey);
        }

        /// <summary>
        /// 发送粘贴按键指令 Ctrl + V
        /// </summary>
        public static void SendPaste() {
            KeyboardInputTools.SendKeyDown(Keys.ControlKey);
            KeyboardInputTools.SendKeyClick(Keys.V);
            KeyboardInputTools.SendKeyUp(Keys.ControlKey);
        }

        /// <summary>
        /// 发送Windows键
        /// </summary>
        /// <param name="keys">和Win键组合 的 键码或修饰符</param>
        public static void SendWinKey(Keys keys = Keys.Modifiers) {
            KeyboardInputTools.SendKeyDown(Keys.LWin);
            if (keys != Keys.Modifiers) {
                KeyboardInputTools.SendKeyClick(keys);
            }
            KeyboardInputTools.SendKeyUp(Keys.LWin);
        }

        /// <summary>
        /// 发送Windows键
        /// </summary>
        /// <param name="keys">和Win键组合 的 键码或修饰符 的 名称</param>
        public static void SendWinKey(string keys) {
            KeyboardInputTools.SendKeyDown(Keys.LWin);
            if (keys != null && keys.Length > 0) {
                KeyboardInputTools.SendKeyClick(keys);
            }
            KeyboardInputTools.SendKeyUp(Keys.LWin);
        }

        /// <summary>
        /// 发送组合键键值
        /// </summary>
        /// <param name="keys">键码或修饰符</param>
        /// <param name="pressShift">是否按下Shift</param>
        /// <param name="pressAlt">是否按下Alt</param>
        /// <param name="pressCtrl">是否按下Ctrl</param>
        public static void SendCombinationKey(string keys, bool pressShift = false, bool pressAlt = false, bool pressCtrl = false, bool pressWin = false) {
            if (pressWin == true) {
                SendKeyDown(Keys.LWin);
            }
            if (pressAlt == true) {
                SendKeyDown(Keys.LMenu);
            }
            if (pressShift == true) {
                SendKeyDown(Keys.LShiftKey);
            }
            if (pressCtrl == true) {
                SendKeyDown(Keys.LControlKey);
            }
            SendKeyClick(keys);
            if (pressAlt == true) {
                SendKeyUp(Keys.LMenu);
            }
            if (pressShift == true) {
                SendKeyUp(Keys.LShiftKey);
            }
            if (pressCtrl == true) {
                SendKeyUp(Keys.LControlKey);
            }
            if (pressWin == true) {
                SendKeyUp(Keys.LWin);
            }
        }

        /// <summary>
        /// 按键描述符
        /// </summary>
        public class KeyDescriptor {
            //-------------------------------------------------------------------------------------------------
            #region Default KeyDescriptor

            public static KeyDescriptor KeyA = new KeyDescriptor("a", "KeyCode : A");
            public static KeyDescriptor KeyB = new KeyDescriptor("b", "KeyCode : B");
            public static KeyDescriptor KeyC = new KeyDescriptor("c", "KeyCode : C");
            public static KeyDescriptor KeyD = new KeyDescriptor("d", "KeyCode : D");
            public static KeyDescriptor KeyE = new KeyDescriptor("e", "KeyCode : E");
            public static KeyDescriptor KeyF = new KeyDescriptor("f", "KeyCode : F");
            public static KeyDescriptor KeyG = new KeyDescriptor("g", "KeyCode : G");
            public static KeyDescriptor KeyH = new KeyDescriptor("h", "KeyCode : H");
            public static KeyDescriptor KeyI = new KeyDescriptor("i", "KeyCode : I");
            public static KeyDescriptor KeyJ = new KeyDescriptor("j", "KeyCode : J");
            public static KeyDescriptor KeyK = new KeyDescriptor("k", "KeyCode : K");
            public static KeyDescriptor KeyL = new KeyDescriptor("l", "KeyCode : L");
            public static KeyDescriptor KeyM = new KeyDescriptor("m", "KeyCode : M");
            public static KeyDescriptor KeyN = new KeyDescriptor("n", "KeyCode : N");
            public static KeyDescriptor KeyO = new KeyDescriptor("o", "KeyCode : O");
            public static KeyDescriptor KeyP = new KeyDescriptor("p", "KeyCode : P");
            public static KeyDescriptor KeyQ = new KeyDescriptor("q", "KeyCode : Q");
            public static KeyDescriptor KeyR = new KeyDescriptor("r", "KeyCode : R");
            public static KeyDescriptor KeyS = new KeyDescriptor("s", "KeyCode : S");
            public static KeyDescriptor KeyT = new KeyDescriptor("t", "KeyCode : T");
            public static KeyDescriptor KeyU = new KeyDescriptor("u", "KeyCode : U");
            public static KeyDescriptor KeyV = new KeyDescriptor("v", "KeyCode : V");
            public static KeyDescriptor KeyW = new KeyDescriptor("w", "KeyCode : W");
            public static KeyDescriptor KeyX = new KeyDescriptor("x", "KeyCode : X");
            public static KeyDescriptor KeyY = new KeyDescriptor("y", "KeyCode : Y");
            public static KeyDescriptor KeyZ = new KeyDescriptor("z", "KeyCode : Z");

            public static KeyDescriptor KeyESC = new KeyDescriptor("ESC", "KeyCode : ESC");
            public static KeyDescriptor KeyHelp = new KeyDescriptor("HELP", "KeyCode : Help");

            public static KeyDescriptor KeyEnter = new KeyDescriptor("ENTER", "KeyCode : Enter");

            public static KeyDescriptor KeyBreak = new KeyDescriptor("BREAK", "KeyCode : Break");
            public static KeyDescriptor KeyDelete = new KeyDescriptor("DELETE", "KeyCode : Delete");
            public static KeyDescriptor KeyInsert = new KeyDescriptor("INSERT", "KeyCode : Insert");
            public static KeyDescriptor KeyBackSpace = new KeyDescriptor("BACKSPACE", "KeyCode : BackSpace");

            public static KeyDescriptor KeyHome = new KeyDescriptor("HOME", "KeyCode : Home");
            public static KeyDescriptor KeyEnd = new KeyDescriptor("END", "KeyCode : End");
            public static KeyDescriptor KeyPageDown = new KeyDescriptor("PGDN", "KeyCode : PageDown");
            public static KeyDescriptor KeyPageUp = new KeyDescriptor("PGUP", "KeyCode : PageUp");

            public static KeyDescriptor KeyPrintScreen = new KeyDescriptor("PRTSC", "KeyCode : PrtSc");
            public static KeyDescriptor KeyScrollLock = new KeyDescriptor("SCROLLLOCK", "KeyCode : ScrLK");
            public static KeyDescriptor KeyCapsLock = new KeyDescriptor("CAPSLOCK", "KeyCode : CapsLock");
            public static KeyDescriptor KeyNumLock = new KeyDescriptor("NUMLOCK", "KeyCode : NmLK");
            //→←↑↓

            public static KeyDescriptor KeyUp = new KeyDescriptor("UP", "KeyCode : ↑");
            public static KeyDescriptor KeyDown = new KeyDescriptor("DOWN", "KeyCode : ↓");
            public static KeyDescriptor KeyLeft = new KeyDescriptor("LEFT", "KeyCode : ←");
            public static KeyDescriptor KeyRight = new KeyDescriptor("RIGHT", "KeyCode : →");

            public static KeyDescriptor KeyTab = new KeyDescriptor("TAB", "KeyCode : Tab");

            public static KeyDescriptor KeyF1 = new KeyDescriptor("F1", "KeyCode : F1");
            public static KeyDescriptor KeyF2 = new KeyDescriptor("F2", "KeyCode : F2");
            public static KeyDescriptor KeyF3 = new KeyDescriptor("F3", "KeyCode : F3");
            public static KeyDescriptor KeyF4 = new KeyDescriptor("F4", "KeyCode : F4");
            public static KeyDescriptor KeyF5 = new KeyDescriptor("F5", "KeyCode : F5");
            public static KeyDescriptor KeyF6 = new KeyDescriptor("F6", "KeyCode : F6");
            public static KeyDescriptor KeyF7 = new KeyDescriptor("F7", "KeyCode : F7");
            public static KeyDescriptor KeyF8 = new KeyDescriptor("F8", "KeyCode : F8");
            public static KeyDescriptor KeyF9 = new KeyDescriptor("F9", "KeyCode : F9");
            public static KeyDescriptor KeyF10 = new KeyDescriptor("F10", "KeyCode : F10");
            public static KeyDescriptor KeyF11 = new KeyDescriptor("F11", "KeyCode : F11");
            public static KeyDescriptor KeyF12 = new KeyDescriptor("F12", "KeyCode : F12");
            public static KeyDescriptor KeyF13 = new KeyDescriptor("F13", "KeyCode : F13");
            public static KeyDescriptor KeyF14 = new KeyDescriptor("F14", "KeyCode : F14");
            public static KeyDescriptor KeyF15 = new KeyDescriptor("F15", "KeyCode : F15");
            public static KeyDescriptor KeyF16 = new KeyDescriptor("F16", "KeyCode : F16");

            public static KeyDescriptor KeyAdd = new KeyDescriptor("ADD", "KeyCode : +");
            public static KeyDescriptor KeySubTract = new KeyDescriptor("SUBTRACT", "KeyCode : -");
            public static KeyDescriptor KeyMultiply = new KeyDescriptor("MULTIPLY", "KeyCode : *");
            public static KeyDescriptor KeyDivide = new KeyDescriptor("DIVIDE", "KeyCode : /");

            public static KeyDescriptor KeySpace = new KeyDescriptor("( )", "KeyCode : Space");

            public static KeyDescriptor KeyShift = new KeyDescriptor(pressShift: true);
            public static KeyDescriptor KeyCtrl = new KeyDescriptor(pressCtrl: true);
            public static KeyDescriptor KeyAlt = new KeyDescriptor(pressAlt: true);

            #endregion

            public static List<KeyDescriptor> KeyDescriptorList = new List<KeyDescriptor>();

            //-------------------------------------------------------------------------------------------------

            static KeyDescriptor() {

                KeyDescriptorList.Add(KeySpace);

                KeyDescriptorList.Add(KeyA);
                KeyDescriptorList.Add(KeyB);
                KeyDescriptorList.Add(KeyC);
                KeyDescriptorList.Add(KeyD);
                KeyDescriptorList.Add(KeyE);
                KeyDescriptorList.Add(KeyF);
                KeyDescriptorList.Add(KeyG);
                KeyDescriptorList.Add(KeyH);
                KeyDescriptorList.Add(KeyI);
                KeyDescriptorList.Add(KeyJ);
                KeyDescriptorList.Add(KeyK);
                KeyDescriptorList.Add(KeyL);
                KeyDescriptorList.Add(KeyM);
                KeyDescriptorList.Add(KeyN);
                KeyDescriptorList.Add(KeyO);
                KeyDescriptorList.Add(KeyP);
                KeyDescriptorList.Add(KeyQ);
                KeyDescriptorList.Add(KeyR);
                KeyDescriptorList.Add(KeyS);
                KeyDescriptorList.Add(KeyT);
                KeyDescriptorList.Add(KeyU);
                KeyDescriptorList.Add(KeyV);
                KeyDescriptorList.Add(KeyW);
                KeyDescriptorList.Add(KeyX);
                KeyDescriptorList.Add(KeyY);
                KeyDescriptorList.Add(KeyZ);


                KeyDescriptorList.Add(KeyESC);
                KeyDescriptorList.Add(KeyHelp);

                KeyDescriptorList.Add(KeyEnter);

                KeyDescriptorList.Add(KeyBreak);
                KeyDescriptorList.Add(KeyDelete);
                KeyDescriptorList.Add(KeyInsert);
                KeyDescriptorList.Add(KeyBackSpace);

                KeyDescriptorList.Add(KeyHome);
                KeyDescriptorList.Add(KeyEnd);
                KeyDescriptorList.Add(KeyPageDown);
                KeyDescriptorList.Add(KeyPageUp);

                KeyDescriptorList.Add(KeyPrintScreen);
                KeyDescriptorList.Add(KeyScrollLock);
                KeyDescriptorList.Add(KeyCapsLock);
                KeyDescriptorList.Add(KeyNumLock);

                KeyDescriptorList.Add(KeyUp);
                KeyDescriptorList.Add(KeyDown);
                KeyDescriptorList.Add(KeyLeft);
                KeyDescriptorList.Add(KeyRight);

                KeyDescriptorList.Add(KeyTab);

                KeyDescriptorList.Add(KeyF1);
                KeyDescriptorList.Add(KeyF2);
                KeyDescriptorList.Add(KeyF3);
                KeyDescriptorList.Add(KeyF4);
                KeyDescriptorList.Add(KeyF5);
                KeyDescriptorList.Add(KeyF6);
                KeyDescriptorList.Add(KeyF7);
                KeyDescriptorList.Add(KeyF8);
                KeyDescriptorList.Add(KeyF9);
                KeyDescriptorList.Add(KeyF10);
                KeyDescriptorList.Add(KeyF11);
                KeyDescriptorList.Add(KeyF12);
                KeyDescriptorList.Add(KeyF13);
                KeyDescriptorList.Add(KeyF14);
                KeyDescriptorList.Add(KeyF15);
                KeyDescriptorList.Add(KeyF16);

                KeyDescriptorList.Add(KeyAdd);
                KeyDescriptorList.Add(KeySubTract);
                KeyDescriptorList.Add(KeyMultiply);
                KeyDescriptorList.Add(KeyDivide);
            }

            //-------------------------------------------------------------------------------------------------

            /// <summary>
            /// 按键描述符类型
            /// </summary>
            public enum KeyDescriptorType {
                KeysCombination,
                SendKeysContent,
                Unknown,
            }

            public KeyDescriptorType Type = KeyDescriptorType.KeysCombination;

            public KeyDescriptor(string script)
                : this(script, false, false, false, null) {
                //this.SetScript(script);
                //this.Description = description;
            }

            public KeyDescriptor(string script, string description)
                : this(script, false, false, false, description) {
                this.ShowDescription = true;
            }

            //---------------------------------------------------------------------
            public KeyDescriptor(bool pressShift = false, bool pressAlt = false, bool pressCtrl = false) {
                this.PressShift = pressShift;
                this.PressAlt = pressAlt;
                this.PressCtrl = pressCtrl;
            }

            public KeyDescriptor(string script, bool pressShift = false, bool pressAlt = false, bool pressCtrl = false, string description = null) {
                this.Explan(script);
                if (pressShift == true && this.PressShift == false) {
                    this.PressShift = pressShift;
                }
                if (pressAlt == true && this.PressAlt == false) {
                    this.PressAlt = pressAlt;
                }
                if (pressCtrl == true && this.PressCtrl == false) {
                    this.PressCtrl = pressCtrl;
                }
                this.Description = description;
            }

            private void _explan_sendkeyscontent(string content) {
                this.Clear();
                if (content != null) {
                    string temp = content.Trim();
                    int tabIndex = temp.IndexOf("-");
                    if (tabIndex != -1) {
                        temp = content.Substring(0, tabIndex - 1).Trim();
                    }
                    //------------------------------------
                    foreach (char ch in temp) {
                        if (ch.Equals('+')) {
                            this.PressShift = true;
                        } else if (ch.Equals('^')) {
                            this.PressCtrl = true;
                        } else if (ch.Equals('%')) {
                            this.PressAlt = true;
                        } else if (ch.Equals(' ')) {

                        } else {
                            break;
                        }
                    }
                    //------------------------------------
                    int i1 = temp.IndexOf("{");
                    int i2 = temp.IndexOf("}");
                    if (i1 != -1 && i2 != -1) {
                        if (i2 > i1) {
                            string temp2 = temp.Substring(i1 + 1, i2 - i1 - 1).Trim();
                            int i3 = temp2.IndexOf(" ");
                            if (i3 != -1) {
                                string temp3 = temp2.Substring(0, i3).Trim();
                                this.KeyCode = temp3;

                                string temp4 = temp2.Substring(i3).Trim();
                                try {
                                    this.Repeat = int.Parse(temp4);
                                } catch (Exception) {
                                    this.Repeat = 1;
                                }
                            } else {
                                this.KeyCode = temp2;
                                this.Repeat = 1;
                            }
                            return;
                        }
                    }
                    //------------------------------------
                    if (this.KeyCode == null) {
                        this.KeyCode = temp;
                    }

                }
            }

            private bool _explan_keyscombination(string content) {
                this.Clear();
                bool result = false;
                if (content == null || content.Trim().Length == 0) {
                    return false;
                } else {

                    List<string> tempList = ListProcesser.SplitString(content, "-", false);
                    if (tempList.Count >= 2) {
                        this.Description = tempList[1];
                        content = tempList[0];
                    }

                    List<string> tempList2 = ListProcesser.SplitString(content, "+", false);
                    if (tempList2.Count > 0) {
                        foreach (string temp in tempList2) {
                            switch (temp.Trim().ToUpper()) {
                                case "WIN":
                                case "WINDOW":
                                case "WINDOWS":
                                    this.PressWin = true;
                                    result = true;
                                    break;
                                case "ALT":
                                    this.PressAlt = true;
                                    result = true;
                                    break;
                                case "CTRL":
                                    this.PressCtrl = true;
                                    result = true;
                                    break;
                                case "SHIFT":
                                    this.PressShift = true;
                                    result = true;
                                    break;
                                default:
                                    Keys tempKeys = EnumTools.GetValueIgnoreCase<Keys>(temp.Trim().ToUpper());
                                    if (tempKeys != Keys.Modifiers && tempKeys != Keys.None) {
                                        //this.VK = tempKeys;
                                        this.KeyCode = EnumTools.GetName(tempKeys);
                                        result = true;
                                    }
                                    break;
                            }
                        }
                    }
                }
                return result;
            }

            public void Explan(string content, KeyDescriptorType type) {
                if (type == KeyDescriptorType.Unknown) {
                    if (this._explan_keyscombination(content) == true) {
                        this.Type = KeyDescriptorType.KeysCombination;
                    } else {
                        this._explan_sendkeyscontent(content);
                        this.Type = KeyDescriptorType.SendKeysContent;
                    }
                } else {
                    if (type == KeyDescriptorType.KeysCombination) {
                        this._explan_keyscombination(content);
                        this.Type = KeyDescriptorType.KeysCombination;
                    } else if (type == KeyDescriptorType.SendKeysContent) {
                        this._explan_sendkeyscontent(content);
                        this.Type = KeyDescriptorType.SendKeysContent;
                    }
                }
            }

            public void Explan(string content) {
                Explan(content, KeyDescriptorType.Unknown);
            }

            public void Clear() {
                this.PressAlt = false;
                this.PressCtrl = false;
                this.PressShift = false;
                this.PressWin = false;
                this.Repeat = 1;
                this.KeyCode = null;
                this.Description = null;
            }

            public bool PressShift = false;

            public bool PressWin = false;

            public bool PressAlt = false;

            public bool PressCtrl = false;

            private int repeat = 1;
            public int Repeat {
                get {
                    return repeat;
                }
                set {
                    if (value >= 1) {
                        repeat = value;
                    }
                }
            }

            private string keyCode = null;
            public string KeyCode {
                get {
                    return keyCode;
                }
                set {
                    keyCode = value;
                }
            }

            public bool ShowDescription = false;

            private string description = null;
            public string Description {
                get {
                    return description;
                }
                set {
                    description = value;
                }
            }

            public KeyDescriptor KeyCodeNoComb {
                get {
                    KeyDescriptor keycode = new KeyDescriptor();
                    keycode.KeyCode = this.KeyCode;
                    keycode.Repeat = this.Repeat;
                    return keycode;
                }
            }

            private string _tostring_sendkeyscontent() {
                string result = "";
                if (this.PressShift == true) {
                    result += "+";
                }
                if (this.PressCtrl == true) {
                    result += "^";
                }
                if (this.PressAlt == true) {
                    result += "%";
                }
                if (keyCode != null) {
                    if (keyCode.IndexOf("(") != -1) {
                        result += keyCode;
                    } else {
                        if (this.KeyCode.Length > 0) {
                            if (this.Repeat <= 1) {
                                result += "{" + this.KeyCode + "}";
                            } else {
                                result += "{" + this.KeyCode + " " + Repeat + "}";
                            }
                        }
                    }
                }
                if (this.ShowDescription == true) {
                    if (this.Description != null && this.Description.Length > 0) {
                        result += " - " + this.Description;
                    }
                }

                return result;
            }

            private string _tostring_keyscombination() {
                string result = "";
                if (this.PressWin) {
                    result += "Win + ";
                }
                if (this.PressAlt) {
                    result += "Alt + ";
                }
                if (this.PressCtrl) {
                    result += "Ctrl + ";
                }
                if (this.PressShift) {
                    result += "Shift + ";
                }
                if (this.KeyCode != null) {
                    result += this.KeyCode;
                }
                if (this.ShowDescription == true) {
                    if (this.Description != null && this.Description.Length > 0) {
                        result += " - " + this.Description;
                    }
                }
                return result;
            }

            public override string ToString() {
                if (this.Type == KeyDescriptorType.SendKeysContent) {
                    return this._tostring_sendkeyscontent();
                } else if (this.Type == KeyDescriptorType.KeysCombination) {
                    return this._tostring_keyscombination();
                } else {
                    return "";
                }
            }
        }
    }
}

说在后面

BackupSupport下载:http://pan.baidu.com/s/1sl2V1k5
演示视频:见twitter @photonaaa
交流群:347678765

BackupSupport v1.3.4

EnglishA more stable version of BackupSupport was completed.(1.3.4 version) The features of this to...
  • photonaaa
  • photonaaa
  • 2017年12月19日 16:02
  • 30

全局热键管理工具

全局热键管理工具
  • educast
  • educast
  • 2011年04月16日 08:37
  • 1343

C#实现组合快捷键设置

以下是几个例子,一看就明白。案例设置: //在form_load的时候写上 // this.KeyPreview=true; //表示窗体接受按键事件 ...
  • zhensoft163
  • zhensoft163
  • 2009年06月03日 17:35
  • 5085

C#课堂笔记之VS注释快捷键

注释: 先CTRL+K,然后CTRL+C 取消注释: 先CTRL+K,然后CTRL+U 支持自行定义快捷键                                              ...
  • qq_34481314
  • qq_34481314
  • 2016年12月08日 10:33
  • 1159

c#界面开发自定义快捷键

1.在button的Text属性后面加上&快捷键    最后就会快捷键下面就会有个小下划线 2.在窗体代码中加入,代码:将你的窗体类替换 FormMain private void FormMain_...
  • lpcz2010
  • lpcz2010
  • 2015年12月25日 13:21
  • 899

jquery-easyui-1.4.4 汉化版

(2组官方演示,增2首页) 有了 jQuery EasyUI 框架的辅助,构建起网页来轻而易举哦! easyui 简于形而强于心。...
  • ranzj
  • ranzj
  • 2015年12月20日 23:32
  • 901

VS快捷键C#

1、alt+shift+F10,引用一个类时,定位找到其命名空间并且引用其命名空间。 2、F12,转到类的定义页面。 3、选中所要注释(反注释)的行或段落,     注释:Ctrl+K, Ctrl+C...
  • brightyanmin
  • brightyanmin
  • 2016年04月24日 10:26
  • 822

C#全局热键设置与窗体热键设置实例

1、窗体热键 首先要设置主窗体KeyPreview为true,可直接在属性中进行设置, 或者在窗体加载中设置: this.KeyPreview = true; 然后添加窗体KeyDown事件,如...
  • cuoban
  • cuoban
  • 2016年02月26日 16:16
  • 3628

C#常用快捷键和代码段

VS2010 快速包含语句块 Ctrl+ K ,S 快速添加属性prop 快速添加构造函数ctor 快速生成方法存根 Ctrl+K,M 格式化代码:Ctrl+K,F...
  • limlimlim
  • limlimlim
  • 2013年06月06日 10:27
  • 1377

C++Primer第五版 1.4.4节练习

练习 1.17:如果输入的所有值都是相等的,本节的程序会输出什么?如果没有重复值,输出又会是怎样的?   练习 1.18:编译并运行本节的程序,给它输入全都相等的值。再次运行程序,输入没有重复的值...
  • fengzhanghao23
  • fengzhanghao23
  • 2015年09月10日 21:04
  • 860
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C# BackupSupport 1.4.4 (管理快捷键)
举报原因:
原因补充:

(最多只允许输入30个字)