Created: March 1, 1995
In a Visual Basic® application, you may want to disable the shift+printscrn keystroke combination. This can be accomplished by trapping the key in a control or form's KeyDown event.
You can trap any key on the keyboard in any form or control. The KeyPreview property of a form, when set to TRUE, forces all keystrokes to the form, not to the control. In this way, the form can be the first object to capture the incoming keystrokes. When the KeyPreview property of a form is set to FALSE, each control must be programmed individually to trap the keystrokes.
To determine when a specific key has been pressed or released on the keyboard, use the KeyDown and KeyUp events, respectively. Both events will be triggered by a keypress or keyrelease for the control that has the current focus. If a form does not have any controls on it, or if the form's KeyPreview property is set to True, the KeyDown and KeyUp events will trap the keystroke at the form level.
The KeyDown and KeyUp events return two variables: the keycode and the shift key status. The keycode is a unique number that is assigned to each key on the keyboard. The CONSTANT.TXT file contains a list of all keycodes supported by Visual Basic®. The shift variable tells you which shift key (shift, alt, or ctrl) was pressed.
In a Visual Basic program, you can determine if shift+printscrn was pressed by executing this statement in the KeyDown event:
If KeyDown = 16 And Shift=1 Then ....
The following program displays a Text Box on the screen. When you press shift+printscrn, the program displays a message to that effect in the text box. If you press the shift+f2 function key combination, the message tells you that that keystroke was pressed.
Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 16 And Shift = 1 Then
Text1.Text = "Shift-PrintScreen pressed"
End If
If KeyCode = 113 And Shift = 1 Then
Text1.Text = "Shift-F2 pressed"
End If
End Sub