using System.Linq; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Input; using static QuikDawEditor.EDITING.ShorcutKeys; namespace QuikDawEditor { public partial class KeyInputWindow : Window { public UserCommandKeyPair thisUserCommandKeyPair; public bool UserCommandKeyPairSet = false; internal PreferencesWindow myPrefWin; public KeyInputWindow(UserCommandKeyPair ucKeyPair, PreferencesWindow mPWin) { InitializeComponent(); thisUserCommandKeyPair = ucKeyPair; FunctionNameTB.Text = ucKeyPair.CommandName; ShortcutKeyComboTB.Text = ucKeyPair.KeyGestureString; myPrefWin = mPWin; } private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Escape) this.Close(); //if (Keyboard.Modifiers == ModifierKeys.None) return; //if (Keyboard.Modifiers == ModifierKeys.Shift) return; if (Regex.IsMatch(e.Key.ToString(), @"(LeftCtrl|RightCtrl|LeftShift|RightShift|LeftAlt|RightAlt)")) return; string modString = ""; modString += (Keyboard.IsKeyDown(Key.LeftCtrl) | Keyboard.IsKeyDown(Key.LeftCtrl)) ? "Ctrl+" : ""; modString += (Keyboard.IsKeyDown(Key.LeftShift) | Keyboard.IsKeyDown(Key.RightShift)) ? "Shift+" : ""; modString += (Keyboard.IsKeyDown(Key.LeftAlt) | Keyboard.IsKeyDown(Key.RightAlt)) ? "Alt+" : ""; string newGestureString = modString + e.Key.ToString(); ; if (newGestureString.Contains("System")) { MessageBox.Show("Cannot use combination:\n\n" + newGestureString + "\n\n(used by system)"); return; } if (Regex.IsMatch(newGestureString, @"(Ctrl\+A$|Ctrl\+Z|Ctrl\+S|Ctrl\+Del|Del|Ctrl\+Home|Ctrl\+End|Enter|Space|R)")) { MessageBox.Show("Cannot use combination:\n\n" + newGestureString + "\n\n(used by QuikDaw)"); return; } //Check if already assigned UserCommandKeyPair foundUCKP = myPrefWin.currentUserCommandKeyPairs.Where(uckp => uckp.KeyGestureString == newGestureString).FirstOrDefault(); if (foundUCKP != null) { if (foundUCKP != thisUserCommandKeyPair) { MessageBox.Show("Key combination already used by:\n" + foundUCKP.CommandName); return; } } ShortcutKeyComboTB.Text = newGestureString; } private void CancelBut_Click(object sender, RoutedEventArgs e) { UserCommandKeyPairSet = false; this.Close(); } private void OkBut_Click(object sender, RoutedEventArgs e) { thisUserCommandKeyPair = new UserCommandKeyPair(thisUserCommandKeyPair.CommandName + "=" + ShortcutKeyComboTB.Text); UserCommandKeyPairSet = true; this.Close(); } } }