Results 1 to 8 of 8
  1. #1
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Hong Kong
    Posts
    359
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How to capture the keystroke which initiates a KeyPress event?
    How could we find out if Esc or Ctrl+I has been pressed?
    Armstrong

  2. #2
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts
    [quote name='armsys' post='780738' date='20-Jun-2009 15:28']How to capture the keystroke which initiates a KeyPress event?
    How could we find out if Esc or Ctrl+I has been pressed?
    Armstrong[/quote]
    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.
    Regards
    John



  3. #3
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Hong Kong
    Posts
    359
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='johnhutchison' post='780740' date='20-Jun-2009 13:13']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.[/quote]

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

  4. #4
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts
    [quote name='armsys' post='780743' date='20-Jun-2009 16:34']Is KeyCode property available in Macro?[/quote]
    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.
    Regards
    John



  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    [quote name='armsys' post='780738' date='20-Jun-2009 13:28']How to capture the keystroke which initiates a KeyPress event?
    How could we find out if Esc or Ctrl+I has been pressed?
    Armstrong[/quote]
    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
    [MS MVP - Word]

  6. #6
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Hong Kong
    Posts
    359
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='macropod' post='780746' date='20-Jun-2009 13:48']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
    [/quote]
    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

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    [quote name='armsys' post='780751' date='20-Jun-2009 08:34']How could my macro capture the keystroke the user just pressed?[/quote]
    Why do you keep on asking? You can't use a macro for that; you need VBA to capture keystrokes.

  8. #8
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Hong Kong
    Posts
    359
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='780754' date='20-Jun-2009 17:36']Why do you keep on asking? You can't use a macro for that; you need VBA to capture keystrokes.[/quote]
    Hi HansV,
    I'm sorry. I didn't know I kept asking. Now I realize. Thanks for your reminder.
    Armstrong

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •