KeyDown different in VS 2005?

epicglottis

New member
Joined
Apr 2, 2005
Messages
2
Programming Experience
1-3
Hello, I discovered the free beta version of VB express 2005 and am trying to become familiar with VS .NET by developing a simple tetris clone.

I have had no problems until I tried to process keyboard input.

All the googling I have done result in the same code which does not work.
(i.e. if e.KeyCode == Keys.Right Then ...)

My question is this: is there a property which enables all the keys to be processed at a form level, or am I not handling the keydown event properly, or something else?
(Short: Why doesn't the commented code work?)

Here is my current code:

VB.NET:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
		Select Case e.KeyCode
			Case Keys.D	 'case Keys.Right
				CurrentBlock.Right()
			Case Keys.A	 'Case Keys.Left
				CurrentBlock.Left()
			Case Keys.W	 'Case Keys.Up
				CurrentBlock.Rotate()
			Case Keys.S	 'Case Keys.Down
				CurrentBlock.Down()
		End Select
	End Sub
Note: oddly enough all the keys work except the arrow keys and enter when I enable the "Key Preview" form property. (the code below works, however I would like to use the commented code instead)

-Thanks in advance!
 
Last edited:

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
on the form, make sure the KeyPreview property is set to True (default is False) and then:

VB.NET:
Select Case Asc(e.keyCode)
  Case Keys.A
-or-

VB.NET:
 Select Case e.keyCode
  Case "a", "A"
would work
 

epicglottis

New member
Joined
Apr 2, 2005
Messages
2
Programming Experience
1-3
Good try

Thanks for the quick reply.

Good idea, I hadn't thought of casting to ascii...but that still didn't work for the arrow keys, nor enter.

Playing with this code, I have noticed that the KeyDown event handler function is not even being called when the arrow keys or enter or esc are pressed. Is there another way to detect these keys being pressed?


VB.NET:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
		Select Case asc(e.KeyCode)
			case Keys.Right
				CurrentBlock.Right()
			Case Keys.Left
				CurrentBlock.Left()
			Case Keys.Up
				CurrentBlock.Rotate()
			Case Keys.Down
				CurrentBlock.Down()
		End Select
	End Sub
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
try using the KeyPress event then and check for:

Asc(e.KeyChar) = Keys.Enter
Asc(e.KeyChar) = Keys.Left
Asc(e.KeyChar) = Keys.Up
Asc(e.KeyChar) = Keys.Right
Asc(e.KeyChar) = Keys.Down

or the KeyUp event and use:

e.KeyCode = 13 'Enter Key

i dont know the keycode for the arrow keys though
 
Top Bottom