Question Detect Key Presses

cable729

New member
Joined
Sep 12, 2008
Messages
2
Programming Experience
Beginner
I have tried many MANY different things, looked at dozens of topics about this, and still it won't work. Here's my code:

VB.NET:
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

        If Val(e.KeyChar) = Keys.Left Then
            MsgBox("")
        End If
    End Sub

and for some reason if i put an or in everything but the first key displays the message box

VB.NET:
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

        If Val(e.KeyChar) = Keys.Left Or Keys.A Then
            MsgBox("")
        End If
    End Sub

I don't know what to try anymore, please help!

BTW, I am using VB.NET 3.5 (2008)

Thanks in advance!
 
VB.NET:
Private Sub Form1_KeyUp(...) Handles Me.KeyUp
    Select Case e.KeyCode
        Case Keys.Left
           Messagebox.Show("Left arrow")
        Case Keys.Right
           Messagebox.Show("Right arrow")
        Case Keys.Up
           Messagebox.Show("Up arrow")
        Case Keys.Down
           Messagebox.Show("Down arrow")
    End Select
End Sub
 
Thanks!
But that's not even close to all my problems. Most everything in my program says not instanced or out of bounds of array, do you want to look at it?

Uploading.com - The best file hosting service!

if you don't trust me for a link, then here:

form1:

VB.NET:
Public Class Form1
	Dim Snake(0) As snake
	Dim Time As Integer
	Dim Speed As Double = 0.6
	Dim Dummy As New PictureBox
	Dim first = True

	Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
		If first = True Then
			tmrLoop.Start()		 'start the loop
			first = False
		End If
		Select Case e.KeyCode
			Case Keys.Left
				Snake(0).Facing = 1
				determineMove(Time)
			Case Keys.Up
				Snake(0).Facing = 2
				determineMove(Time)
			Case Keys.Right
				Snake(0).Facing = 3
				determineMove(Time)
			Case Keys.Down
				Snake(0).Facing = 4
				determineMove(Time)
		End Select
		If (Time * 10) Mod (Speed * 10) = 0 Then		'reaches a new grid block
			For i = 0 To Snake.Length Step 1
				Snake(i + 1).Facing = Snake(i).Facing
			Next
		End If
	End Sub
	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Randomize()			 'randomize the random function
		Array.Resize(Snake, 1)  'sets the initial size of snake
		clear(0)				'set all values to nothing
		addBody(0, 165, 191)	'create the head at the center of the screen
	End Sub

	Private Sub addBody(ByVal Index, ByVal X, ByVal Y)
		Dummy.Image = My.Resources.body
		Dummy.Location = New System.Drawing.Point(X, Y)
		Controls.Add(Dummy)
		Array.Resize(Snake, Snake.Length + 1)
		clear(Snake.Length)
		Snake(Index).Body = Dummy
		Dummy = Nothing
	End Sub

	Private Sub clear(ByVal i)
		Snake(i).Body = Nothing
		Snake(i).Facing = Nothing
	End Sub
	Private Sub tmrLoop_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrLoop.Tick
		'time it takes to migrate from old to new square
		'very fast = 0.2 fast = 0.4 normal = 0.6 slow = 0.8 very slow = 1.0

		Time = Time + 0.01  'timer increment value
		determineMove(Time)
	End Sub

	Private Sub determineMove(ByVal Time)
		Select Case Time									'if time is less than 5 speed's
			Case Is < Speed * 500						   'speed-based second increments
				If (Time * 10) Mod (Speed * 10) = 0 Then	'checks if there's a need to add a new body part
					addBody(1, 165, 191)					'body parts , middle of screen, add 1 to length
					Return
				Else
					For i = 0 To Snake.Length Step 1
						moveSnake(i, Snake(i).Facing)
					Next
				End If
			Case Else
				For i = 0 To Snake.Length Step 1
					moveSnake(i, Snake(i).Facing)
				Next
		End Select
	End Sub

	Private Sub moveSnake(ByVal Index As Integer, ByVal Facing As Integer)
		Select Case Facing
			Case 1
				Snake(Index).Body.Location.Offset(-(Speed / 1), 0)			 'left
			Case 2
				Snake(Index).Body.Location.Offset(0, -(Speed / 1))			 'up
			Case 3
				Snake(Index).Body.Location.Offset((Speed / 1), 0)			  'right
			Case 4
				Snake(Index).Body.Location.Offset((Speed / 1), (Speed / 1))   'down
		End Select
	End Sub
End Class

module1:

VB.NET:
Module Module1
	Public Structure snake
		Dim Body As PictureBox
		Dim Facing As Integer
	End Structure
End Module

and on the form designer there's only a timer set to 1 millisecond increments
 
Back
Top