On key press not working for everyone

tmyers

Well-known member
Local time
Today, 01:31
Joined
Sep 8, 2020
Messages
1,091
I have a couple different keypress events throughout my application but it has come to my attention that they don't work for everyone and am curious as to why. Most are very, very simple and are typically me just setting enter key behavior to call buttons and such.

Here is a snip of one:
Code:
If KeyAscii = 13 Then
    Call LoginBtn_Click
End If

When I hit either enter key on my computer, it works just fine but for others hitting either enter key does nothing. Is there some global property I need to set for it to work for everyone consistently?
 
As well as setting KeyPreview = Yes, change the event to KeyDown as KeyPress doesn't work properly with the Enter key

A KeyPress event can occur when any of the following keys are pressed:
  • Any printable keyboard character.
  • CTRL combined with a character from the standard alphabet.
  • CTRL combined with any special character.
  • BACKSPACE.
  • ESC.

A KeyPress event does not occur under the following conditions:
  • Pressing TAB.
  • Pressing ENTER.
  • Pressing an arrow key.
  • When a keystroke causes the focus to move from one control to another.

This should work:
Code:
Private Sub YourControlName_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Then Call LoginBtn_Click
End Sub
 
Ah that makes sense that the enter key would have slightly different behavior then other keys. I changed to the keydown event and changed the code to your suggestion and will report back if it still causes problems.

Thanks to both of you!
 

Users who are viewing this thread

Back
Top Bottom