Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I link arrow keys to code in VB.NET?

    Hello there

    I'm trying to write code that responds to the user manipulating two of the arrow keys (you know, the four keys just under the delete, end and page down keys). The following code compiles okay but it doesn't do anything. I just wondered how I should modify it. All contributions gratefully accepted.

    meandmymachine

    Heres the code:

    Dim vbKeyRight As Integer
    Dim vbKeyLeft As Integer

    Private Sub KeyPress1_KeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
    If KeyCode = vbKeyRight Then
    MsgBox("Right Key Pressed!")
    End If
    End Sub

    Private Sub KeyPress2_KeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
    If KeyCode = vbKeyLeft Then
    MsgBox("Left Key Pressed!")
    End If
    End Sub

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    You shouldn't have to declared vbKeyRight and vbKeyLeft if they are constants... How about changing it to:

    Private Sub KeyPress1_KeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
    MsgBox(KeyCode)
    End Sub

    Then press the arrow keys and see what you get, that should give you the keycode for it.

    If that doesn't work you could use DirectInput. You'd have to if you want to be able to process multiple keys at the same time.
    OracleGuy

  • #3
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I put mentioned code into VB editor pressed "play" and nothing happened. What is wrong?
    M.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •