1 - Crea un nuevo proyecto
2 - Inserta un Timer1 y un Label1
Copia y pega este código
Pruébalo y verás como funciona correctamente esta API
Código vb:
Ver originalOption Explicit
' Api Captura Teclado
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Function KeysPressed(KeyCode1 As KeyCodeConstants, Optional KeyCode2 As KeyCodeConstants, Optional KeyCode3 As KeyCodeConstants) As Boolean
' Funcion que devuelve VERDADERO si se pulsaron las teclas solicitadas
If GetAsyncKeyState(KeyCode1) >= 0 Then Exit Function
If KeyCode2 = 0 Then KeysPressed = True: Exit Function
If GetAsyncKeyState(KeyCode2) >= 0 Then Exit Function
If KeyCode3 = 0 Then KeysPressed = True: Exit Function
If GetAsyncKeyState(KeyCode3) >= 0 Then Exit Function
KeysPressed = True
End Function
Private Sub Form_Load()
' Activamos TIMER que capturará las teclas por ejemplo cada 10 milisegundos
Timer1.Interval = 10
Timer1.Enabled = True
' Maximizamos la pantalla
Me.WindowState = vbMaximized
' Ajustamos y centramos el Label
Label1.FontSize = 18
Label1.FontBold = True
Label1.ForeColor = vbRed
Label1.Caption = "oOo"
Label1.AutoSize = True
Label1.Move (Screen.Width - Label1.Width) / 2, (Screen.Height - Label1.Height) / 2
End Sub
Private Sub Timer1_Timer()
' Captura de Teclas en el Temporizador
Timer1.Enabled = False
If KeysPressed(vbKeyLeft) Then
Label1.Move Label1.Left - 10
ElseIf KeysPressed(vbKeyUp) Then
Label1.Move Label1.Left, Label1.Top - 10
ElseIf KeysPressed(vbKeyDown) Then
Label1.Move Label1.Left, Label1.Top + 10
ElseIf KeysPressed(vbKeyRight) Then
Label1.Move Label1.Left + 10
End If
Timer1.Enabled = True
End Sub