示例#1
0
        KeySequence UnicodeKeySequence(string keys)
        {
            var keySequence = new KeySequence();

            foreach (var ch in keys)
            {
                keySequence.Add(Key.FromKeySymOrChar(Convert.ToString(ch)));
            }
            return(keySequence);
        }
示例#2
0
        private void ParseRule(string line)
        {
            // If this is an include directive, use LoadFile() again
            Match m0 = m_r0.Match(line);

            if (m0.Success)
            {
                string file = m0.Groups[1].Captures[0].Value;

                // We support %H (user directory) but not %L (locale-specific dir)
                if (file.Contains("%L"))
                {
                    return;
                }
                file = file.Replace("%H", Utils.UserDir);

                // Also if path is not absolute, prepend user directory
                if (!Path.IsPathRooted(file))
                {
                    file = Path.Combine(Utils.UserDir, file);
                }

                // Prevent against include recursion
                if (!m_loaded_files.Contains(file))
                {
                    LoadFile(file);
                }

                return;
            }

            var m1 = m_r1.Match(line);

            if (!m1.Success)
            {
                return;
            }

            KeySequence seq     = new KeySequence();
            var         keysyms = m_r2.Split(m1.Groups[1].Captures[0].Value);

            for (int i = 1; i < keysyms.Length - 1; ++i)
            {
                Key k = Key.FromKeySymOrChar(keysyms[i]);
                if (k == null)
                {
                    if (!m_invalid_keys.ContainsKey(keysyms[i]))
                    {
                        m_invalid_keys[keysyms[i]] = true;
                        Log.Debug($"Unknown key name <{keysyms[i]}>, ignoring sequence");
                    }
                    return; // Unknown key name! Better bail out
                }

                seq.Add(k);
            }

            // Only bother with sequences of length >= 2 that start with <Multi_key>
            if (seq.Count < 2 || seq[0].VirtualKey != VK.COMPOSE)
            {
                return;
            }

            string result = m1.Groups[2].Captures[0].Value;

            if (result[0] == '"')
            {
                result = result.Trim('"');
                // Unescape \n \\ \" and more in the string output
                result = Regex.Replace(result, @"\\.", m =>
                {
                    switch (m.Value)
                    {
                    // These sequences are converted to their known value
                    case @"\n": return("\n");

                    case @"\r": return("\r");

                    case @"\t": return("\t");

                    // For all other sequences, just strip the leading \
                    default: return(m.Value.Substring(1));
                    }
                });
            }
            else
            {
                var result_key = Key.FromKeySymOrChar(result);
                if (result_key == null)
                {
                    Log.Debug($"Unknown key name {result}, ignoring sequence");
                    return;
                }
                result = result_key.PrintableResult;
            }
            string description = m1.Groups.Count >= 5 ? m1.Groups[4].Captures[0].Value : "";

            // Try to translate the description if appropriate
            int utf32 = StringToCodepoint(result);

            if (utf32 >= 0)
            {
                string key      = $"U{utf32:X04}";
                string alt_desc = unicode.Char.ResourceManager.GetString(key);
                if (!string.IsNullOrEmpty(alt_desc))
                {
                    description = alt_desc;
                }
            }

            // HACK: remove the first key (Multi_key) for now, because the
            // rest of the code cannot handle it.
            seq.RemoveAt(0);

            InsertSequence(seq, result, utf32, description);
            ++Count;
        }