When you create an application that accepts user keystrokes, you may also want to monitor key combinations, such as the SHIFT, ALT, and CTRL keys. When a combination key is pressed at the same time as another key, or when you press the mouse at the same time, your application can respond appropriately: the letter S may cause only one "s" to appear on the screen, but if you press Ctrl+s, you can save the current document.
To determine which key combination is pressed
Use the bitwise-AND operator (in Visual Basic for and, & in Visual C #) for the value of the ModifierKeys property and Keys enumeration to determine which key combination is pressed. (ModifierKeys is a shared member of the Control class; For more information about shared members, see shared members.) )
' Visual Basic
Private Sub button1_keypress (ByVal sender as Object, ByVal e As _
System.Windows.Forms.KeyPressEventArgs) Handles button1. KeyPress
If (Control.modifierkeys and keys.shift) = Keys.shift Then
MessageBox.Show ("Pressed" & Keys.shift)
End If
End Sub
Http://www.cnblogs.com/hfzsjz/archive/2010/05/31/1748046.html
C#
private void Button1_keypress (object sender, System.Windows.Forms.KeyPressEventArgs e) {
if ((Control.modifierkeys & keys.shift) = = Keys.shift) {
MessageBox.Show ("Pressed" + keys.shift);
}
}
--------------------------------------------------------------------------------------------------------------- ---------
First, the bool variable is used to save the CTRL key is pressed, the initial value is False,bool assignment in the KeyDown event, to determine whether the CTRL key is pressed, if pressed, the bool variable is true, otherwise false. The bool variable is set to False in the KeyUp event
Second, the value of the bool variable is judged in the MouseDown event.
--------------------------------------------------------------------------------------------------------------- ---------
if ((Control.modifierkeys & keys.control) = = Keys.control)
{
MessageBox.Show ("Ctrl-key is pressed");
}
c#-determine if the Shift,alt,ctrl is pressed, and the key combination is pressed