H o m e

 

C a t c h   M o d i f i e r   K e y s

By Michael McIntyre

mikemc@getdotnetcode.com

You can monitor key strokes to catch the SHIFT, CTRL, or ALT modifier keys. When a modifier key is pressed in combination with other keys the information is provided in the System.Windows.Forms.KeyEventArgs passed to a KeyDown event handler method.

Examples:

' Catch SHIFT + A key strokes.

If e.KeyCode = Keys.A And (e.Shift And Not e.Control And Not e.Alt) Then

   MessageBox.Show("Pressed: SHIFT + A")

End If

' Catch CONTROL + A key strokes.

If e.KeyCode = Keys.A And (Not e.Shift And e.Control And Not e.Alt) Then

   MessageBox.Show("Pressed: CONTROL + A")

End If

' Catch ALT + A key strokes.

If e.KeyCode = Keys.A And (Not e.Shift And Not e.Control And e.Alt) Then

   MessageBox.Show("Pressed: ALT + A")

End If

' Catch SHIFT + CONTROL + A key strokes.

If e.KeyCode = Keys.A And (e.Shift And e.Control And Not e.Alt) Then

   MessageBox.Show("Pressed: SHIFT + CONTROL + A")

End If

' Catch SHIFT + CONTROL + ALT + A key strokes.

If e.KeyCode = Keys.A And (e.Shift And e.Control And e.Alt) Then

   MessageBox.Show("Pressed: SHIFT + CONTROL + ALT + A")

End If

 

To Test:  Use Visual Studio.Net to create a VB.NET Win Forms application.  Add a TextBox control to Form1.  Add a KeyDown handler for the TextBox.  Add the sample code to the KeyDown handler.  Start the application.

 

In the TextBox try these key strokes.

 

1.  Hold down the SHIFT key and type:  A

2.  Hold down the CONTROL key and type: A

3.  Hold down the ALT key and type: A

4.  Hold down the SHIFT and CONTROL keys and type: A

5.  Hold down the SHIFT and CONTROL and ALT keys and type: A

 

 

Copyright © 2001-2003 aZ Software Developers. All rights reserved.