VBA/Excel/Access/Word/Access/Key

Материал из VB Эксперт
Перейти к: навигация, поиск

Arrow key, page up, page down key

   <source lang="vb">

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

  If ImportantKey(KeyCode, Shift) Then
      msgBox "Important key pressed"
  End If

End Sub Function ImportantKey(KeyCode, Shift)

   ImportantKey = False
   "If Alt key was pressed, exit function
   If Shift = acAltMask Then
       Exit Function
   End If
   "If Delete, Backspace, or a typeable character was pressed
   If KeyCode = vbKeyDelete Or KeyCode = vbKeyBack Or (KeyCode > 31 _
       And KeyCode < 256) Then
       "If the typeable character was NOT a right, left, up,
       "or down arrow, page up, or page down, return True
       If KeyCode = vbKeyRight Or KeyCode = vbKeyLeft Or _
           KeyCode = vbKeyUp Or KeyCode = vbKeyDown Or _
           KeyCode = vbKeyPageUp Or KeyCode = vbKeyPageDown Then
       Else
           ImportantKey = True
       End If
   End If

End Function

</source>
   
  


Check the keycode

   <source lang="vb">

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

       If KeyCode = vbKeyPageDown Or _
        KeyCode = vbKeyPageUp Then
        KeyCode = 0
       End If

End Sub

</source>
   
  


Shift key, alt key and control key mask

   <source lang="vb">

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

   Select Case Shift 
       Case acShiftMask 
           MsgBox "You pressed the SHIFT key." 
       Case acCtrlMask 
           MsgBox "You pressed the CTRL key." 
       Case acAltMask 
           MsgBox "You pressed the ALT key." 
   End Select 

End Sub

</source>
   
  


Writing the Form_KeyDown Event Procedure

   <source lang="vb">

Private Sub Form_KeyDown (KeyCode As Integer, Shift As Integer)

   Select Case KeyCode 
       Case vbKeyF1 
           MsgBox "You pressed the F1 key." 
       Case vbKeyHome 
           MsgBox "You pressed the Home key." 
       Case vbKeyTab 
           MsgBox "You pressed the Tab key." 
   End Select 

End Sub

</source>
   
  


Writing the Form_KeyPress Event Procedure

   <source lang="vb">

Private Sub Form_KeyPress(KeyAscii As Integer)

   Debug.Print "KeyAscii = " & KeyAscii & Space(1) & "= " & Chr(KeyAscii) 
   If KeyAscii = 27 Then 
       DoCmd.Close 
   Else 
       KeyAscii = 0 
   End If 

End Sub

</source>
   
  


Writing the Form_KeyUp Event Procedure

   <source lang="vb">

Private Sub Form_KeyUp(KeyCode As Integer,Shift As Integer)

   Debug.Print "You released " & KeyCode & Space(1) & Chr(KeyCode) 

End Sub

</source>