C # Converts each other based on KeyEventArgs and composite key strings

Source: Internet
Author: User
Tags key string
/// Shortcut related classes
    /// </ summary>
    public static class HotKeyInfo
    {
        /// <summary>
        /// Generate a composite key string based on 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>
        /// Get a single key value corresponding string according to the key
        /// </ 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>
        /// Generate KeyEventArgs based on string
        /// </ 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;
                    }
                    //digital
                    else if (int.TryParse (keyEach, out numberKey))
                    {
                        KeysConverter converter = new KeysConverter ();
                        Keys getKey = (Keys) converter.ConvertFromString ('D' + keyEach);
                        keyResult = keyResult | getKey;
                    }
                    // Others (letters, F0-F12)
                    else
                    {
                        KeysConverter converter = new KeysConverter ();
                        Keys getKey = (Keys) converter.ConvertFromString (keyEach);
                        keyResult = keyResult | getKey;
                    }
                }
 
            }
            KeyEventArgs newEventArgs = new KeyEventArgs (keyResult);
            return newEventArgs;
        }
    } 


The above is C # based on KeyEventArgs and the combination of key strings to convert the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.