Page 1 of 2 12 LastLast
Results 1 to 40 of 46

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

  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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

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

    Thread moved from the 'Application Deployment' forum (which is for questions about installing/distributing your software) to the 'VB.Net' (VB2002 and later) forum

    For the benefit of others, ozirock's profile says VB 2005 Express

  3. #3
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    You didn't add handlers to your Form_KeyDown and Form_KeyUp method.

    What I would do is something like this:
    vb Code:
    1. Private Sub MainForm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.  
    3.         Select Case e.KeyCode
    4.             Case Keys.Up : MsgBox("UP")
    5.             Case Keys.Right : MsgBox("RIGHT")
    6.             Case Keys.Down : MsgBox("DOWN")
    7.             Case Keys.Left : MsgBox("LEFT")
    8.         End Select
    9.  
    10.     End Sub

    ps: Don't forget to set the KeyPreview property of your form to True!

  4. #4

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

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

    Sorry about the mix up with the forums guys.

    Thanks for your help gonzalioz, I can get your code to work in a file on its own but when I try to integrate it to my original code, it has no effect, I'm wondering if the code I wrote to connect to the interface board is causing problems for my other code even though it's telling me there are no errors, could that be the case?

    Or is the fact that I placed buttons on my form a factor? Is there a setting I missed related to this?

    Also I didnt know about the KeyPreview property so thanks for that

    Thanks,
    Oisin

  5. #5
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    Quote Originally Posted by ozirock View Post
    I'm wondering if the code I wrote to connect to the interface board is causing problems for my other code even though it's telling me there are no errors, could that be the case?
    Do you mean the button1_Click method? I don't think so


    What do you mean with integrated? English is not a my native language . Did you change anything to my code when using it in your own app? If so, can you post that code?

    The only setting that you have to change is me.keypreview = true (or using design mode) or else it won't work.
    Last edited by gonzalioz; Nov 6th, 2009 at 06:28 AM. Reason: brb Lunch

  6. #6

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

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

    Quote Originally Posted by gonzalioz View Post
    What do you mean with integrated?
    Sorry I just meant I changed my code to try change the colour of the label like I had been trying to do originally but I have since deleted my attempts and replaced my button pressing code with yours which I had tried on my computer and worked so I taught it should work in my original code but even the code below does not work for me.

    I dont see what could be causing the problem because the only thing there now is to connect to the board, it's very annoying.

    Thanks for your help,
    Oisin


    vb code:

    Public Class Form1

    Private Declare Function OpenDevice Lib "k8055d.dll" (ByVal CardAddress As Integer) As Integer
    Private Declare Sub SetDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    Private Declare Sub ClearDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    ' Public up, down, lleft, rright As Boolean 'to keep track of which buttons are already pressed


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim CardAddress As Integer
    Dim h As Integer
    CardAddress = 3

    h = OpenDevice(3)
    Label1.Text = "Card " + Str(h) + " connected"

    End Sub

    Private Sub MainForm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

    Select Case e.KeyCode
    Case Keys.Up : MsgBox("UP")
    Case Keys.Right : MsgBox("RIGHT")
    Case Keys.Down : MsgBox("DOWN")
    Case Keys.Left : MsgBox("LEFT")
    End Select

    End Sub

    End Class

  7. #7
    Addicted Member Dark Anima's Avatar
    Join Date
    Sep 2008
    Posts
    183

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

    Quote Originally Posted by ozirock View Post
    Sorry about the mix up with the forums guys.

    Thanks for your help gonzalioz, I can get your code to work in a file on its own but when I try to integrate it to my original code, it has no effect, I'm wondering if the code I wrote to connect to the interface board is causing problems for my other code even though it's telling me there are no errors, could that be the case?

    Or is the fact that I placed buttons on my form a factor? Is there a setting I missed related to this?

    Also I didnt know about the KeyPreview property so thanks for that

    Thanks,
    Oisin
    Remove your old key-event handlers if you still have them. If you have a duplicate procedure with different parameters you won't get an error, but only one of them will be called. If you have focus on control other than your form, handling MyBase.KeyDown() won't help you, but you'll have to handle that controls events instead. I recommend overriding the keydown event if you need to focus on other controls, like this:

    Code:
    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
            MsgBox(e.KeyCode)
            MyBase.OnKeyDown(e)
        End Sub

  8. #8

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

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

    I placed that into my form like below but it didn't work but it works in a new form on its own no problem.


    vb code:

    Public Class Form1

    Private Declare Function OpenDevice Lib "k8055d.dll" (ByVal CardAddress As Integer) As Integer
    Private Declare Sub SetDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    Private Declare Sub ClearDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    ' Public up, down, lleft, rright As Boolean 'to keep track of which buttons are already pressed


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim CardAddress As Integer
    Dim h As Integer
    CardAddress = 3

    h = OpenDevice(3)
    Label1.Text = "Card " + Str(h) + " connected"

    End Sub


    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
    MsgBox(e.KeyCode)
    MyBase.OnKeyDown(e)
    End Sub

    End Class

  9. #9
    Addicted Member Dark Anima's Avatar
    Join Date
    Sep 2008
    Posts
    183

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

    Quote Originally Posted by ozirock View Post
    I placed that into my form like below but it didn't work but it works in a new form on its own no problem.


    vb code:

    Public Class Form1

    Private Declare Function OpenDevice Lib "k8055d.dll" (ByVal CardAddress As Integer) As Integer
    Private Declare Sub SetDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    Private Declare Sub ClearDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    ' Public up, down, lleft, rright As Boolean 'to keep track of which buttons are already pressed


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim CardAddress As Integer
    Dim h As Integer
    CardAddress = 3

    h = OpenDevice(3)
    Label1.Text = "Card " + Str(h) + " connected"

    End Sub


    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
    MsgBox(e.KeyCode)
    MyBase.OnKeyDown(e)
    End Sub

    End Class
    Umm.. that should work just fine. Is that the new forms code? If so, please post the old ones.

  10. #10
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    Quote Originally Posted by Dark Anima View Post
    I recommend overriding the keydown event if you need to focus on other controls, like this:

    Code:
    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
            MsgBox(e.KeyCode)
            MyBase.OnKeyDown(e)
        End Sub
    Or keyPreview right? I know it's not the same but it does the job in this case.

  11. #11
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    Something I just thought of:

    When you make a connection. Are you sure it does connect and doesn't try forever? Maybe there is a loop somewhere that goes on forever, so your app never gets to running MsgBox(e.KeyCode) and MyBase.OnKeyDown(e)?

    It's a little far fetched but you never know.


    ---
    Sry for double post.

  12. #12

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

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

    No that was the code in side my old form, which doesn't work for me, I have buttons on my form does that cause a problem?

    Also I notice this byval e piece of code is in both the button and the keypress code, could one be stopping the other? What does the e mean, I am guessing is like a variable?

    Thanks,
    Oisin

  13. #13

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

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

    Oh wait a minute, if I don't push the connect button it wont read the rest of the code?

    I don't have the board with me, but it does connect cause I've controlled stuff by clicking buttons before but I didn't get the keys working.

  14. #14
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    Quote Originally Posted by ozirock View Post
    Oh wait a minute, if I don't push the connect button it wont read the rest of the code?
    No sry for the confusion. I meant:

    If you click the button.. It will connect. But maybe this connecting business goes on forever. so when you press a key after you clicked the button it will never run.

    But if you just run your app and press a button on your keyboard and nothing happens... then the problem is probaly something else.


    This is really a mystery lol XD.

  15. #15
    Addicted Member Dark Anima's Avatar
    Join Date
    Sep 2008
    Posts
    183

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

    Quote Originally Posted by ozirock View Post
    Oh wait a minute, if I don't push the connect button it wont read the rest of the code?

    I don't have the board with me, but it does connect cause I've controlled stuff by clicking buttons before but I didn't get the keys working.
    The code I gave you will pop up a messagebox with they keycode of whatever key you press on your keyboard.

  16. #16
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    Copied your code into a new project on my pc and it doesn't work either lol.

  17. #17
    Addicted Member Dark Anima's Avatar
    Join Date
    Sep 2008
    Posts
    183

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

    Quote Originally Posted by gonzalioz View Post
    Copied your code into a new project on my pc and it doesn't work either lol.
    Code:
    Public Class Form1
    
    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
            MsgBox(e.KeyCode)
            MyBase.OnKeyDown(e)
        End Sub
    
    End Class
    That really doesn't pop up anything when you press a button on your keyboard? Are you using VS2008?

  18. #18
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    O, now it works. Used this code of yours:

    vb 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.  
    21. Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
    22. MsgBox(e.KeyCode)
    23. MyBase.OnKeyDown(e)
    24. End Sub
    25.  
    26. End Class

    I am sorry, i have absolutely no clue why it doesn't work on your pc. There must be some setting/property that messes everything up.

  19. #19

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

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

    Quote Originally Posted by Dark Anima View Post
    Code:
    Public Class Form1
    
    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
            MsgBox(e.KeyCode)
            MyBase.OnKeyDown(e)
        End Sub
    
    End Class
    That really doesn't pop up anything when you press a button on your keyboard? Are you using VS2008?
    This code works on its own in a different form but when I add it into mine its not working, I really cant understand it

  20. #20

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

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

    If you add a few buttons to your form gonzalioz, does it still work?

  21. #21
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    Quote Originally Posted by ozirock View Post
    If you add a few buttons to your form gonzalioz, does it still work?
    Yes. Is keypreview set to true (guess it is, posted it a lot sorry :P). Even with dark anima's way using override etc.. you still need keypreview apparently.

  22. #22

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

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

    Just to check I done it right, I click on the form, went to the properties section in the bottom right of my screen and changed the key preview property to true. That is what you mean right?

  23. #23
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    Yeah .

    I think I got it though. If you have a lot of buttons on your form. The up down left right keys makes you select another button on your form! Did you try to press the key T for example when using the code dark anima posted? That works with me. The up and down etc keys however don't work, because all they do is go to another button.

    No idea how to solve this though. You could use W A S D instead ?

  24. #24

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

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

    Ha thats it, you figure it out, well done, I'll just have to use letters instead of arrows I guess or just get rid of the buttons

  25. #25
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    Just tried some other events, and the keyUP event does work.. But also still does select a new button . This also works and also still selects a new button:

    vb Code:
    1. Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
    2.  
    3.         Select Case keyData
    4.             Case Keys.Up : MsgBox("UP")
    5.             Case Keys.Right : MsgBox("RIGHT")
    6.             Case Keys.Down : MsgBox("DOWN")
    7.             Case Keys.Left : MsgBox("LEFT")
    8.         End Select
    9.  
    10.         Return (MyBase.ProcessDialogKey(keyData))
    11.     End Function

    But I guess it can be quite frustrating when your controlling your rc car, when pressing a key the selection of your buttons changes constantly :P

  26. #26

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

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

    That is perfect, it's exactly what I was trying to do, thank you so much for all your help, your a legend. You've no idea how excited I am to head home and wire it up now

  27. #27

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

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

    I've one more question if you can help, any idea how to make it change back when the key is released I taught I could do below but i think it needs to be refreshed constantly or something like that.

    I made two attempts and failed

    vb code attempt 1:

    Public Class Form1

    Private Declare Function OpenDevice Lib "k8055d.dll" (ByVal CardAddress As Integer) As Integer
    Private Declare Sub SetDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    Private Declare Sub ClearDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    Public up, down, lleft, rright As Boolean 'to keep track of which buttons are already pressed


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim CardAddress As Integer
    Dim h As Integer
    CardAddress = 3

    h = OpenDevice(3)
    Label1.Text = "Card " + Str(h) + " connected"

    End Sub

    Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean

    Select Case keyData
    Case Keys.Up : Label1.ForeColor = Color.Red
    Case Keys.Right : Label4.ForeColor = Color.Red
    Case Keys.Down : Label2.ForeColor = Color.Red
    Case Keys.Left : Label3.ForeColor = Color.Red
    Case Else
    Label1.ForeColor = Color.Black
    Label2.ForeColor = Color.Black
    Label3.ForeColor = Color.Black
    Label4.ForeColor = Color.Black

    End Select

    Return (MyBase.ProcessDialogKey(keyData))
    End Function

    End Class

    Attempt 2 vb code:

    Public Class Form1

    Private Declare Function OpenDevice Lib "k8055d.dll" (ByVal CardAddress As Integer) As Integer
    Private Declare Sub SetDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    Private Declare Sub ClearDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
    Public up, down, lleft, rright As Boolean 'to keep track of which buttons are already pressed


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim CardAddress As Integer
    Dim h As Integer
    CardAddress = 3

    h = OpenDevice(3)
    Label1.Text = "Card " + Str(h) + " connected"

    End Sub

    Private Sub MainForm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

    Select Case e.KeyCode
    Case Keys.A : Label1.ForeColor = Color.Red
    Case Keys.L : Label2.ForeColor = Color.Red
    Case Keys.Z : Label3.ForeColor = Color.Red
    Case Keys.K : Label4.ForeColor = Color.Red
    End Select

    End Sub

    Private Sub MainForm_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

    Select Case e.KeyCode
    Case Keys.A : Label1.ForeColor = Color.Black
    Case Keys.L : Label2.ForeColor = Color.Black
    Case Keys.Z : Label3.ForeColor = Color.Black
    Case Keys.K : Label4.ForeColor = Color.Black
    End Select

    End Sub


    End Class

  28. #28
    Addicted Member Dark Anima's Avatar
    Join Date
    Sep 2008
    Posts
    183

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

    Please take advantage of the code tags.
    What are you trying to achieve? Changing the labels colors works just fine for me like this:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim CardAddress As Integer
            Dim h As Integer
            CardAddress = 3
            h = OpenDevice(3)
            Label1.Text = "Card " + Str(h) + " connected"
        End Sub
    
        Private Declare Function OpenDevice Lib "k8055d.dll" (ByVal CardAddress As Integer) As Integer
        Private Declare Sub SetDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
        Private Declare Sub ClearDigitalChannel Lib "k8055d.dll" (ByVal Channel As Integer)
        Public up, down, lleft, rright As Boolean
    
        Private Sub MainForm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            Select Case e.KeyCode
                Case Keys.A : Label1.ForeColor = Color.Red
                Case Keys.L : Label2.ForeColor = Color.Red
                Case Keys.Z : Label3.ForeColor = Color.Red
                Case Keys.K : Label4.ForeColor = Color.Red
            End Select
        End Sub
    
        Private Sub MainForm_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
            Select Case e.KeyCode
                Case Keys.A : Label1.ForeColor = Color.Black
                Case Keys.L : Label2.ForeColor = Color.Black
                Case Keys.Z : Label3.ForeColor = Color.Black
                Case Keys.K : Label4.ForeColor = Color.Black
            End Select
        End Sub
    End Class

  29. #29
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    Yeah, attempt 2 works for me as well. ?

  30. #30
    Addicted Member Dark Anima's Avatar
    Join Date
    Sep 2008
    Posts
    183

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

    EDIT: Oops. Nvm.

  31. #31

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

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

    I was trying to have it change colour when the button is pressed and change back when the button is released, it's the changing back I'm having trouble with, sorry should have explained that

  32. #32
    Addicted Member Dark Anima's Avatar
    Join Date
    Sep 2008
    Posts
    183

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

    Quote Originally Posted by ozirock View Post
    I was trying to have it change colour when the button is pressed and change back when the button is released, it's the changing back I'm having trouble with, sorry should have explained that
    I copy & pasted your code, and also the releasing worked fine.

  33. #33

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

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

    Thats annoying, I'm not at my computer anymore, I had to go to the lab so can't try again but I will later and I'll let you know how I get on, they were staying red when I was doing it, very annoying, I#ll let you know how I get on

  34. #34
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

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

    Quote Originally Posted by gonzalioz View Post
    Just tried some other events, and the keyUP event does work.. But also still does select a new button .
    If you want to use the arrow keys but not select controls on the form, override ProcessDialogKey but leave out the call to the MyBase Function. For example:
    Code:
       Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
            Select Case keyData
                Case Keys.Up
                    'do Up key action
                Case Keys.Down
                    'do Down key action
                    'etc.
            End Select
     
        End Function
    This is a situation where you have to override a Sub or Function instead of handling the corresponding event.

    cheers, BB

  35. #35
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    Quote Originally Posted by boops boops View Post
    If you want to use the arrow keys but not select controls on the form, override ProcessDialogKey but leave out the call to the MyBase Function. For example:
    Code:
       Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
            Select Case keyData
                Case Keys.Up
                    'do Up key action
                Case Keys.Down
                    'do Down key action
                    'etc.
            End Select
     
        End Function
    This is a situation where you have to override a Sub or Function instead of handling the corresponding event.

    cheers, BB
    Didn't work either . Tried that.

    This also works and also still selects a new button:

    vb Code:
    1. Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
    2.  
    3.         Select Case keyData
    4.             Case Keys.Up : MsgBox("UP")
    5.             Case Keys.Right : MsgBox("RIGHT")
    6.             Case Keys.Down : MsgBox("DOWN")
    7.             Case Keys.Left : MsgBox("LEFT")
    8.         End Select
    9.  
    10.         Return (MyBase.ProcessDialogKey(keyData))
    11.     End Function

  36. #36
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

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

    This is an implicit call to MyBase.ProcessDialogKey:
    Code:
    Return (MyBase.ProcessDialogKey(keyData))
    Try commenting it out. BB

  37. #37
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

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

    Quote Originally Posted by boops boops View Post
    This is an implicit call to MyBase.ProcessDialogKey:
    Code:
    Return (MyBase.ProcessDialogKey(keyData))
    Try commenting it out. BB
    Nice that seems to work (remember I am not the topic starter). But he must be able to register when the key is down and released again. So he uses the keydown and keyup events with character keys instead of the arrowkeys.

  38. #38
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

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

    Quote Originally Posted by gonzalioz View Post
    Nice that seems to work (remember I am not the topic starter). But he must be able to register when the key is down and released again. So he uses the keydown and keyup events with character keys instead of the arrowkeys.
    Once ProcessDialogKey has been overriden without a call to MyBase, the OP can code responses to the arrow keys in the KeyUp and KeyDown events; control selection will not happen. That is what ozirock wanted, isn't it? For example:
    Code:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.Up Then MsgBox("Up key is pressed")
        End Sub
    Incidentally, instead of leaving out the call to MyBase.ProcessDialogKey, it could go in an Else clause in the Case statement. That way the Tab and Enter keys would remain effective.

    cheers, BB
    Last edited by boops boops; Nov 6th, 2009 at 10:21 AM.

  39. #39

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

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

    Sorry you were right, I loaded up attempt two again and it worked, I don't know how I missed it, I'll work on the arrow keys some more during the week, I have a test in biomaterial's this week so I need to keep the head in the books for the time being. I'm going to make a website about this project and any others I do and I'd like to acknowledge you guy's help in it, would you like me to put your usernames as references i.e. gonzalioz from vbforums or actual names or nothing at all, thats cool too, if I dont get a reply I'll assume you don't want your name mentioned I think thats the fairest way.

    I'll keep you guy's informed of how I get on with the arrow keys and post the code if I get it working.

    Thanks for all the help guy's

  40. #40

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

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

    Made a small video of it working, I got rid of the buttons from the form and just left the labels, here it is

Page 1 of 2 12 LastLast

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