Exemple #1
0
        bool StringToAccelerator(string shortcut)
        {
            bool parsed = true;

            if (!IsStringASCII(shortcut))
            {
                throw new NotSupportedException("The accelerator string can only contain ASCII characters");
            }

            AcceleratorModifiers modifiers = 0;
            string code    = string.Empty;
            bool   shifted = false;

            var tokens = shortcut.Split(separator, StringSplitOptions.RemoveEmptyEntries);

            foreach (var token in tokens)
            {
                AcceleratorModifiers modifier = 0;
                if (Enum.TryParse(token, true, out modifier))
                {
                    modifiers |= modifier;
                }
                else
                {
                    if (token.Length == 1)
                    {
                        (code, shifted) = KeyboardUtilities.KeyboardCodeFromCharCode(token[0]);
                    }
                    else
                    {
                        AcceleratorKeys c;
                        if (Enum.TryParse(token, true, out c))
                        {
                            code = token;
                        }
                    }
                }
            }

            if (code == string.Empty)
            {
                throw new ArgumentException($"Argument: {nameof(shortcut)} does not contain a valid key.");
            }

            accelerator = shortcut;

            return(parsed);
        }
Exemple #2
0
        private Accelerator(string key, params AcceleratorModifiers[] modifiers)
        {
            var code  = key;
            var shift = false;

            if (key.Length == 1)
            {
                (code, shift) = KeyboardUtilities.KeyboardCodeFromCharCode(key[0]);
            }

            AcceleratorModifiers mods = (shift) ? AcceleratorModifiers.Shift : 0;

            foreach (var mod in modifiers)
            {
                mods |= mod;
            }
            var accel = new StringBuilder(string.Join("+", mods.ToString().Split(',').Select(t => t.Trim()).ToArray()));

            accel.Append($"+{code}");
            accelerator = accel.ToString();
        }