Key press

How do you make something react when a key is pressed?

Page 1 of 1

3 Replies - 1446 Views - Last Post: 18 February 2009 - 06:44 PM Rate Topic: -----

#1 0FreakFire0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-January 09

Key press

Post icon  Posted 18 February 2009 - 05:36 PM

I am making a game that involves a stick figure moving. I can get him to move using buttons, but I want to use arrow keys. Here's what i have so far:
Public Class Form1
	Dim StepCyclel As Integer
	Dim StepCycler As Integer
	Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'here is where my problem is		
If e.KeyCode = Keys.A Then
			Timer1.Enabled = True
			Stand.Enabled = False
		End If
		If e.KeyCode = Keys.D Then
			Timer2.Enabled = True
			Stand.Enabled = False
		End If
	End Sub

	Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
		If e.KeyCode = Keys.A Then
			Timer1.Enabled = False
			Stand.Enabled = True
		End If
		If e.KeyCode = Keys.D Then
			Timer2.Enabled = False
			Stand.Enabled = True
		End If
	End Sub
	'And here is where it ends
	Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
		Timer1.Enabled = True
		Stand.Enabled = False
	End Sub

	Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
		Timer1.Enabled = False
		Stand.Enabled = True
	End Sub
	Private Sub Button2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseDown
		Timer2.Enabled = True
		Stand.Enabled = False
	End Sub

	Private Sub Button2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseUp
		Timer2.Enabled = False
		Stand.Enabled = True
	End Sub
	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		PictureBox1.Left = PictureBox1.Left - 4
		PictureBox2.Left = PictureBox2.Left - 4
		PictureBox3.Left = PictureBox3.Left - 4
		PictureBox4.Left = PictureBox4.Left - 4
		If StepCyclel < 5 Then
			StepCyclel = StepCyclel + 1
		End If
		If StepCyclel = 4 Then
			StepCyclel = 0
		End If
		If StepCyclel = 0 Then
			PictureBox1.Visible = True
			PictureBox2.Visible = False
			PictureBox3.Visible = False
			PictureBox4.Visible = False
		End If
		If StepCyclel = 1 Then
			PictureBox4.Visible = True
			PictureBox1.Visible = False
			PictureBox2.Visible = False
			PictureBox3.Visible = False
		End If
		If StepCyclel = 2 Then
			PictureBox3.Visible = True
			PictureBox1.Visible = False
			PictureBox2.Visible = False
			PictureBox4.Visible = False
		End If
		If StepCyclel = 3 Then
			PictureBox4.Visible = True
			PictureBox1.Visible = False
			PictureBox2.Visible = False
			PictureBox3.Visible = False
		End If
	End Sub

	Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
		PictureBox1.Left = PictureBox1.Left + 4
		PictureBox2.Left = PictureBox2.Left + 4
		PictureBox3.Left = PictureBox3.Left + 4
		PictureBox4.Left = PictureBox4.Left + 4
		If StepCycler < 5 Then
			StepCycler = StepCycler + 1
		End If
		If StepCycler = 4 Then
			StepCycler = 0
		End If
		If StepCycler = 0 Then
			PictureBox1.Visible = True
			PictureBox2.Visible = False
			PictureBox3.Visible = False
			PictureBox4.Visible = False
		End If
		If StepCycler = 1 Then
			PictureBox4.Visible = True
			PictureBox1.Visible = False
			PictureBox2.Visible = False
			PictureBox3.Visible = False
		End If
		If StepCycler = 2 Then
			PictureBox3.Visible = False
			PictureBox1.Visible = False
			PictureBox2.Visible = True
			PictureBox4.Visible = False
		End If
		If StepCycler = 3 Then
			PictureBox4.Visible = True
			PictureBox1.Visible = False
			PictureBox2.Visible = False
			PictureBox3.Visible = False
		End If
	End Sub

	Private Sub Stand_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stand.Tick
		StepCyclel = 0
		StepCycler = 0
		PictureBox1.Visible = True
		PictureBox2.Visible = False
		PictureBox3.Visible = False
		PictureBox4.Visible = False

	End Sub

	

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	End Sub
End Class


Please help if you can. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Key press

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Key press

Posted 18 February 2009 - 06:10 PM

Use the Keys.Up and Keys.Down for the arrow buttons and make sure that you have set the Forms KeyPreview property to True. Otherwise the control that has focus will process the event.

You can also set it in code in the forms Load event.
Me.KeyPreview = True

Was This Post Helpful? 0
  • +
  • -

#3 luigiplumbersinc  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 16-February 09

Re: Key press

Posted 18 February 2009 - 06:16 PM

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'here is where my problem is		
If e.KeyCode = Keys.A Then
			Timer1.Enabled = True
			Stand.Enabled = False
		End If
		If e.KeyCode = Keys.D Then
			Timer2.Enabled = True
			Stand.Enabled = False
		End If
	End Sub

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
		If e.KeyCode = Keys.A Then
			Timer1.Enabled = False
			Stand.Enabled = True
		End If
		If e.KeyCode = Keys.D Then
			Timer2.Enabled = False
			Stand.Enabled = True
		End If
	End Sub
	'And here is where it ends



perhaps some troubleshooting will do the trick if its anything like VB... the keydown event will be called over and over until the key is no longer down and if the enabled property is anything like .Start() it may never reach its interval so 2 things

If e.KeyCode = Keys.A And Timer1.Enabled = False Then
	Timer1.Enabled = True
	Stand.Enabled = False
End If



and secondly you should make sure that when you hold down a its keycode matches that returned by "Keys.A"
... a message box would be an appropriate means..

Goodluck :D
Was This Post Helpful? 0
  • +
  • -

#4 0FreakFire0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-January 09

Re: Key press

Posted 18 February 2009 - 06:44 PM

Thanks for your help! It was very... helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1