Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyS And Shift = 2 Then
MsgBox "Ctrl and S Was pressed"
End If
End Sub
The Shift variable explained: An integer that corresponds to the state of the SHIFT, CTRL, and ALT keys at the time of the event. The shift argument is a bit field with the least-significant bits corresponding to the SHIFT key (bit 0), the CTRL key (bit 1), and the ALT key (bit 2 ). These bits correspond to the values 1, 2, and 4, respectively. Some, all, or none of the bits can be set, indicating that some, all, or none of the keys are pressed. For example, if both CTRL and ALT are pressed, the value of shift is 6.
[blue]Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyS And Shift = [b]vbCtrlMask[/b] Then
MsgBox "Ctrl and S Was pressed"
End If
End Sub[/blue]
since this gets rid of the magic number, and make the code more readable. vbAltMask and vbShiftMask represent the other possibilities.
Using your example of checking for CTRL and ALT, I'd argue that
[tt]If Shift = vbAltMask + vbShiftMask ...[/tt]
(or, more properly, [tt]If Shift = vbAltMask Or vbShiftMask[/tt])
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.