Results 1 to 6 of 6

Thread: Arrow Keys....

  1. #1

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Angry Arrow Keys....

    Ok... I thought I had the code to move a Command Button around the screen using the Arrow keys... but it doesn't appear to be working.

    Can someone post some code for me?

    Thanks in advance
    Don't Rate my posts.

  2. #2
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2. Select Case KeyCode
    3. Case vbKeyUp
    4.     Command1.Move Command1.Left, Command1.Top - 120
    5. Case vbKeyDown
    6.     Command1.Move Command1.Left, Command1.Top + 120
    7. Case vbKeyLeft
    8.     Command1.Move Command1.Left - 120
    9. Case vbKeyRight
    10.     Command1.Move Command1.Left + 120
    11. End Select
    12. End Sub
    13.  
    14. Private Sub Form_Load()
    15. KeyPreview = True
    16. Command1.TabStop = False
    17. End Sub
    Throw a picturebox control on the form and make it's width and height 0 then this code will work. Why do you need to move a command button around the form with the keys?
    <removed by admin>

  3. #3
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    umm, you're probably dealing with one of those stupid problems where the keydown event doesnt' fire. right?

    that's evil
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Or you can use the API in a timer to move it. It goes a lot faster this way so you might want to lower the number in the move function.
    VB Code:
    1. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    2. Private Const VK_LEFT = &H25
    3. Private Const VK_UP = &H26
    4. Private Const VK_RIGHT = &H27
    5. Private Const VK_DOWN = &H28
    6.  
    7. Private Sub Form_Load()
    8. Timer1.Interval = 1
    9. End Sub
    10.  
    11. Private Sub Timer1_Timer()
    12. If GetAsyncKeyState(VK_UP) <> 0 Then
    13.     With Command1
    14.         .Move .Left, .Top - 120
    15.     End With
    16. End If
    17. If GetAsyncKeyState(VK_DOWN) <> 0 Then
    18.     With Command1
    19.         .Move .Left, .Top + 120
    20.     End With
    21. End If
    22. If GetAsyncKeyState(VK_LEFT) <> 0 Then
    23.     With Command1
    24.         .Move .Left - 120
    25.     End With
    26. End If
    27. If GetAsyncKeyState(VK_RIGHT) <> 0 Then
    28.     With Command1
    29.         .Move .Left + 120
    30.     End With
    31. End If
    32. End Sub
    <removed by admin>

  5. #5

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    2D Game I'm trying to make... I was planning on making a real icon for it if I got something half good going..

    Sweet, the code works
    Thanks Guys
    Don't Rate my posts.

  6. #6
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Tweaked so it only works when the form has focus.
    VB Code:
    1. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    2. Private Declare Function GetActiveWindow Lib "user32" () As Long
    3.  
    4. Private Const VK_LEFT = &H25
    5. Private Const VK_UP = &H26
    6. Private Const VK_RIGHT = &H27
    7. Private Const VK_DOWN = &H28
    8.  
    9. Private Sub Form_Load()
    10. Timer1.Interval = 1
    11. End Sub
    12.  
    13. Private Sub Timer1_Timer()
    14. If GetAsyncKeyState(VK_UP) <> 0 And GetActiveWindow() = Me.hWnd Then
    15.     With Command1
    16.         .Move .Left, .Top - 60
    17.     End With
    18. End If
    19. If GetAsyncKeyState(VK_DOWN) <> 0 And GetActiveWindow() = Me.hWnd Then
    20.     With Command1
    21.         .Move .Left, .Top + 60
    22.     End With
    23. End If
    24. If GetAsyncKeyState(VK_LEFT) <> 0 And GetActiveWindow() = Me.hWnd Then
    25.     With Command1
    26.         .Move .Left - 60
    27.     End With
    28. End If
    29. If GetAsyncKeyState(VK_RIGHT) <> 0 And GetActiveWindow() = Me.hWnd Then
    30.     With Command1
    31.         .Move .Left + 60
    32.     End With
    33. End If
    34. End Sub
    <removed by admin>

Posting Permissions

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



Click Here to Expand Forum to Full Width