/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ControlPanel_KeyDown(object sender, KeyEventArgs e) { if (!waitingForShortcut) return; switch (e.Key) { // convert right shift to left shift case Key.RightShift: if (!currentPressedKeys.Contains(Key.LeftShift)) currentPressedKeys.Add(Key.LeftShift); currentShortcutButton.Content = ParentWindow.GetModifiersAsText(currentPressedKeys); return; // catch modifier keys case Key.LeftShift: case Key.LeftCtrl: case Key.LeftAlt: case Key.LWin: case Key.RightCtrl: case Key.RightAlt: case Key.RWin: if (!currentPressedKeys.Contains(e.Key)) currentPressedKeys.Add(e.Key); currentShortcutButton.Content = ParentWindow.GetModifiersAsText(currentPressedKeys); return; // catch alt/left ctrl key when disguised as system key case Key.System: if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt || e.SystemKey == Key.LeftCtrl) { if (!currentPressedKeys.Contains(e.SystemKey)) currentPressedKeys.Add(e.SystemKey); currentShortcutButton.Content = ParentWindow.GetModifiersAsText(currentPressedKeys); return; } break; // ignore these keys case Key.None: case Key.DeadCharProcessed: return; default: break; } // TODO: Convert Oem keys to nice strings String currentKey = e.Key == Key.System ? ParentWindow.KeyToString(e.SystemKey) : ParentWindow.KeyToString(e.Key); String txt = ParentWindow.GetModifiersAsText(currentPressedKeys); if (txt.Length > 0) txt += "+" + currentKey; else txt = currentKey; KeyboardShortcutProfile profile = SettingsManager.GetCurrentShortcutProfile(); // see if shortcut already exists bool createdNew = false; foreach (KeyboardShortcut sc in profile.Shortcuts) { if (sc.Keys == txt && sc != currentShortcut) { string title = U.T("MessageShortcutClash", "Title"); string message = U.T("MessageShortcutClash", "Message"); string name = U.T("Shortcut_" + sc.Name.Replace(" ", "_")); string category = U.T("Shortcut" + sc.Category); message = message.Replace("%name", name); message = message.Replace("%category", category); if (MessageBox.Show(message, title, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { // if current profile is protected we create a copy of it KeyboardShortcut _sc = sc; if (profile.IsProtected) { profile = CreateShortcutProfile(); currentShortcut = SettingsManager.GetKeyboardShortcut(profile, currentShortcut.Keys); _sc = SettingsManager.GetKeyboardShortcut(profile, sc.Keys); createdNew = true; } Button b = (Button)shortcutButtons[sc.Category + "_" + sc.Name.Replace(" ", "_")]; if (b != null) { b.Content = U.T("ShortcutNotUsed"); b.FontStyle = FontStyles.Italic; } _sc.Keys = ""; break; } else { ResetShortcutButton(); e.Handled = true; return; } } } // if current profile is protected we create a copy of it (if we haven't already) if (profile.IsProtected && !createdNew) { profile = CreateShortcutProfile(); currentShortcut = SettingsManager.GetKeyboardShortcut(profile, currentShortcut.Keys); } // set shortcut and button text currentShortcutButton.FontStyle = FontStyles.Normal; waitingForShortcut = false; currentShortcutButton.Content = txt; if (currentShortcut != null) currentShortcut.Keys = txt; currentShortcut = null; currentShortcutButton = null; e.Handled = true; }
/// <summary> /// /// </summary> private void ResetShortcutButton() { if (!waitingForShortcut) return; waitingForShortcut = false; currentShortcutButton.Content = currentShortcut.Keys == "" ? U.T("ShortcutNotUsed") : currentShortcut.Keys; currentShortcutButton.FontStyle = currentShortcut.Keys == "" ? FontStyles.Italic : FontStyles.Normal; currentShortcut = null; currentShortcutButton = null; }
/// <summary> /// /// </summary> /// <param name="profile"></param> /// <param name="category"></param> /// <param name="name"></param> /// <param name="keysAsText"></param> /// <param name="isGlobal"></param> private static void SetKeyboardShortcut(KeyboardShortcutProfile profile, String category, String name, String keysAsText, bool isGlobal = false) { KeyboardShortcut sc = GetKeyboardShortcut(profile, category, name); if (sc == null) { sc = new KeyboardShortcut(); sc.Category = category; sc.Name = name; profile.Shortcuts.Add(sc); } SetKeyboardShortcut(sc, keysAsText, isGlobal); }
/// <summary> /// Invoked when the user clicks on a shortcut /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event data</param> private void PrefShortcutButton_Clicked(object sender, RoutedEventArgs e) { currentShortcutButton = sender as Button; string[] name = currentShortcutButton.Name.Split(new char[] { '_' }, 2); currentShortcut = SettingsManager.GetKeyboardShortcut(SettingsManager.GetCurrentShortcutProfile(), name[0], name[1].Replace("_", " ")); waitingForShortcut = true; U.ListenForShortcut = false; currentShortcutButton.Content = U.T("ShortcutPress"); currentShortcutButton.FontStyle = FontStyles.Italic; currentPressedKeys.Clear(); }
/// <summary> /// /// </summary> /// <param name="sc"></param> /// <param name="keysAsText"></param> /// <param name="isGlobal"></param> private static void SetKeyboardShortcut(KeyboardShortcut sc, String keysAsText, bool isGlobal = false) { sc.Keys = keysAsText; sc.IsGlobal = isGlobal; }