private void KeyValidationTextBox(object sender, KeyEventArgs e) { e.Handled = true; var modifiers = Keyboard.Modifiers; var key = e.Key; ushort keyCode = (ushort)System.Windows.Input.KeyInterop.VirtualKeyFromKey(key); MSG l_Msg; ushort l_Scancode; PresentationSource source = e.InputSource; var l_MSGField = source.GetType().GetField("_lastKeyboardMessage", BindingFlags.NonPublic | BindingFlags.Instance); l_Msg = (MSG)l_MSGField.GetValue(source); l_Scancode = (ushort)(l_Msg.lParam.ToInt32() >> 16); if (modifiers == ModifierKeys.None && (key == Key.Delete || key == Key.Back || key == Key.Escape)) { hotKey = null; KeyTextBox.Text = ""; return; } if (key == Key.System) { key = e.SystemKey; } if (key == Key.LeftCtrl || key == Key.RightCtrl || key == Key.RWin || key == Key.LeftAlt || key == Key.LeftShift || key == Key.RightAlt || key == Key.RightShift || key == Key.LWin || key == Key.OemClear || key == Key.Apps || key == Key.Clear) { return; } if (modifiers != ModifierKeys.None) { if (modifiers == ModifierKeys.Shift || modifiers == ModifierKeys.Control) { hotKey = new Hotkey(key, modifiers, keyCode, l_Scancode, true); } else { return; } } else { hotKey = new Hotkey(key, modifiers, keyCode, l_Scancode, false); } KeyTextBox.Text = hotKey.ToString(); }