none
How to detect arrow keys in vb.net? RRS feed

  • Question

  • Hi,

    How do we detect arrow keys(up,down,left,right) in a vb.net application? Also, what are the code values for these keys?

    The application consists of a form that has an image control on it. When the user presses the right or left arrow keys, the image is supposed to move to the right or left.
    Monday, April 17, 2006 6:39 AM

Answers

  • The appropriate event to use is either keyup, or keydown. You can detect the keys either at the form level, or at the image-control level. If you wish to process at the form-level (before the image control gets the event), then set the form's KeyPreview property to true.

    Within the keyup/keydown event handler, you can read the keycode from the KeyEventArgs e parameter in your event handler, and use a case statement. Here's an example:

    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        ' Sets Handled to true to prevent other controls from 
        ' receiving the key if an arrow key was pressed
        Dim bHandled As Boolean = False
        Select Case e.KeyCode
            Case Keys.Right
                'do stuff
                e.Handled = True
            Case Keys.Left
                'do other stuff
                e.Handled = True
            Case Keys.Up
                'do more stuff
                e.Handled = True
            Case Keys.Down
                'do more stuff
                e.Handled = True
        End Select
    End Sub
     

    Monday, April 17, 2006 7:19 AM

All replies

  • The appropriate event to use is either keyup, or keydown. You can detect the keys either at the form level, or at the image-control level. If you wish to process at the form-level (before the image control gets the event), then set the form's KeyPreview property to true.

    Within the keyup/keydown event handler, you can read the keycode from the KeyEventArgs e parameter in your event handler, and use a case statement. Here's an example:

    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        ' Sets Handled to true to prevent other controls from 
        ' receiving the key if an arrow key was pressed
        Dim bHandled As Boolean = False
        Select Case e.KeyCode
            Case Keys.Right
                'do stuff
                e.Handled = True
            Case Keys.Left
                'do other stuff
                e.Handled = True
            Case Keys.Up
                'do more stuff
                e.Handled = True
            Case Keys.Down
                'do more stuff
                e.Handled = True
        End Select
    End Sub
     

    Monday, April 17, 2006 7:19 AM
  • This does not work. .

    But this does :

     Protected Overrides Function ProcessCmdKey(ByRef msg As Message, _
      ByVal keyData As Keys) As Boolean



     Select Case keyData
    Case Keys.Up
    'do whatever....up arrow pressed
    bHandled = True
    Case Keys.Down
    'do whatever....down arrwo pressed
    bHandled = True

    End Select
    Return bHandled
    End Function
    • Proposed as answer by Chivalry4 Thursday, October 27, 2011 2:18 PM
    Monday, February 4, 2008 3:04 PM
  •  

    Hi,

     

    For the code in the 1st post to work the

     

    KeyPreview

     

    property needs to be set to TRUE as mentioned in the text. You can also set it in DESIGN mode.>

     

    Option Strict On

     

    Public Class Form1

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

    Me.KeyPreview = True

     

    End Sub

     

     

    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

    ' Sets Handled to true to prevent other controls from

    ' receiving the key if an arrow key was pressed

    Dim bHandled As Boolean = False

    Select Case e.KeyCode

    Case Keys.Right

    Me.Text = "Right"

    e.Handled = True

    Case Keys.Left

    Me.Text = "Left"

    e.Handled = True

    Case Keys.Up

    Me.Text = "Up"

    e.Handled = True

    Case Keys.Down

    Me.Text = "Down"

    e.Handled = True

    End Select

    End Sub

    End Class

     

     

     

     

    Regards,

     

    John

     

    Monday, September 8, 2008 3:50 PM
  • Brilliant solution. Works great. Just remember to declare bHandled as a Boolean.

     

    Sunday, May 9, 2010 4:56 PM
  • I had a problem using the keyDown event when I had button controls present but Steeves Saillant's solution worked a treat!

     

    Thursday, October 27, 2011 2:19 PM
  • Hi,
    I have a form and inside I have two buttons. When I press left or right arrow it does not work.

    Ciprian LUPU

    Monday, April 16, 2012 12:15 PM
  • Hi

    I have a form with several button controls and I can't get this to work.

    All other keys work, but not the arrow keys that I need! I need to detect KeyDown and KeyUp to start and stop an action.

    I am using VS2010 and VB.Net 4.0


    • Edited by Phlazar Friday, May 4, 2012 8:10 AM
    Friday, May 4, 2012 8:09 AM
  • In order to capture keystrokes in a Forms control, you must derive a new class that is based on the class of the control that you want, and you override the ProcessCmdKey().

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
          'handle your keys here
    End Function

    Full Source...VB.Net Arrow Key Press

    Bonave


    • Edited by bonave2008 Saturday, August 9, 2014 3:08 PM NA
    Saturday, August 9, 2014 3:07 PM
  • First question:  I don't understand why you declare bHandled, but then it's not used.  Is it necessary?

    Second question: I can use your code just fine within the KeyDown event.  However, when I put a control, such as a button on my form, then the code doesn't work unless I use the Control Key in conjunction with the Arrow Keys.  Can you offer any advice on how to make it so that I can use just the Arrow Keys with the Control Key?

    Thanks.

    • Proposed as answer by Padwan Johnson Thursday, June 11, 2015 1:50 AM
    • Unproposed as answer by Padwan Johnson Thursday, June 11, 2015 1:51 AM
    Thursday, June 11, 2015 1:33 AM
  • This link seems to answer my 2nd question:  http://net-informations.com/q/faq/arrowkeys.html 
    Thursday, June 11, 2015 1:51 AM