-
Nov 5th, 2009, 08:31 PM
#1
Thread Starter
Member
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:
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 Form_KeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer) '------------------------------------------------------------------' 'This section of code detects which key is being pressed. Once the 'key is pressed, it changes the colour of the label to red and adds the 'correct value to ouput. 1,2,4,8 are the correct values. To write 'to the parallel port, it's done in binary. 1 in binary is 00000001 '2 in binary is 00000010, 4 in binary is 00000100, 8 in binary is 00001000 'so by saying "out DlPortWritePortUchar, 4" we are actually turning on one pin of the parallel port 'If we say "out 888, (1+2)" or "DlPortWritePortUchar 888, 3" we are turning on two pins, pins 1 and 2. '-------------------------------------------------------------------' If (KeyCode = 38) And up <> True Then Label2.ForeColor = Color.Red : up = True If (KeyCode = 40) And down <> True Then Label2.ForeColor = Color.Red : down = True If (KeyCode = 37) And lleft <> True Then Label3.ForeColor = Color.Red : lleft = True If (KeyCode = 39) And rright <> True Then Label4.ForeColor = Color.Red : rright = True End Sub Private Sub Form_KeyUp(ByVal KeyCode As Integer, ByVal Shift As Integer) '------------------------------------------------------------------' 'This section of code detects when a pressed key has been lifted. 'changes the label's colour back to black and subtracts a value from 'output. It does the opposite of the keydown code, in that it turns 'off a parallel pin rather than turning it on. '-------------------------------------------------------------------' If (KeyCode = 38) Then Label1.ForeColor = Color.Black : up = False If (KeyCode = 40) Then Label2.ForeColor = Color.Black : down = False If (KeyCode = 37) Then Label3.ForeColor = Color.Black : lleft = False If (KeyCode = 39) Then Label4.ForeColor = Color.Black : rright = False End Sub End Class
Last edited by si_the_geek; Nov 6th, 2009 at 05:05 AM.
Reason: added code tags
-
Nov 6th, 2009, 05:04 AM
#2
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
-
Nov 6th, 2009, 05:21 AM
#3
Hyperactive Member
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:
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
ps: Don't forget to set the KeyPreview property of your form to True!
-
Nov 6th, 2009, 06:00 AM
#4
Thread Starter
Member
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
-
Nov 6th, 2009, 06:25 AM
#5
Hyperactive Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by ozirock
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
-
Nov 6th, 2009, 06:34 AM
#6
Thread Starter
Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by gonzalioz
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
-
Nov 6th, 2009, 06:34 AM
#7
Addicted Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by ozirock
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
-
Nov 6th, 2009, 06:41 AM
#8
Thread Starter
Member
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
-
Nov 6th, 2009, 06:54 AM
#9
Addicted Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by ozirock
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.
-
Nov 6th, 2009, 06:55 AM
#10
Hyperactive Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by Dark Anima
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.
-
Nov 6th, 2009, 07:00 AM
#11
Hyperactive Member
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.
-
Nov 6th, 2009, 07:03 AM
#12
Thread Starter
Member
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
-
Nov 6th, 2009, 07:07 AM
#13
Thread Starter
Member
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.
-
Nov 6th, 2009, 07:11 AM
#14
Hyperactive Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by ozirock
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.
-
Nov 6th, 2009, 07:12 AM
#15
Addicted Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by ozirock
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.
-
Nov 6th, 2009, 07:13 AM
#16
Hyperactive Member
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.
-
Nov 6th, 2009, 07:19 AM
#17
Addicted Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by gonzalioz
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?
-
Nov 6th, 2009, 07:21 AM
#18
Hyperactive Member
Re: Cant figure out how to use keys, any ideas?
O, now it works. Used this code of yours:
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
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.
-
Nov 6th, 2009, 07:24 AM
#19
Thread Starter
Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by Dark Anima
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
-
Nov 6th, 2009, 07:26 AM
#20
Thread Starter
Member
Re: Cant figure out how to use keys, any ideas?
If you add a few buttons to your form gonzalioz, does it still work?
-
Nov 6th, 2009, 07:31 AM
#21
Hyperactive Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by ozirock
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.
-
Nov 6th, 2009, 07:35 AM
#22
Thread Starter
Member
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?
-
Nov 6th, 2009, 07:36 AM
#23
-
Nov 6th, 2009, 07:38 AM
#24
Thread Starter
Member
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
-
Nov 6th, 2009, 07:46 AM
#25
Hyperactive Member
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:
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Up : MsgBox("UP")
Case Keys.Right : MsgBox("RIGHT")
Case Keys.Down : MsgBox("DOWN")
Case Keys.Left : MsgBox("LEFT")
End Select
Return (MyBase.ProcessDialogKey(keyData))
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
-
Nov 6th, 2009, 07:54 AM
#26
Thread Starter
Member
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
-
Nov 6th, 2009, 08:39 AM
#27
Thread Starter
Member
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
-
Nov 6th, 2009, 08:47 AM
#28
Addicted Member
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
-
Nov 6th, 2009, 08:49 AM
#29
Hyperactive Member
Re: Cant figure out how to use keys, any ideas?
Yeah, attempt 2 works for me as well. ?
-
Nov 6th, 2009, 08:52 AM
#30
Addicted Member
Re: Cant figure out how to use keys, any ideas?
-
Nov 6th, 2009, 08:58 AM
#31
Thread Starter
Member
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
-
Nov 6th, 2009, 09:07 AM
#32
Addicted Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by ozirock
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.
-
Nov 6th, 2009, 09:18 AM
#33
Thread Starter
Member
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
-
Nov 6th, 2009, 09:27 AM
#34
Re: Cant figure out how to use keys, any ideas?
Originally Posted by gonzalioz
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
-
Nov 6th, 2009, 09:34 AM
#35
Hyperactive Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by boops boops
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:
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean Select Case keyData Case Keys.Up : MsgBox("UP") Case Keys.Right : MsgBox("RIGHT") Case Keys.Down : MsgBox("DOWN") Case Keys.Left : MsgBox("LEFT") End Select Return (MyBase.ProcessDialogKey(keyData)) End Function
-
Nov 6th, 2009, 09:46 AM
#36
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
-
Nov 6th, 2009, 09:56 AM
#37
Hyperactive Member
Re: Cant figure out how to use keys, any ideas?
Originally Posted by boops boops
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.
-
Nov 6th, 2009, 10:12 AM
#38
Re: Cant figure out how to use keys, any ideas?
Originally Posted by gonzalioz
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.
-
Nov 7th, 2009, 11:17 AM
#39
Thread Starter
Member
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
-
Nov 8th, 2009, 09:36 AM
#40
Thread Starter
Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|