private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                e.Handled = true;

                ListView focussedLV = null;

                if (Keyboard.FocusedElement is ListViewItem) focussedLV = ((FrameworkElement)Keyboard.FocusedElement).GetAncestorOfType<ListView>();

                var settings = HyperSearchSettings.Instance().Input;
                var elementWithFocus = Keyboard.FocusedElement as UIElement;

                DependencyObject listViewItem = null;

                if (listView.SelectedIndex >= 0)
                    listViewItem = listView.ItemContainerGenerator.ContainerFromIndex(listView.SelectedIndex);

                var ctrl = FindISettingsControls(listViewItem).FirstOrDefault();

                SettingsListViewItem item = listView.SelectedItem as SettingsListViewItem;
                SettingTypeAttribute attrib = null;

                if (item != null) attrib = item.SettingType;

                var actionKey = new KeyList(Key.Enter);

                if (settings.Action != null && settings.Action.Keys.Count > 0) actionKey = settings.Action;

                if (actionKey.Is(e.Key) && attrib != null)
                {
                    if (ctrl != null)
                    {
                        if (ctrl is TrueFalse)
                        {
                            ((TrueFalse)ctrl).IsChecked = !((TrueFalse)ctrl).IsChecked;
                            item.Property.SetValue(item.ParentObject, ((TrueFalse)ctrl).IsChecked, null);
                        }
                        else if (ctrl is MultiOption)
                        {
                            if (attrib != null)
                            {
                                if (attrib.Type == SettingsType.FolderPath)
                                {
                                    var dlg = new System.Windows.Forms.FolderBrowserDialog();

                                    if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                    {
                                        item.Value = dlg.SelectedPath;
                                    }

                                }
                                else if (attrib.Type == SettingsType.FilePath)
                                {
                                    var dlg = new System.Windows.Forms.OpenFileDialog();

                                    dlg.Multiselect = false;
                                    dlg.Filter = "RocketLauncher|Rocketlauncher.exe|All filers (*.*)|*.*";

                                    if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                    {
                                        item.Value = dlg.FileName;
                                    }
                                }
                                else if (attrib.ActionType != SettingActionType.None)
                                {
                                    HandleActionType(attrib);
                                }
                                else // default
                                {
                                    if (attrib.EnumSource != null)
                                    {
                                        //var items = attrib.MutliValueCsv.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                        var items = Enum.GetNames(attrib.EnumSource);

                                        MultiOptionSelector win = new MultiOptionSelector();

                                        win.SetItemSource(items);

                                        if (Win.Modal(win, this))
                                        {
                                            if (win.SelectedItem != null)
                                            {
                                                var o = (TextInputType)Enum.Parse(attrib.EnumSource, win.SelectedItem.ToString());

                                                if (o == TextInputType.Orb)
                                                {
                                                    Alert.ShowExclamation("Orb type not yet supported", secondsBeforeWeMayClose:0);
                                                    return;
                                                }

                                                item.Value = win.SelectedItem.ToString();
                                                item.Property.SetValue(item.ParentObject, o, null);
                                            }
                                        }
                                    }
                                }

                            }
                        }
                        else
                        {
                            ctrl.IsActive = !ctrl.IsActive;
                        }
                    }
                    else
                    {
                        if (attrib.Type == SettingsType.Action)
                        {
                            HandleActionType(attrib);
                        }
                    }
                }
                else if (settings.Clear.Is(e.Key))
                {
                    if (ctrl == null) return;

                    ctrl.ResetValue();
                    item.Property.SetValue(item.ParentObject, null, null);
                }
                else if (settings.Exit.Is(e.Key))
                {
                    this.Close();
                }
                else if (settings.Down.Is(e.Key))
                {
                    if (focussedLV == sectionListView)
                    {
                        listView.SelectAndFocusItem();
                    }
                    else
                    {
                        if (listView.SelectedIndex >= listView.Items.Count - 1)
                        {
                            listView.SelectedIndex = 0;
                            listView.ScrollIntoView(listView.SelectedItem);
                            listView.SelectAndFocusItem(0);
                        }
                        else
                        {
                            elementWithFocus.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                        }
                    }
                }
                else if (settings.Up.Is(e.Key))
                {
                    if (listView.SelectedIndex == 0)
                    {
                        listView.SelectedIndex = listView.Items.Count - 1;
                        listView.ScrollIntoView(listView.SelectedItem);
                        listView.SelectAndFocusItem(listView.Items.Count - 1);
                    }
                    else
                    {
                        elementWithFocus.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
                    }
                }
                else if (settings.Left.Is(e.Key))
                {
                    if (sectionListView.SelectedIndex >= 1)
                    {
                        sectionListView.SelectedIndex = sectionListView.SelectedIndex - 1;
                        sectionListView.ScrollIntoView(sectionListView.SelectedItem);
                    }
                    else
                    {
                        sectionListView.SelectAndFocusItem(sectionListView.Items.Count - 1);
                    }
                }
                else if (settings.Right.Is(e.Key))
                {
                    if (sectionListView.SelectedIndex < sectionListView.Items.Count - 1)
                    {
                        sectionListView.SelectedIndex = sectionListView.SelectedIndex + 1;
                        sectionListView.ScrollIntoView(sectionListView.SelectedItem);
                    }
                    else
                    {
                        sectionListView.SelectAndFocusItem(0);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.HandleException(ex);
            }
        }
        private void OnSelectLayoutTemplate()
        {
            var dataTemplate = this.Resources["ButtonLayoutSelectorListViewItemDataTemplate"] as DataTemplate;

            MultiOptionSelector sel = new MultiOptionSelector(dataTemplate);

            sel.SetItemSource(_controllerLayoutTemplates.ToArray());

            sel.Width = 600;
            sel.Height = 600;

            Win.Modal(sel, this);

            //if (Win.Modal(sel, this))
            {
                ControllerLayoutDefinition def = (ControllerLayoutDefinition)sel.SelectedItem;

                this.SelectedLayoutDefinition = def;

                layoutTemplate.Value = def.Name;

                //var layoutConfig = new LayoutConfig();

                //layoutConfig.Name = def.Name;
                //layoutConfig.Description = def.Description;
                //layoutConfig.Buttons = new List<ButtonConfig>();

                //foreach(var p in def.ButtonPositionMapping)
                //{
                //    layoutConfig.Buttons.Add(new ButtonConfig() { Position = p.Key, HueOffset = def.ButtonPositionMapping[p.Key].HueOffset });
                //}

                HyperSearchSettings.Instance().Input.LayoutConfig = def;

            }
        }