Sunday, August 2, 2009

How would I make a shape move using arrow keys in Visual Basic?

I would like it to move a a pixel or two each time. I understand I need to use vbKeyUp, vbKeyDown, vbKeyLeft, vbKeyRight, but I don't understand how I am supposed to write the code. Say I am using Shape1 as the object I would like to move. What code do I have to write? Under what private sub? I need to introduce Keycode as integer? Why?

How would I make a shape move using arrow keys in Visual Basic?
Hi,


Put a shape on the form and write the following code:


--------------------------------------...


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)


'for going left and right


If KeyCode = 37 Then Shape1.Left = Shape1.Left - 10


If KeyCode = 39 Then Shape1.Left = Shape1.Left + 10





'for going up and down


If KeyCode = 38 Then Shape1.Top = Shape1.Top - 10


If KeyCode = 40 Then Shape1.Top = Shape1.Top + 10


End Sub


--------------------------------------...


No comments:

Post a Comment