如何在C#Windows程序中单击按钮时确定何时按住控制键?我想为Ctrl/Click执行一个操作,为Click执行另一个操作.
还有一点:
private void button1_Click ( object sender, EventArgs e ) { if( (ModifierKeys & Keys.Control) == Keys.Control ) { ControlClickMethod(); } else { ClickMethod(); } } private void ControlClickMethod() { MessageBox.Show( "Control is pressed" ); } private void ClickMethod() { MessageBox.Show ( "Control is not pressed" ); }
假设WinForms,使用Control.ModifierKeys,例如:
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(Control.ModifierKeys.ToString()); }
假设使用WPF,请使用Keyboard.Modifiers,例如:
private void Button_Click(object sender, RoutedEventArgs e) { MessageBox.Show(Keyboard.Modifiers.ToString()); }