예제 #1
0
        private void ReloadKeyBindings(MainWindowViewModel viewModel)
        {
            try
            {
                RemoveAllKeyBindings(viewModel);
                globalHotkeyHooks = new GlobalHotKey[Config.Instance.HotkeyConfigItems.Length];

                if (GeneralHelper.AreHotKeyConfigItemsValid(Config.Instance.HotkeyConfigItems, false))
                {
                    for (var i = 0; i < Config.Instance.HotkeyConfigItems.Length; i++)
                    {
                        if (Config.Instance.HotkeyConfigItems[i].TextKey > 0 && Config.Instance.HotkeyConfigItems[i].TextModifierKeys > 0)
                        {
                            ModifierKeys modifierKeys = (ModifierKeys)Config.Instance.HotkeyConfigItems[i].TextModifierKeys;
                            Key          mainKey      = (Key)Config.Instance.HotkeyConfigItems[i].TextKey;

                            //use global registred hotkeys or local
                            if (Config.Instance.UseGlobalHotkeys)
                            {
                                globalHotkeyHooks[i] = new GlobalHotKey(modifierKeys, mainKey.ToKeys(), this, i);
                                globalHotkeyHooks[i].HotKeyPressed += (globalHotKey) =>
                                {
                                    viewModel.HotkeyItemCollection[globalHotKey.ConfigId].MainAction(true);
                                };
                            }
                            else
                            {
                                InputBinding ib = new InputBinding(viewModel.HotkeyItemCollection[i].MainActionTextCommand, new KeyGesture(mainKey, modifierKeys));
                                this.InputBindings.Add(ib);
                            }
                        }
                    }
                }
                else
                {
                    Config.Instance.ResetHotkeySettings();
                    RemoveAllKeyBindings(viewModel);
                    GeneralHelper.ShowMessageBox("De sneltoetsen konden niet geconfigureerd worden, de instellingen worden gereset!", MessageBoxImage.Error, this);
                }
            }
            catch (Exception e)
            {
                Config.Instance.ResetHotkeySettings();
                RemoveAllKeyBindings(viewModel);
                GeneralHelper.ShowMessageBox("Er is een fout opgetreden bij het laden van de hotkeys, ze worden gereset en allemaal verwijderd!", MessageBoxImage.Error, this);
            }
        }