VB6 using keydown

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
Hi, I'm creating a sample program in VB6. It is consist only of one textbox and one command button. If I'll press the command button it will output "f" on the textbox as stated on my code below. Can you teach me how to make the command button be "pressed" without using images or pictures using only keyboard keys. For example using the cursor key UP, when I press it, the command button will be "pressed" (it should look like it is being pressed also) and when I release the cursor key Up again then the command button will be "unpressed". I hope you understand what I mean. Here's my first code:

Rich (BB code):
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
On Error Resume Next
Select Case KeyCode
Case vbKeyUp 
    text1.text = "f"   'output "f" on textbox
End Select
End Sub

Private Sub Command1_Click()
text1.text = "f"
End Sub
Thanks in advance.
 
Last edited:

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
do you want the same button to appear depressed and hold that position till another event triggers? Sorry didn't get you.
Thanks for the reply edgetrigger,
Since I have only one textbox and one command button in the program, yes. I want the only one command button to be pressed and unpressed when I'm pressing the vbkey.
 

jOmega

Joined Mar 22, 2011
1
Lloyd,

To make it work, there are a couple of considerations.
1 - Form1 Properties : KeyPreview needs to be set to TRUE. (default is FALSE) This enables the Up Arrow key being pressed to be detected. When set to FALSE, the arrow keys will move things but you will not be able to detect their being pressed.

You can either set that property to TRUE in the design environment or, you can make the change in the Form Load procedure as follows:

Private Sub Form_Load()
Form1.KeyPreview = True
End Sub

With that accomplished, now your code will place the letter f in the Text Box when the Up Arrow key is depressed.

2. Next, you said that you wanted the Command1 button to appear to be depressed when the Up Arrow key is depressed.... and to remain depressed until the Up Arrow key is released. This is accomplished by changing the Command1 Default property from False to True and then back to False. The code below accomplishes that.

Private Sub Form_Load()

Form1.KeyPreview = True

End Sub
---------------------------------------------------------------
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

On Error Resume Next

Select Case KeyCode
Case vbKeyUp
End Select

Command1.Default = True

Text1.Text = "f"

End Sub
--------------------------------------------------------------
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)

On Error Resume Next

Select Case KeyCode
Case vbKeyUp
End Select

Command1.Default = False

End Sub
-------------------------------------------------------------
Private Sub Command1_Click()

Text1.Text = "f"

End Sub
 
Top