예제 #1
0
        private void Load(string path, string basePath)
        {
            if (!File.Exists(basePath))
            {
                return;
            }

            var xmlBaseReader = new CommonXmlReader(basePath);
            var baseDic       = new KeyConfigDictionary()
            {
                { "shortcutnames", xmlBaseReader.GetAttributes("shortcutname", "shortcuts/shortcut") },
                { "specialkeies", xmlBaseReader.GetAttributes("specialkey", "shortcuts/shortcut") },
                { "mainkeies", xmlBaseReader.GetAttributes("mainkey", "shortcuts/shortcut") },
                { "descriptions", xmlBaseReader.GetValues("shortcuts/shortcut") }
            };

            var modConverter = new ModifierKeysConverter();
            var keyConverter = new KeyConverter();

            for (int i = 0; i < baseDic.MinValueCount; ++i)
            {
                ModifierKeys specialKey = (ModifierKeys)modConverter.ConvertFromString(baseDic["specialkeies"][i]);
                Key          mainKey    = (Key)keyConverter.ConvertFromString(baseDic["mainkeies"][i]);
                ShortcutKeies.Add(baseDic["shortcutnames"][i],
                                  new ShortcutKey(baseDic["shortcutnames"][i], specialKey, mainKey, baseDic["descriptions"][i].TrimEnd('\n').TrimEnd('\r')));
            }

            if (!File.Exists(path))
            {
                return;
            }

            var xmlReader = new CommonXmlReader(path);
            var dic       = new KeyConfigDictionary
            {
                { "shortcutnames", xmlReader.GetAttributes("shortcutname", "shortcuts/shortcut") },
                { "specialkeies", xmlReader.GetAttributes("specialkey", "shortcuts/shortcut") },
                { "mainkeies", xmlReader.GetAttributes("mainkey", "shortcuts/shortcut") }
            };

            for (int i = 0; i < dic.MinValueCount; ++i)
            {
                ModifierKeys specialKey;
                if (dic["specialkeies"][i].Equals("None"))
                {
                    specialKey = ModifierKeys.None;
                }
                else
                {
                    specialKey = (ModifierKeys)modConverter.ConvertFromString(dic["specialkeies"][i]);
                }
                Key    mainKey     = (Key)keyConverter.ConvertFromString(dic["mainkeies"][i]);
                string description = ShortcutKeies[dic["shortcutnames"][i]].Description;
                ShortcutKeies[dic["shortcutnames"][i]] = new ShortcutKey(dic["shortcutnames"][i], specialKey, mainKey, description);
            }
            return;
        }
예제 #2
0
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is string)
            {
                string bindingString = value as string;

                if (!string.IsNullOrEmpty(bindingString))
                {
                    KeyBinding keybind = new KeyBinding();

                    string[] values = bindingString.Split('+');

                    for (int i = 0; i < values.Length; i++)
                    {
                        if (i == values.Length - 1)
                        {
                            KeyConverter k = new KeyConverter();
                            keybind.Key = (Key)k.ConvertFromString(values[i]);
                        }
                        else
                        {
                            ModifierKeysConverter conv = new ModifierKeysConverter();
                            keybind.Modifiers = keybind.Modifiers | (ModifierKeys)conv.ConvertFromString(values[i]);
                        }
                    }

                    return(keybind);
                }

                return(null);
            }

            return(null);
        }
예제 #3
0
        public virtual void SetKeys(string modifiers, string key)
        {
            ModifierKeysConverter mod_ser = new ModifierKeysConverter();

            Modifiers_ = (ModifierKeys)mod_ser.ConvertFromString(modifiers);
            KeyConverter key_ser = new KeyConverter();

            Key_ = (Key)key_ser.ConvertFromString(key);
        }
예제 #4
0
파일: App.xaml.cs 프로젝트: zerkms/shary
        private void InitializeHotKeys()
        {
            var kc  = new SWF.KeysConverter();
            var mkc = new ModifierKeysConverter();

            _sshHotkey = new HotKey((ModifierKeys)mkc.ConvertFromString(_config.Hotkeys.Select.Modifiers), (SWF.Keys)kc.ConvertFromString(_config.Hotkeys.Select.Key));
            _sshHotkey.HotKeyPressed += k => ((MainWindow)this.MainWindow).ShowFullscreenWindow();

            Exit += (s, e) =>
            {
                _sshHotkey.Dispose();
            };
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source)
        {
            if (source is string)
            {
                var    str      = (string)source;
                string fullName = str.Trim();
                if (fullName == "")
                {
                    return(new AnyKeyGesture(Key.None));
                }

                string keyToken;
                string modifiersToken;
                string displayString;

                // Break apart display string
                int index = fullName.IndexOf(DisplayStringSeparator);
                if (index >= 0)
                {
                    displayString = fullName.Substring(index + 1).Trim();
                    fullName      = fullName.Substring(0, index).Trim();
                }
                else
                {
                    displayString = "";
                }

                // Break apart key and modifiers
                index = fullName.LastIndexOf(ModifiersDelimiter);
                if (index >= 0)
                {
                    modifiersToken = fullName.Substring(0, index);
                    keyToken       = fullName.Substring(index + 1);
                }
                else
                {
                    modifiersToken = "";
                    keyToken       = fullName;
                }

                return(new AnyKeyGesture(
                           _keyConverter.ConvertFromString <Key>(keyToken, culture, context),
                           _modifierKeysConverter.ConvertFromString <ModifierKeys>(modifiersToken, culture, context),
                           displayString));
            }
            throw GetConvertFromException(source);
        }
예제 #6
0
        public MultiKeyGestureExtension(params string[] textPieces)
        {
            var text = ", ".Join(textPieces);

            if (text.Contains("+"))
            {
                var parts = text.Split('+');
                text = parts[1];
                mods = (ModifierKeys)modifierConverter.ConvertFromString(parts[0]);
            }
            else
            {
                mods = ModifierKeys.None;
            }

            keys = text.Split(',').Select(k => (Key)keyConverter.ConvertFromString(k.Trim())).ToArray();
        }
        private ModifierKeys GetModifiers(string[] keyStrings)
        {
            ModifierKeys combined  = ModifierKeys.None;
            var          converter = new ModifierKeysConverter();

            foreach (var keyString in keyStrings)
            {
                try
                {
                    var key = (ModifierKeys)converter.ConvertFromString(keyString);
                    combined = combined | key;
                }
                catch (Exception /*e*/)
                {
                    // The key was not a modifier key, just skip it.
                }
            }

            return(combined);
        }
예제 #8
0
        private static void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var textBox = sender as TextBox;

            if (!_ignoredKey.Contains(e.Key) && (e.Key != Key.System || (e.Key == Key.System && !_ignoredKey.Contains(e.SystemKey))))
            {
                var keys   = (e.Key == Key.System && !_ignoredKey.Contains(e.SystemKey)) ? e.SystemKey : e.Key;
                var hotKey = new HotKey()
                {
                    Ctrl  = ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control),
                    Alt   = ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt),
                    Shift = ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift),
                    Key   = keys
                };
                if (hotKey.Ctrl || hotKey.Alt || hotKey.Shift == true)
                {
                    var kc  = new KeyConverter();
                    var mkc = new ModifierKeysConverter();
                    if (textBox.Name == "HotKeyQ")
                    {
                        if (e.Key == Key.C)
                        {
                            return;
                        }

                        Key          key = (Key)kc.ConvertFromString(Settings.Default.hotKeyQ);
                        ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModQ);

                        QTranse.HotKeyManage.Unregister(key, mod);
                        QTranse.HotKeyManage.Register(e.Key, Keyboard.Modifiers);
                        Settings.Default.hotKeyQ    = kc.ConvertToString(e.Key);
                        Settings.Default.hotKeyModQ = mkc.ConvertToString(Keyboard.Modifiers);
                        Settings.Default.Save();
                        QTranse.Mvvm.HotKeyQ = string.Format($"{hotKey}");
                    }
                    if (textBox.Name == "HotKeyW")
                    {
                        if (e.Key == Key.C)
                        {
                            return;
                        }

                        Key          key = (Key)kc.ConvertFromString(Settings.Default.hotKeyW);
                        ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModW);

                        QTranse.HotKeyManage.Unregister(key, mod);
                        QTranse.HotKeyManage.Register(e.Key, Keyboard.Modifiers);
                        Settings.Default.hotKeyW    = kc.ConvertToString(e.Key);
                        Settings.Default.hotKeyModW = mkc.ConvertToString(Keyboard.Modifiers);
                        Settings.Default.Save();
                        QTranse.Mvvm.HotKeyW = string.Format($"{hotKey}");
                    }
                    if (textBox.Name == "HotKeyB")
                    {
                        if (e.Key == Key.C)
                        {
                            return;
                        }

                        Key          key = (Key)kc.ConvertFromString(Settings.Default.hotKeyB);
                        ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModB);

                        QTranse.HotKeyManage.Unregister(key, mod);
                        QTranse.HotKeyManage.Register(e.Key, Keyboard.Modifiers);
                        Settings.Default.hotKeyB    = kc.ConvertToString(e.Key);
                        Settings.Default.hotKeyModB = mkc.ConvertToString(Keyboard.Modifiers);
                        Settings.Default.Save();
                        QTranse.Mvvm.HotKeyB = string.Format($"{hotKey}");
                    }
                    if (textBox.Name == "HotKeyG")
                    {
                        if (e.Key == Key.C)
                        {
                            return;
                        }

                        Key          key = (Key)kc.ConvertFromString(Settings.Default.hotKeyG);
                        ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModG);

                        QTranse.HotKeyManage.Unregister(key, mod);
                        QTranse.HotKeyManage.Register(e.Key, Keyboard.Modifiers);
                        Settings.Default.hotKeyG    = kc.ConvertToString(e.Key);
                        Settings.Default.hotKeyModG = mkc.ConvertToString(Keyboard.Modifiers);
                        Settings.Default.Save();
                        QTranse.Mvvm.HotKeyG = string.Format($"{hotKey}");
                    }
                }
            }
            e.Handled = true;
        }
예제 #9
0
        private void InitlializeShortucts()
        {
            // shortcuts dictionary
            _shortcuts = new Dictionary <Tuple <Key, ModifierKeys>, Action>();

            // key converters
            var keyConverter          = new KeyConverter();
            var modifiersKeyConverter = new ModifierKeysConverter();

            // open shortcut
            _shortcuts.Add(
                new Tuple <Key, ModifierKeys>(
                    (Key)keyConverter.ConvertFromString("O"),
                    (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")),
                () => Open());

            // save shortcut
            _shortcuts.Add(
                new Tuple <Key, ModifierKeys>(
                    (Key)keyConverter.ConvertFromString("S"),
                    (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")),
                () => Save());

            // export shortcut
            _shortcuts.Add(
                new Tuple <Key, ModifierKeys>(
                    (Key)keyConverter.ConvertFromString("E"),
                    (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")),
                () => Export());

            // undo shortcut
            _shortcuts.Add(
                new Tuple <Key, ModifierKeys>(
                    (Key)keyConverter.ConvertFromString("Z"),
                    (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")),
                () => _view.Undo());

            // redo shortcut
            _shortcuts.Add(
                new Tuple <Key, ModifierKeys>(
                    (Key)keyConverter.ConvertFromString("Y"),
                    (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")),
                () => _view.Redo());

            // snap shortcut
            _shortcuts.Add(
                new Tuple <Key, ModifierKeys>(
                    (Key)keyConverter.ConvertFromString("S"),
                    ModifierKeys.None),
                () => _view.ToggleSnap());

            // clear shortcut
            _shortcuts.Add(
                new Tuple <Key, ModifierKeys>(
                    (Key)keyConverter.ConvertFromString("Delete"),
                    (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")),
                () => _view.Clear());

            // editor shortcuts
            foreach (var editor in _view.Editors)
            {
                var _editor = editor;
                _shortcuts.Add(
                    new Tuple <Key, ModifierKeys>(
                        (Key)keyConverter.ConvertFromString(editor.Key),
                        editor.Modifiers == "" ? ModifierKeys.None : (ModifierKeys)modifiersKeyConverter.ConvertFromString(editor.Modifiers)),
                    () => _view.Enable(_editor));
            }

            // block shortcut
            _shortcuts.Add(
                new Tuple <Key, ModifierKeys>(
                    (Key)keyConverter.ConvertFromString("G"),
                    ModifierKeys.None),
                () => _view.CreateBlock());

            // delete shortcut
            _shortcuts.Add(
                new Tuple <Key, ModifierKeys>(
                    (Key)keyConverter.ConvertFromString("Delete"),
                    ModifierKeys.None),
                () => _view.Delete());
        }
예제 #10
0
        // 热键注册/响应
        private void RegisterHotKey()
        {
            var kc  = new KeyConverter();
            var mkc = new ModifierKeysConverter();

            try
            {
                if (Settings.Default.hotKeyModQ != "" && Settings.Default.hotKeyQ != "")
                {
                    ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModQ);
                    Key          key = (Key)kc.ConvertFromString(Settings.Default.hotKeyQ);

                    HotKeyManage.Register(key, mod);
                }
                else
                {
                    HotKeyManage.Register(Key.Q, ModifierKeys.Control);

                    Settings.Default.hotKeyModQ = mkc.ConvertToString(ModifierKeys.Control);
                    Settings.Default.hotKeyQ    = kc.ConvertToString(Key.Q);
                }
                Mvvm.HotKeyQ = HotKeyManage.ToString();
            }

            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
                Mvvm.HotKeyQ = HotKeyManage.ToString() + "(冲突)";
            }

            //////////////////////////////////////////////////


            try
            {
                if (Settings.Default.hotKeyModW != "" && Settings.Default.hotKeyW != "")
                {
                    ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModW);
                    Key          key = (Key)kc.ConvertFromString(Settings.Default.hotKeyW);

                    HotKeyManage.Register(key, mod);
                }
                else
                {
                    HotKeyManage.Register(Key.W, ModifierKeys.Control);

                    Settings.Default.hotKeyModW = mkc.ConvertToString(ModifierKeys.Control);
                    Settings.Default.hotKeyW    = kc.ConvertToString(Key.W);
                }
                Mvvm.HotKeyW = HotKeyManage.ToString();
            }

            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
                Mvvm.HotKeyW = HotKeyManage.ToString() + "(冲突)";
            }

            //////////////////////////////////////////////////

            try
            {
                if (Settings.Default.hotKeyModB != "" && Settings.Default.hotKeyB != "")
                {
                    ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModB);
                    Key          key = (Key)kc.ConvertFromString(Settings.Default.hotKeyB);

                    HotKeyManage.Register(key, mod);
                }
                else
                {
                    HotKeyManage.Register(Key.B, ModifierKeys.Control);

                    Settings.Default.hotKeyModB = mkc.ConvertToString(ModifierKeys.Control);
                    Settings.Default.hotKeyB    = kc.ConvertToString(Key.B);
                }
                Mvvm.HotKeyB = HotKeyManage.ToString();
            }

            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
                Mvvm.HotKeyB = HotKeyManage.ToString() + "(冲突)";
            }

            //////////////////////////////////////////////////

            try
            {
                if (Settings.Default.hotKeyModG != "" && Settings.Default.hotKeyG != "")
                {
                    ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModG);
                    Key          key = (Key)kc.ConvertFromString(Settings.Default.hotKeyG);

                    HotKeyManage.Register(key, mod);
                }
                else
                {
                    HotKeyManage.Register(Key.G, ModifierKeys.Control);

                    Settings.Default.hotKeyModG = mkc.ConvertToString(ModifierKeys.Control);
                    Settings.Default.hotKeyG    = kc.ConvertToString(Key.G);
                }
                Mvvm.HotKeyG = HotKeyManage.ToString();
            }

            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
                Mvvm.HotKeyG = HotKeyManage.ToString() + "(冲突)";
            }


            Settings.Default.Save();
        }