C# 根據KeyEventArgs與按鍵組合字串相互轉換

來源:互聯網
上載者:User
   /// 快速鍵相關的類    /// </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('+');            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('D' + 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與按鍵組合字串相互轉換的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.