i want to design a software for a robot. i want to use the arrow keys of keyboard to direct the robot. can any one tell me that how can i do coding of that?

Dani AI

Generated

Nice start, . Building on ’s tip: arrow keys do not raise KeyPress in VB6, so handle KeyDown/KeyUp. For : you do not declare the arrow keys yourself; VB6 already exposes constants you can use (values shown only for clarity).

' Built-in constants:
' vbKeyUp    = &H26
' vbKeyDown  = &H28
' vbKeyLeft  = &H25
' vbKeyRight = &H27
' (Optional, for readability)
Public Enum ArrowKeys: kUp = vbKeyUp: kDown = vbKeyDown: kLeft = vbKeyLeft: kRight = vbKeyRight: End Enum

For smooth robot motion (not one-shot MsgBox), set Form.KeyPreview = True and use a Timer to stream commands while a key is held. This avoids flooding your serial/UDP link and gives you clean start/stop behavior.

' On the form: KeyPreview=True, add Timer tmrMove Interval=50
Option Explicit
Dim mvUp As Boolean, mvDown As Boolean, mvLeft As Boolean, mvRight As Boolean

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyUp Then mvUp = True
    If KeyCode = vbKeyDown Then mvDown = True
    If KeyCode = vbKeyLeft Then mvLeft = True
    If KeyCode = vbKeyRight Then mvRight = True
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyUp Then mvUp = False
    If KeyCode = vbKeyDown Then mvDown = False
    If KeyCode = vbKeyLeft Then mvLeft = False
    If KeyCode = vbKeyRight Then mvRight = False
End Sub

Private Sub tmrMove_Timer()
    If mvUp Then SendRobot "FWD"
    If mvDown Then SendRobot "BACK"
    If mvLeft Then SendRobot "LEFT"
    If mvRight Then SendRobot "RIGHT"
End Sub

Private Sub SendRobot(ByVal cmd As String)
    ' TODO: write to MSComm/WinSock/etc
End Sub

For : KeyPreview only works while your form has focus. To show a hidden form with a key, either register a global hotkey (recommended: Ctrl+K) or poll the key globally. Quick option:

' In a module:
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
' On a hidden form with Timer Interval=25:
Private Sub tmrHotkey_Timer()
    Static armed As Boolean
    Dim down As Boolean: down = (GetAsyncKeyState(vbKeyK) And &H8000) <> 0
    If down And Not armed Then Me.Show: Me.Visible = True
    armed = down
End Sub

Tip: if a child control still eats the arrows, handle that control’s KeyDown too.

Recommended Answers

All 5 Replies

Set KeyPreview as True on form properties.
Then add this following code :

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'make sure KeyPreview is True on Form Properties
    'On Error Resume Next
    Select Case KeyCode
        Case vbKeyUp
            MsgBox "Up"
        Case vbKeyDown
            MsgBox "Down"
        Case vbKeyLeft
            MsgBox "Left"
        Case vbKeyRight
            MsgBox "Right"
    End Select
End Sub
commented: Good +0

Thnk you..!! it worked..!!! i can direct my robot.. using arrow keys..!!!

How do you declare the up, down, left and right keys?

hOW CAN i dO IT when My Focus is not on the Form Like I have hidden and and I want to show it with a key say "K" anybody knows how to do that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.