Key shortcuts in VB

Use the KeyPress Event - Put this code behind the textbox (or other item that the user will be pressing the enter key in). Replace Text1 with the name of the text box that the user will be pressing Enter in. Replace Command1 with the name of the Command Button whose click action you wish to call:

Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        Command1_Click
    End If
End Sub
 
Use the KeyPress Event - Put this code behind the textbox (or other item that the user will be pressing the enter key in). Replace Text1 with the name of the text box that the user will be pressing Enter in. Replace Command1 with the name of the Command Button whose click action you wish to call:

Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        Command1_Click
    End If
End Sub

I understand how to do this... Just what if I want to use, oh say the "Del" button as my program's clear button? And why KeyAscII = 13? I gotta understand this stuff, I am trying to learn some VB. The "For Dummies" book I bought is about as useless as a DVD-Rewinder.


EDIT: I am getting an error on this...


Thanks
 
Last edited:
I understand how to do this... Just what if I want to use, oh say the "Del" button as my program's clear button? And why KeyAscII = 13? I gotta understand this stuff, I am trying to learn some VB.
The KeyAscii=xx is the ASCII value of that key. 13 is the ASCII value for Carriage Return (Enter). If you wanted to use the escape key to do the same thing, you'd just replace that line with KeyAscii=27, if you wanted to use the backspace key it's KeyAscii=8. Take a look at an ASCII Table for all of them: http://www.asciitable.com/. Basically, all you're saying is that if a key with a particular ASCII value is pressed, call a certain Sub.

If you want to have, say, the Enter button emulate on command button, and the backspace button emulate another, the code would look something like this:

Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        Command1_Click
    ElseIf KeyAscii = 8 Then
        Command2_Click
    End If
End Sub

EDIT: I am getting an error on this...
Are you using VB6? What's the error? Make sure that all the buttons are correctly named - I just tried it then and it worked OK for me.

The "For Dummies" book I bought is about as useless as a DVD-Rewinder.
LOL
 
Last edited:
The KeyAscii=xx is the ASCII value of that key. 13 is the ASCII value for Carriage Return (Enter). If you wanted to use the escape key to do the same thing, you'd just replace that line with KeyAscii=27, if you wanted to use the backspace key it's KeyAscii=8. Take a look at an ASCII Table for all of them: http://www.asciitable.com/. Basically, all you're saying is that if a key with a particular ASCII value is pressed, call a certain Sub.

If you want to have, say, the Enter button emulate on command button, and the backspace button emulate another, the code would look something like this:

Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        Command1_Click
    ElseIf KeyAscii = 8 Then
        Command2_Click
    End If
End Sub


Are you using VB6? What's the error? Make sure that all the buttons are correctly named - I just tried it then and it worked OK for me.


LOL

I need it for the whole form... Just the Enter key to have the same function as "Button1". I will try again... Why would I use that ancient crap ? Pfft....Visual Basic 6 or anything older....... VB 2005 EE.


Thx
 


I don't see why this is occurring, it's pretty damn clear in my mind how this would work, but then again WTF!?


Thanks
 
Last edited:
Maybe its been to long almost a year since i did a vb code but i thought it was the Button1.Onclick() maybe i'm wrong i dont remember i have had to many languages all mixxed togethor in my brain...
 
It doesn't work, the code is correct but, after I get done typing in my info into my textbox, and have the cursor in TextBox4, I then press "Enter" but to no effect...

WTF!?


Thanks

Edit: Got it to work finally, using only the code that Cromewell gave me... Anyway, I have that damn Windows Chime in the background, that is annoying, how do I get rid of it?


Thanks Again
 
Last edited:
Back
Top