Trap Alt+S or Alt+z

K

Kon

I wonder what's the mistake on that code to trap ALT+S or Alt+z and only the
Enter code works

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = 2 And KeyCode = vbKeyS Then GoTo opened
If KeyCode = 13 Then GoTo opened
If Shift = 2 And KeyCode = vbKeyZ Then GoTo opened
End Sub
 
D

Dirk Goldgar

Kon said:
I wonder what's the mistake on that code to trap ALT+S or Alt+z and
only the Enter code works

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = 2 And KeyCode = vbKeyS Then GoTo opened
If KeyCode = 13 Then GoTo opened
If Shift = 2 And KeyCode = vbKeyZ Then GoTo opened
End Sub

The "shift mask" value for the Alt key is 4, not 2, so at the least your
code should be using either

If Shift = 4

or

If Shift = acAltMask

Does it matter to you whether the user is also holding down the Shift
key? Your question used mixed case -- "ALT+S" in one example, "Alt+z"
in the other, and it makes a difference. To test whether the Alt key is
down while these keys are pressed, regardless of whether the Shift key
is also down, do it like this:

Dim intAltDown As Integer

intAltDown = (Shift And acAltMask) <> 0

If (KeyCode = vbKeyS) And intAltDown Then GoTo opened
If KeyCode = 13 Then GoTo opened
If (KeyCode = vbKeyZ) And intAltDown Then GoTo opened

On the other hand, if you do care whether the {Shift} key is down or
not, you have to check the Shift value for acShiftMask as well, and take
that into account.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top