Results 1 to 40 of 46

Thread: Cant figure out how to use keys, any ideas?

Threaded View

  1. #1

    Thread Starter
    Member ozirock's Avatar
    Join Date
    Nov 2009
    Posts
    35

    Cant figure out how to use keys, any ideas?

    Hi Guy's,

    I'm pretty much a newbie to vb, what I'm trying to do is control an rc car from my pc and I'm trying to do this through an interface board I have. I have code below where I have started the design but I'm trying to figure out how to take inputs from the arrow keys which I cant figure out, I've just tried to set up a test by changing the colour of labels but it's not working, can anyone see my error? Please excuse any foolish newbie mistakes.

    Thanks,
    Oisin

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Declare Function OpenDevice Lib "k8055d.dll" (ByVal CardAddress As Integer) As Integer
    4.     Private Declare Sub SetDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    5.     Private Declare Sub ClearDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    6.     Public up, down, lleft, rright As Boolean 'to keep track of which buttons are already pressed
    7.  
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.  
    11.         Dim CardAddress As Integer
    12.         Dim h As Integer
    13.         CardAddress = 3
    14.  
    15.         h = OpenDevice(3)
    16.         Label1.Text = "Card " + Str(h) + " connected"
    17.  
    18.     End Sub
    19.  
    20.     Private Sub Form_KeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
    21.         '------------------------------------------------------------------'
    22.         'This section of code detects which key is being pressed.  Once the
    23.         'key is pressed, it changes the colour of the label to red and adds the
    24.         'correct value to ouput.  1,2,4,8 are the correct values.  To write
    25.         'to the parallel port, it's done in binary.  1 in binary is 00000001
    26.         '2 in binary is 00000010, 4 in binary is 00000100, 8 in binary is 00001000
    27.         'so by saying "out DlPortWritePortUchar, 4" we are actually turning on one pin of the parallel port
    28.         'If we say "out 888, (1+2)" or "DlPortWritePortUchar 888, 3" we are turning on two pins, pins 1 and 2.
    29.         '-------------------------------------------------------------------'
    30.  
    31.         If (KeyCode = 38) And up <> True Then Label2.ForeColor = Color.Red : up = True
    32.         If (KeyCode = 40) And down <> True Then Label2.ForeColor = Color.Red : down = True
    33.         If (KeyCode = 37) And lleft <> True Then Label3.ForeColor = Color.Red : lleft = True
    34.         If (KeyCode = 39) And rright <> True Then Label4.ForeColor = Color.Red : rright = True
    35.  
    36.     End Sub
    37.  
    38.     Private Sub Form_KeyUp(ByVal KeyCode As Integer, ByVal Shift As Integer)
    39.         '------------------------------------------------------------------'
    40.         'This section of code detects when a pressed key has been lifted.
    41.         'changes the label's colour back to black and subtracts a value from
    42.         'output.  It does the opposite of the keydown code, in that it turns
    43.         'off a parallel pin rather than turning it on.
    44.         '-------------------------------------------------------------------'
    45.  
    46.         If (KeyCode = 38) Then Label1.ForeColor = Color.Black : up = False
    47.         If (KeyCode = 40) Then Label2.ForeColor = Color.Black : down = False
    48.         If (KeyCode = 37) Then Label3.ForeColor = Color.Black : lleft = False
    49.         If (KeyCode = 39) Then Label4.ForeColor = Color.Black : rright = False
    50.      
    51.     End Sub
    52.  
    53. End Class
    Last edited by si_the_geek; Nov 6th, 2009 at 05:05 AM. Reason: added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width