INTELCRAZY
banned
I have a button in my program that I want the "Enter" button on my keyboard to emulate. How can I declare this?
Thanks
Thanks
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
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.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.
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.EDIT: I am getting an error on this...
LOLThe "For Dummies" book I bought is about as useless as a DVD-Rewinder.
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