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
Printable View
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
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?VB Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyUp Command1.Move Command1.Left, Command1.Top - 120 Case vbKeyDown Command1.Move Command1.Left, Command1.Top + 120 Case vbKeyLeft Command1.Move Command1.Left - 120 Case vbKeyRight Command1.Move Command1.Left + 120 End Select End Sub Private Sub Form_Load() KeyPreview = True Command1.TabStop = False End Sub
umm, you're probably dealing with one of those stupid problems where the keydown event doesnt' fire. right?:rolleyes:
that's evil:eek:
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:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Private Const VK_LEFT = &H25 Private Const VK_UP = &H26 Private Const VK_RIGHT = &H27 Private Const VK_DOWN = &H28 Private Sub Form_Load() Timer1.Interval = 1 End Sub Private Sub Timer1_Timer() If GetAsyncKeyState(VK_UP) <> 0 Then With Command1 .Move .Left, .Top - 120 End With End If If GetAsyncKeyState(VK_DOWN) <> 0 Then With Command1 .Move .Left, .Top + 120 End With End If If GetAsyncKeyState(VK_LEFT) <> 0 Then With Command1 .Move .Left - 120 End With End If If GetAsyncKeyState(VK_RIGHT) <> 0 Then With Command1 .Move .Left + 120 End With End If End Sub
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 :D
Thanks Guys
Tweaked so it only works when the form has focus.
VB Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Private Declare Function GetActiveWindow Lib "user32" () As Long Private Const VK_LEFT = &H25 Private Const VK_UP = &H26 Private Const VK_RIGHT = &H27 Private Const VK_DOWN = &H28 Private Sub Form_Load() Timer1.Interval = 1 End Sub Private Sub Timer1_Timer() If GetAsyncKeyState(VK_UP) <> 0 And GetActiveWindow() = Me.hWnd Then With Command1 .Move .Left, .Top - 60 End With End If If GetAsyncKeyState(VK_DOWN) <> 0 And GetActiveWindow() = Me.hWnd Then With Command1 .Move .Left, .Top + 60 End With End If If GetAsyncKeyState(VK_LEFT) <> 0 And GetActiveWindow() = Me.hWnd Then With Command1 .Move .Left - 60 End With End If If GetAsyncKeyState(VK_RIGHT) <> 0 And GetActiveWindow() = Me.hWnd Then With Command1 .Move .Left + 60 End With End If End Sub