public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     if (destinationType == typeof(string))
     {
         if (value == null)
         {
             return("");
         }
         var keyGesture = value as AnyKeyGesture;
         if (keyGesture == null)
         {
             throw GetConvertToException(value, destinationType);
         }
         if (keyGesture.Key == Key.None)
         {
             return("");
         }
         string strKey = _keyConverter.ConvertToString(keyGesture.Key, culture, context);
         if (strKey == "")
         {
             return("");
         }
         string strModifiers = _modifierKeysConverter.ConvertToString(keyGesture.Modifiers, culture, context);
         return((strModifiers != "" ? strModifiers + ModifiersDelimiter : "")
                + strKey
                + (keyGesture.DisplayString != "" ? DisplayStringSeparator + keyGesture.DisplayString : ""));
     }
     throw GetConvertToException(value, destinationType);
 }
Esempio n. 2
0
        private static string GetModifierString(ModifierKeys modifier)
        {
            string modifierStr = null;

            if (_localizationResourceManager != null)
            {
                modifierStr = _localizationResourceManager.GetString("ModifierKeys_" + modifier);
            }

            return(modifierStr ?? ModifierKeysConverter.ConvertToString(modifier));
        }
Esempio n. 3
0
        public override string ToString()
        {
            string modifiers = null;

            if (ModifierKeys != ModifierKeys.None)
            {
                var converter = new ModifierKeysConverter();
                modifiers = converter.ConvertToString(ModifierKeys);
            }

            string key = null;

            if (Key != Key.None)
            {
                var keyConverter = new KeyConverter();
                key = keyConverter.ConvertToString(Key);
            }

            return(string.IsNullOrEmpty(key)
                       ? string.Empty
                       : (string.IsNullOrEmpty(modifiers) ? key : string.Format("{0}+{1}", modifiers, key)));
        }
Esempio n. 4
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;
        }
Esempio n. 5
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();
        }