• Capturing Keystroke in Key Press Event

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Capturing Keystroke in Key Press Event

    Author
    Topic
    #460643

    How to capture the keystroke which initiates a KeyPress event?
    How could we find out if Esc or Ctrl+I has been pressed?
    Armstrong

    Viewing 1 reply thread
    Author
    Replies
    • #1165564

      How to capture the keystroke which initiates a KeyPress event?
      How could we find out if Esc or Ctrl+I has been pressed?
      Armstrong

      To test what key has been pressed you actually need to use the Key Down event.

      Code:
      Private Sub infantID_KeyDown(KeyCode As Integer, Shift As Integer)
      	If KeyCode = vbKeyEscape Then
      		MsgBox "Escape Key"
      	ElseIf (KeyCode = vbKeyI) And (Shift And acCtrlMask) > 0 Then
      		MsgBox "Ctrl I"
      	End If
      End Sub

      Search in VBA help for more on this event. Search for Keycodes and it lists all the vb constants that represent the various keys.

      • #1165567

        To test what key has been pressed you actually need to use the Key Down event.

        Code:
        Private Sub infantID_KeyDown(KeyCode As Integer, Shift As Integer)
        	 If KeyCode = vbKeyEscape Then
        		 MsgBox "Escape Key"
        	 ElseIf (KeyCode = vbKeyI) And (Shift And acCtrlMask) > 0 Then
        		 MsgBox "Ctrl I"
        	 End If
         End Sub

        Search in VBA help for more on this event. Search for Keycodes and it lists all the vb constants that represent the various keys.

        Hi John,
        Thanks for your help. Is KeyCode property available in Macro?
        Armstrong

        • #1165570

          Is KeyCode property available in Macro?

          Not that I know of, but I have not written macros for many years now, so I am very rusty on what they can and cannot do.

    • #1165571

      How to capture the keystroke which initiates a KeyPress event?
      How could we find out if Esc or Ctrl+I has been pressed?
      Armstrong

      Hi Armstrong,

      For the values of any kestroke combination, try something along the lines of:

      Code:
      Private Sub TextBox1_KeyUp(ByVal KeyAscii As MSForms.ReturnInteger, ByVal Shift As Integer)
      TextBox1.Text = KeyAscii & "," & Shift
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1165576

        Hi Armstrong,

        For the values of any kestroke combination, try something along the lines of:

        Code:
        Private Sub TextBox1_KeyUp(ByVal KeyAscii As MSForms.ReturnInteger, ByVal Shift As Integer)
         TextBox1.Text = KeyAscii & "," & Shift
         End Sub

        Hi Paul,
        I’m sorry. I should have been more explicit about the purpose.
        After a user presses Esc, my macro should cancel the Add Event.
        How could my macro capture the keystroke the user just pressed?
        Armstrong

        • #1165579

          How could my macro capture the keystroke the user just pressed?

          Why do you keep on asking? You can’t use a macro for that; you need VBA to capture keystrokes.

          • #1165581

            Why do you keep on asking? You can’t use a macro for that; you need VBA to capture keystrokes.

            Hi HansV,
            I’m sorry. I didn’t know I kept asking. Now I realize. Thanks for your reminder.
            Armstrong

    Viewing 1 reply thread
    Reply To: Capturing Keystroke in Key Press Event

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: