如何检测WPF中的修饰键的状态?

是否有一些全局的结构,我可以使用,无论我需要访问控制,Shift键,Alt键是否closures? 例如在TreeView MouseDown事件中。

如果这样怎么样?

使用类Keyboard 。 使用Keyboard.IsKeyDown你可以检查控制,Shift,Alt现在是否closures。

换档:

 if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) { /* Your code */ } 

对于控制:

 if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { /* Your code */ } 

对于Alt:

 if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt)) { /* Your code */ } 

还有:

 // Have to get this value before opening a dialog, or user will have released the control key if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) { } 
  private bool IsShiftKey { get; set; } private void OnPreviewKeyDown(object sender, KeyEventArgs e) { IsShiftKey = Keyboard.Modifiers == ModifierKeys.Shift ? true : false; if ((Key.Oem3 == e.Key || ((IsShiftKey && Key.Oem4 == e.Key) || (IsShiftKey && Key.Oem6 == e.Key) || (IsShiftKey && Key.Oem5 == e.Key)) && (validatorDefn as FormatValidatorDefinition).format == "packedascii")) { e.Handled = true; } }