Keydown procedure?


New member
Jan 18, 2005
Programming Experience
How can you set the keydown procedure in
I need snggrav to be equal to -30 when up is pressed. am i on the right track? this is all the code i have pretaining to it
Private Sub Form1_Keydown(ByVal keycode As Integer, ByVal shift As Integer)

Select Case Keys.KeyCode

Case Keys.Up

snggrav = -30

End select

End sub
The code you have looks like it could be VB6.
In VB.NET the code would look more like this:
Private Sub Form1_KeyUp(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.KeyEventArgs) _
  Handles MyBase.KeyUp
    If e.KeyCode = Keys.Up Then
        snggrav = -30
    End If
End Sub
To have the IDE create the signature of the procedure for you: in the code window look for the two dropdown lists at the top. In the left one, select '(Base Class Events)' for events pertaining to the class you are in (you can also select other controls as well). In the right dropdown, select the event you want to hadle and the IDE will either create the procedure outline if it doesn't exist or place the cursor in that procedure.
Here is code for an 'F3' keypress on a grid...

Private Sub Grid1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Grid1.KeyDown
' Check if F3 Key is pressed.
If e.KeyCode = Keys.F3 Then
tCurrentUnit = Grid1.Columns("Units").Value
Catch ex As Exception
End Try
End If
End Sub