Visual Basic Express: how to capture the arrow keys - basic.visual
This is a discussion on Visual Basic Express: how to capture the arrow keys - basic.visual ; in VB Express all (!) I want to do is capture & test the key pressed..
and if its a 'arrow' key (up,down,right, left) move an object on the
screen
So I was hoping to do siomething like the following
...
-
Visual Basic Express: how to capture the arrow keys
in VB Express all (!) I want to do is capture & test the key pressed..
and if its a 'arrow' key (up,down,right, left) move an object on the
screen
So I was hoping to do siomething like the following
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = Microsoft.VisualBasic.Chr(72) Then
MsgBox("It works!")
End If
End Sub
but this does nothing (its never called for an arrow key at all.. any
ideas what I should be calling to do this ???
-
Re: Visual Basic Express: how to capture the arrow keys
peterbailey50@aol.com wrote:
> in VB Express all (!) I want to do is capture & test the key pressed..
> and if its a 'arrow' key (up,down,right, left) move an object on the
> screen
>
> So I was hoping to do siomething like the following
>
>
>
> Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As
> System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
>
> If e.KeyChar = Microsoft.VisualBasic.Chr(72) Then
> MsgBox("It works!")
> End If
>
> End Sub
>
> but this does nothing (its never called for an arrow key at all.. any
> ideas what I should be calling to do this ???
Can't say for certain if this works in VBE (Isn't that a .NET derivative,
this group is for VB "Classic", pre dot net, VB6 and earlier)
If the form has a KeyPreview property set it to True. That will let you
examine the key strokes before VB performs the windows defined action on
them.
Hope this helps, though I'm not cetrain it will.
Best,
Bill
-
Re: Visual Basic Express: how to capture the arrow keys
Look for the KeyDown instead of the KeyPress.
On Mar 8, 4:54 pm, peterbaile...@aol.com wrote:
> in VB Express all (!) I want to do is capture & test the key pressed..
> and if its a 'arrow' key (up,down,right, left) move an object on the
> screen
>
> So I was hoping to do siomething like the following
>
> Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As
> System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
>
> If e.KeyChar = Microsoft.VisualBasic.Chr(72) Then
> MsgBox("It works!")
> End If
>
> End Sub
>
> but this does nothing (its never called for an arrow key at all.. any
> ideas what I should be calling to do this ???
-
Re: Visual Basic Express: how to capture the arrow keys
Hello,
Here's my solution to the arrow key thing:
Important:
1. Set Form1 property "KeyPreview" to "True". This will allow the form to check what key the user pressed.
2. If you're using this exact code, rename (if needed) your form to "Form1" and the picturebox to "picImage" (you can change this to any other object on your form).
3. I did not copy-paste the following piece of code, I wrote it directly in this message, there might be some syntax errors or spelling mistakes.
4. Every line starting with ' is a comment line, this is to explain what action is performed in the according line of code.
5. Use this well and share it with as many people as possible, I've been trying to solve this problem for a few months and I want to prevent people from having to go through the same thing 
Private Sub ... Handles Form1.KeyPress
Dim pntLocation As Point = picImage.Location
'Store the location of picturebox picImage in pntLocation, declared as point.
'Change "picImage" to the name of the object you want to move if you're not using a picturebox named "picImage".
Select Case e.KeyChar
'Question the char of the pressed key
Case ChrW(38)
'ChrW(38) represents the UP key.
pntLocation.Y -= 1
'Decrease the location's Y value by 1, move up.
Case ChrW(40)
'ChrW(40) reporesents the DOWN key.
pntLocation.Y += 1
'Increase the location's Y value by 1, move down.
Case ChrW(37)
'ChrW(37) represents the LEFT key.
pntLocation.X -= 1
'Decrease the location's X value by 1, move left.
Case ChrW(39)
'ChrW(39) represents the RIGHT key.
pntLocation.X += 1
'Increase the location's X value by 1, move right.
End Select
'Close the questioning.
picImage.Location = pntLocation
'Store the value of pntLocation, which we changed in this Sub, in the location of picImage. This makes the picturebox move.
'Change "picImage" to the name of the object you want to move if you're not using a picturebox named "picImage".
End Sub
==========================================================
HAPPY PROGRAMMING
Similar Threads
-
By Application Development in forum ADO DAO RDO RDS
Replies: 1
Last Post: 10-16-2007, 01:52 PM
-
By Application Development in forum DOTNET
Replies: 2
Last Post: 10-04-2007, 07:52 AM
-
By Application Development in forum DOTNET
Replies: 1
Last Post: 10-30-2006, 10:10 AM
-
By Application Development in forum Smalltalk
Replies: 2
Last Post: 10-09-2006, 09:47 PM
-
By Application Development in forum basic.visual
Replies: 3
Last Post: 12-19-2005, 09:34 AM