# Detecting when "enter" is clicked in VB6



## cj_white

I am working on my final for my programming class. I have a login screen and I want to be able to hit enter to accept the user and pass instead of having to hit the command button. Also where would I have to put the code? Would it be for the text box or for the form? Thanks in advance


----------



## MustBNuts

Here is some food for thought, courtesy of MSDN Library, good luck.

The KeyPress Event

The KeyPress event occurs when any key that corresponds to an ASCII character is pressed. The ASCII character set represents not only the letters, digits, and punctuation on a standard keyboard but also most of the control keys. The KeyPress event only recognizes the ENTER, TAB, and BACKSPACE keys, however. The other function, editing, and navigation keys can be detected by the KeyDown and KeyUp events.

Use the KeyPress event whenever you want to process the standard ASCII characters. For example, if you want to force all the characters in a text box to be uppercase, you can use this event to change the case of the keys as they are typed:

Private Sub Text1_KeyPress (KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub

The keyascii argument returns an integer value corresponding to an ASCII character code. The procedure above uses Chr to convert the ASCII character code into the corresponding character, UCase to make the character uppercase, and Asc to turn the result back into a character code.

Using the same ASCII character codes, you can test whether a key recognized by the KeyPress event is pressed. For instance, the following event procedure uses KeyPress to detect if the user is pressing the BACKSPACE key:

Private Sub Text1_KeyPress (KeyAscii As Integer)
If KeyAscii = 8 Then MsgBox "You pressed the _
BACKSPACE key."
End Sub

You can also use the Visual Basic key-code constants in place of the character codes. The BACKSPACE key in the example above has an ASCII value of 8. The constant value for the BACKSPACE key is vbKeyBack.

For More Information For a complete list of character codes, see "Character Set (0127)" and "Character Set (128255)" in the Language Reference. A complete list of key code constants with corresponding ASCII values is available in "Key Code Constants" or by using the Object Browser and searching for KeyCodeConstants.

You can also use the KeyPress event to alter the default behavior of certain keys. For example, pressing ENTER when there is no Default button on the form causes a beep. You can avoid this beep by intercepting the ENTER key (character code 13) in the KeyPress event.

Private Sub Text1_KeyPress (KeyAscii As Integer)
If KeyAscii = 13 Then KeyAscii = 0
End Sub​
Just remember, you have to account for every situation in which a user might press Enter, not just the obvious one. Start by coding one, and when that works, add another trap.

Have fun.

MBN


----------



## GUSMAN

Hi 
Im not an expert at VB, and not sure if this is what you want. but my tutor just advised me on a similar problem. I have a screen where you enter an account number, cash amount then press enter. then when you finish, on the same form you have a user, pass, and enter.
I was doing the same thing clicking the command button with the mouse, so he advised me to, do this.
when I open the form the account number label has focus (cursor is in the box) In the design mode (I think you call it that) I go to the properties of the account number and there's a "Tab Index" lets say the Tab is 10. I then set the Amount label to 12, and the enter button to 14. Then the username to 16, pass to 18 and "enter Password" button to 20.
So its like this. I start the form , enter the account number, "tab" to the amount label and enter the amount, "tab" to the enter button (or command button) and I can press the enter button on my keybard to go to the next form. When I go back to form 1, the cursor is back in the account number label. I press "tab" 4 times until it is in the username label, enter username "tab" to put in password,enter password "tab" to highlight the enter button and press enter on the keyboard to go to the relevant form.
No mouse needed at all.

Hope this helps

Cheers

Gus

PS just play around with it.


----------



## MyName

Probably not what you really wanted, but you could just make a command button named Enter and then on your Enter button, in the properties box change the "Default" value to "True"


----------



## GUSMAN

I have 2 enter buttons on my form so this way suits me better


----------



## cj_white

This is what I ended up doing.

'Detect to see if enter button is hit after the password field has become active
Private Sub txtPass_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
'If the enter button has been hit call Check_credentials function to verify login
Call Check_Credentials(txtUser.Text, txtPass.Text)
End If
End Sub

It is almost exactly what MustBNuts posted from the MSDN site (I never thought to look there). I had trouble with it at first but it now works. Thanks for all the posts guys


----------

