07-20-2005, 11:40 PM
Ok so I am a real noob to visual basic.net. I am trying to make something happen whenever I press the the w key on my keyboad. I have searched in on the forums but everytime it says (keyascii as integer, shift as integer).
I don't understand how to do this and make it work. So basically could someone help me about by writing the exact code for making something happen when I press the "w" key, "a" key, "s" key, "d" key, UP arrow, Left Arrow, Right Arrow, and Down arrow. Including the by val stuff. Thanks in advance.
07-21-2005, 10:00 AM
Double click your form in the form designer.
Look to the top right, there will be a drop down box, it should say Load in it with a lightningbolt. Click the drop down box and look for KeyDown. Click Keydown and vb.net will make a new sub for you
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Now, put in
'E is the keyevent arguments. It holds the key that was pressed in its keycode property.
'When you type keys. a list should come up with all the keys.
if e.keycode = keys.w then
'Anything here will only execute if the user has pressed the W key.
But, if you have any controls on the form, this won't work and you'll have to put a certain line of code in the form_load event:
me.keypreview = true
'Let's the form preview all the keys that are pressed before they're sent to the controls.
07-22-2005, 01:00 AM
Is there any way I can make vb.net read two keys pressed at the same Time? For instance I want to move something up if "w" key, left if "left" key, right if "d" key and down if "s". But if I press the "q" key at the same time as a "w" key, or "a", or "s", or "d" then I want it to move differently.
07-22-2005, 06:53 AM
VBs processing is pretty quick. It wouldn't make a difference if you coded for W+S together. Pressing them both will still instruct both commands.
07-22-2005, 08:20 AM
If you are planning on being able to hold down the W and the S key so that it moves in both directions at once, then you should consider using booleans that can determine the key state based on which event fired most recently for that key.
Dim WOn As Boolean
in the KeyDown event, if the W key is pressed, set WOn = True
In the KeyUp event, if the W key is pressed, set WOn = False
Then, in a timer, you'd look at the value of WOn to determine if the W key is pressed. :)
09-18-2005, 01:23 PM
err going with the newb theme, lol, i did vb in grade 10 and now am in college, and its only my 4th class or so, so im kinda just jumping ahead, but i remeber when i wanted say a picture on my form to move it would be somthing like self.x= self.x +1; (self being the picture, and mabye im just thinking another language and thats whats screwing me up) :-\
09-18-2005, 06:15 PM
Those threads might help a bit