|
-
Jun 4th, 2002, 12:16 AM
#1
Thread Starter
PowerPoster
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
-
Jun 4th, 2002, 12:35 AM
#2
PowerPoster
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
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?
-
Jun 4th, 2002, 12:35 AM
#3
-
Jun 4th, 2002, 12:39 AM
#4
PowerPoster
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
-
Jun 4th, 2002, 12:40 AM
#5
Thread Starter
PowerPoster
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
-
Jun 4th, 2002, 12:44 AM
#6
PowerPoster
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|