If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

VB6 - Using the KeyDown Event

I have seen a lot of people asking about using the Form_KeyDown event in the VisBasic 6 forum. I know a lot of you know how to use this, but many do not, I am putting this in for future reference, so when someone asks a question about this, this thread can be sent. So here is the code to run code when you press a key on the keyboard.

just i what it so that when i press left and space, it doesnt stop left.... looked at the code from the attachment by jacob hehe no idea what was happening there :d can anyone help me i need something small but effective and simple

Re: VB6 - Using the KeyDown Event

Re: VB6 - Using the KeyDown Event

Hi,
I have a form with 3 groups of command buttons, each with 2 rows of buttons and each grouped into a separate picture box. I'm trying to get my prog. to be able to utilize the up and down arrow keys to make navigation easier. I have coded keyup and keydown events on the command button object. When I run the code the up and down buttons just go left and right through the command buttons on the form. I have set the keypreview property to true on my form. Any ideas? Please see code below:Private Sub Command1_KeyDown(index As Integer, KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDown Then
Command1(index + 16).SetFocus
End If
End Sub

Private Sub Command1_KeyUp(index As Integer, KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then
Command1(index - 16).SetFocus
End If
End Sub

Re: VB6 - Using the KeyDown Event

I actually have 32 buttons (16 on each line) in one group, 26 in another and 16 in yet another. I got your code to work fine with the number pad buttons, but it won't work for the arrow keys. Weird! Do you know if this is a bug with VB? DO you know any other way to get this to work with the arrow keys?

Re: VB6 - Using the KeyDown Event

Re: VB6 - Using the KeyDown Event

Still no luck. The up and down arrow keys still just go left and right. I got the vbkeynumpad2 and vbkeynumpad8 to work just fine. If I turn numlock off though it gets treated like the arrow keys and only goes left and right. Any suggestions?????

Re: VB6 - Using the KeyDown Event

What if you want it to detect two simultaneous key presses? such as CTRL and F? Thanks!

VB Code:

PrivateSub Form_KeyDown(KeyCode AsInteger, Shift AsInteger)

If(KeyCode = vbKeyF)And(Shift = vbCtrlMask)Then ...

EndSub

Shift = vbShiftMask
Ctrl = vbCtrlMask
Alt = vbAltMask

The most difficult part of developing a program is understanding the problem.The second most difficult part is deciding how you're going to solve the problem.Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.Please Help Us To Save Ana

Re: VB6 - Using the KeyDown Event

Public Function CurLeft() As Boolean
CurLeft = CBool(GetAsyncKeyState(37))
End Function
Public Function CurRight() As Boolean
CurRight = CBool(GetAsyncKeyState(39))
End Function
Public Function CurUp() As Boolean
CurUp = CBool(GetAsyncKeyState(38))
End Function
Public Function CurDown() As Boolean
CurDown = CBool(GetAsyncKeyState(40))
End Function

Private Sub Form_Load()
X = 0
End Sub

Private Sub Timer1_Timer()
If (CurUp Or CurDown Or CurLeft Or CurRight) Then
If CurUp = True Then
Label1.Caption = "UP"
End If
If CurDown = True Then
Label1.Caption = "DOWN"
End If
If CurDown = True and CurUp Then
Label1.Caption = "DOWN and UP"
End If

Re: VB6 - Using the KeyDown Event

Hey i am trying to use the keydown control to move an image across the form but when the form loads it has a command button selected and the image will not move. I have little experience with the set focus control and do not understand how to use it. Any help would be great.

is it possible to have this event and make two buttons pressed at once. Currently if two are pressed neither works, either one button can be pressed or both have to be held down at the exact same time.