Si quieres leer el teclado desde cualquier sitio tienes que usar algo como GetAsyncKeyState.
Algo asi:
Con un timer...
Código vb:
Ver originalOption Explicit
Private Const constKeyDown = -32767
Const MOUSEEVENTF_MOVE = &H1 ' movimiento del mouse
Const MOUSEEVENTF_LEFTDOWN = &H2 ' botón izquierdo presionado
Const MOUSEEVENTF_LEFTUP = &H4 ' botón izquierdo soltado
Const MOUSEEVENTF_RIGHTDOWN = &H8 ' botón derecho presionado
Const MOUSEEVENTF_RIGHTUP = &H10 ' botón derecho soltado
Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' botón central presionado
Const MOUSEEVENTF_MIDDLEUP = &H40 ' botón central soltado
Const MOUSEEVENTF_ABSOLUTE = &H8000 ' movimiento absoluto
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Sub Form_Load()
Timer1.Interval = 50
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim Point As POINTAPI
Static Levantar_L As Integer
Static Levantar_R As Integer
If GetAsyncKeyState(40) <= constKeyDown Then
'MsgBox ("Abajo")
GetCursorPos Point
SetCursorPos Point.X, Point.Y + 10
End If
If GetAsyncKeyState(38) <= constKeyDown Then
'MsgBox ("Arriba")
GetCursorPos Point
SetCursorPos Point.X, Point.Y - 10
End If
If GetAsyncKeyState(37) <= constKeyDown Then
'MsgBox ("Izquierda")
GetCursorPos Point
SetCursorPos Point.X - 10, Point.Y
End If
If GetAsyncKeyState(39) <= constKeyDown Then
'MsgBox ("Derecha")
GetCursorPos Point
SetCursorPos Point.X + 10, Point.Y
End If
If GetAsyncKeyState(vbKeyZ) <= constKeyDown Then
If Levantar_L = 0 Then
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0
Levantar_L = 1
End If
Else
If Levantar_L = 1 Then
mouse_event MOUSEEVENTF_LEFTUP Or MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0
Levantar_L = 0
End If
'MsgBox ("Z")
End If
If GetAsyncKeyState(vbKeyX) = constKeyDown Then
If Levantar_R = 0 Then
mouse_event MOUSEEVENTF_RIGHTDOWN Or MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0
Levantar_R = 1
End If
Else
If Levantar_R = 1 Then
mouse_event MOUSEEVENTF_RIGHTUP Or MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0
Levantar_R = 0
End If
'MsgBox ("X")
End If
End Sub
Saludos
PD: Lo he editado para mejorarlo. Ahora funciona tambien en diagonal.