c#-determine if the Shift,alt,ctrl is pressed, and the key combination is pressed

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.