首页 > 后端开发 > C#.Net教程 > C# 根据KeyEventArgs与组合键字符串相互转换

C# 根据KeyEventArgs与组合键字符串相互转换

黄舟
发布: 2017-02-15 11:43:47
原创
1951 人浏览过

   /// 快捷键相关的类
    /// </summary>
    public static class HotKeyInfo
    {
        /// <summary>
        /// 根据KeyEventArgs生成组合键字符串
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string GetStringByKey(KeyEventArgs e)
        {
            if (e.KeyValue == 16)
            {
                return "Shift + ";
            }
            else if(e.KeyValue == 17)
            { 
                return "Ctrl + ";
            }
            else if(e.KeyValue == 18)
            {
                return "Alt + ";
            }
            else
            {
                StringBuilder keyValue = new StringBuilder();
                if (e.Modifiers != 0)
                {
                    if (e.Control)
                    {
                        keyValue.Append("Ctrl + ");
                    }
                    if (e.Alt)
                    {
                        keyValue.Append("Alt + ");
                    }
                    if (e.Shift)
                    {
                        keyValue.Append("Shift + ");
                    }
                }
                if ((e.KeyValue >= 48 && e.KeyValue <= 57))    //0-9
                {
                    keyValue.Append(e.KeyCode.ToString());
                    //keyValue.Append(e.KeyCode.ToString().Substring(1));
                }
                else
                {
                    keyValue.Append(e.KeyCode);
                }

                return keyValue.ToString();
            }
        }

        /// <summary>
        ///  根据按键获得单一键值对应字符串
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string GetSingleStrByKey(KeyEventArgs e)
        {
            if (e.KeyValue == 16)
            {
                return "Shift";
            }
            else if (e.KeyValue == 17)
            {
                return "Ctrl";
            }
            else if (e.KeyValue == 18)
            {
                return "Alt";
            }
            else
            {
                return e.KeyCode.ToString();
            }
        }

        /// <summary>
        /// 根据string生成KeyEventArgs
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static KeyEventArgs GetKeyByString(string strKey)
        {
            Keys keyResult = new Keys();
            string[] strKeyCodes = strKey.Split(&#39;+&#39;);
            if (strKeyCodes.Length > 0)
            {
                int numberKey;
                foreach (string keyEach in strKeyCodes)
                {
                    if (keyEach.Trim().ToUpper() == "CTRL")
                    {
                        keyResult = keyResult | Keys.Control;
                    }
                    else if (keyEach.Trim().ToUpper() == "SHIFT")
                    {
                        keyResult = keyResult | Keys.Shift;
                    }
                    else if (keyEach.Trim().ToUpper() == "ALT")
                    {
                        keyResult = keyResult | Keys.Alt;
                    }
                    //数字
                    else if (int.TryParse(keyEach, out numberKey))
                    {
                        KeysConverter converter = new KeysConverter();
                        Keys getKey = (Keys)converter.ConvertFromString(&#39;D&#39; + keyEach);
                        keyResult = keyResult | getKey;
                    }
                    //其他(字母,F0-F12)
                    else
                    {
                        KeysConverter converter = new KeysConverter();
                        Keys getKey = (Keys)converter.ConvertFromString(keyEach);
                        keyResult = keyResult | getKey;
                    }
                }
 
            }
            KeyEventArgs newEventArgs = new KeyEventArgs(keyResult);
            return newEventArgs;
        }
    }
登录后复制

以上就是C# 根据KeyEventArgs与组合键字符串相互转换的内容,更多相关内容请关注PHP中文网(www.php.cn)!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板