Right now i'm using
If keycode = vbkeyright then
me.right = me.right + 100
endif
If keycode = vbkeyup then
me.top = me.top - 100
endif
how do i make it so i can press them both at the same time so i can have both effects at the same time?
Printable View
Right now i'm using
If keycode = vbkeyright then
me.right = me.right + 100
endif
If keycode = vbkeyup then
me.top = me.top - 100
endif
how do i make it so i can press them both at the same time so i can have both effects at the same time?
Well, you can't check 2 keys at once. Here is an alternative I found somewhere and modified it a little.
This code is not very good, 'cause it needs a timer. But it works. :p
First, in Key_Up/Down event check which keys are pressed, then inside the timer move the form depending on those keys.
VB Code:
Option Explicit Dim bUpPressed As Boolean Dim bDownPressed As Boolean Dim bLeftPressed As Boolean Dim bRightPressed As Boolean '============================================================= Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyUp bUpPressed = True bDownPressed = False Case vbKeyDown bDownPressed = True bUpPressed = False Case vbKeyLeft bLeftPressed = True bRightPressed = False Case vbKeyRight bRightPressed = True bLeftPressed = False End Select End Sub '============================================================= Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyUp bUpPressed = False Case vbKeyDown bDownPressed = False Case vbKeyLeft bLeftPressed = False Case vbKeyRight bRightPressed = False End Select End Sub '============================================================= Private Sub Form_Load() Timer1.Interval = 10 End Sub '============================================================= Private Sub Timer1_Timer() If bUpPressed = True Then Me.Top = Me.Top - 50 ElseIf bDownPressed = True Then Me.Top = Me.Top + 50 End If ' If bLeftPressed = True Then Me.Left = Me.Left - 50 ElseIf bRightPressed = True Then Me.Left = Me.Left + 50 End If End Sub
Hi lilwupster,
This code involves a couple API calls, but it's a little more straight forward.
It should also do what you want.
Add a timer control first
VB Code:
'in the declare section Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Private Declare Function GetActiveWindow Lib "user32" () As Long Private Sub Form_Load() Timer1.Interval = 10 End Sub Private Sub Timer1_Timer() 'check if window has focus 'otherwise window will move even when another program has focus If GetActiveWindow() <> Me.hWnd Then Exit Sub If GetAsyncKeyState(vbKeyUp) Then Me.Top = Me.Top - 15 If GetAsyncKeyState(vbKeyDown) Then Me.Top = Me.Top + 15 If GetAsyncKeyState(vbKeyLeft) Then Me.Left = Me.Left - 15 If GetAsyncKeyState(vbKeyRight) Then Me.Left = Me.Left + 15 End Sub
Hi iPrank, I swear I'm not stalking you. thanks for the rate.
thank you, they both work very well
Your welcome.
DirectInput would have also done it.
Don't worry. :)Quote:
Originally Posted by Keith_VB6
iSpy on people everyday. ;)