Detecting arrow keys

  • Thread starter Thread starter Nathan
  • Start date Start date
Hi Nathan,

You can trap them in the keydown event:
Dim xstring As String

xstring = e.KeyCode()

MessageBox.Show(xstring)

The left arrow is 37, right 39, down 40, up 38.

HTH,

Bernie Yaeger
 
don't forget to turn keypreview on in the form, if you don't you wont trap
much ;p

hope it helps

eric
 
In addition to the others, here is a more elegent snipit.

'A textbox example.
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

Select Case e.KeyCode

Case Keys.Down ' << Note this useful enumeration
MsgBox("DOWN")
Case Keys.Up
MsgBox("UP")
Case Keys.Left
MsgBox("LEFT")
Case Keys.Right
MsgBox("RIGHT")
End Select

End Sub

Regards - OHM
 
Thanks for the info guys.

Nathan


Bernie Yaeger said:
Hi Nathan,

You can trap them in the keydown event:
Dim xstring As String

xstring = e.KeyCode()

MessageBox.Show(xstring)

The left arrow is 37, right 39, down 40, up 38.

HTH,

Bernie Yaeger
 
* "One Handed Man said:
In addition to the others, here is a more elegent snipit.

'A textbox example.
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

Select Case e.KeyCode

Case Keys.Down ' << Note this useful enumeration
MsgBox("DOWN")
Case Keys.Up
MsgBox("UP")
Case Keys.Left
MsgBox("LEFT")
Case Keys.Right
MsgBox("RIGHT")
End Select

End Sub

This will not work, if there are, for example, buttons on the form --
even if 'KeyPreview' is set to 'True'.
 
Back
Top