473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Keydown event in visual basic 6.0

i have tryied about everything to get a picture to move in code using visual basic 6.0 i need help i would like to move unit(man) to move while to screen is centered on the picture but i cant seem to get it to rea the key down and key left and key right and key up buttons on key board please help to get it to work
Feb 7 '08 #1
10 5429
kadghar
1,295 Expert 1GB
i have tryied about everything to get a picture to move in code using visual basic 6.0 i need help i would like to move unit(man) to move while to screen is centered on the picture but i cant seem to get it to rea the key down and key left and key right and key up buttons on key board please help to get it to work
have you tried with SELECT CASE and using VB constants in the KeyDown event?

e.g.
Expand|Select|Wrap|Line Numbers
  1. Select Case KeyCode
  2.     Case vbKeyDown
  3.         '[Code for moving down]
  4.     Case vbKeyUp
  5.         '[Code for moving up]
  6.     Case vbKeyLeft
  7.         '[Code for moving left]
  8.     Case vbKeyRight
  9.         '[Code for moving right]
  10. End Select
HTH
Feb 7 '08 #2
VACEPROGRAMER
168 100+
OK. Go to the form , enter in the form code , select "ONE KEY DOWN" in the left upper corner and then write this:

DIm Up as KeyCodeConstants
up = vbKeyUp

If KeyCode = Up Then
PictureBox.Top = PictureBox.Top + 25
End If

And you can use otther values (+25 ; +30 ; - 30 and . . . . ) and you can define other buttons. And the program is finished . . . .


*VACE*
Feb 7 '08 #3
ive tried that but it dosnt move the picture1 anywhere if i can get it to mave and still have the screen centered on the picture ht all i neeed i the code im trying is this







Expand|Select|Wrap|Line Numbers
  1.  If e.KeyCode = Keys.Left Then
  2.             CharacterLocation.Offset(-1, 0)
  3.         ElseIf e.KeyCode = Keys.Up Then
  4.             CharacterLocation.Offset(0, -1)
  5.         ElseIf e.KeyCode = Keys.Right Then
  6.             CharacterLocation.Offset(1, 0)
  7.         ElseIf e.KeyCode = Keys.Down Then
  8.             CharacterLocation.Offset(0, 1)
  9.         End If
  10.  


this is put into the keydown event but i wont move the picture anwhere
Feb 7 '08 #4
is their anyone who can help me please
Feb 8 '08 #5
Killer42
8,435 Expert 8TB
Don't be impatient. The people here are scattered around the world (in different time zones) and volunteering what time they can spare to help others. It can take a while to get a response.

A few thoughts...
  • Can you please explain what you mean about "still have the screen centred on the picture"? If you mean this the way it sounds, then you would need to move the picture, and move the form in the opposite direction. Or perhaps physically move the monitor, but that's certainly outside the scope of this discussion.
  • Since I'm not familiar with the version of VB you're using (I'm an old VB6 programmer) can you explain to me what CharacterLocation.Offset(0, 1) does? Does it actually change something (and is it the right "something?) or is it merely a function that returns a value (which you are ignoring)?
  • If you're moving your picture one "twip" at a time, it will be a long time before you see any noticeable difference.
  • Is the key detection working? At this point we don't know what is broken. Trace the execution and see whether it is going where you expect.
Feb 8 '08 #6
Im Sorry if im impatient im new to these online fourms and can be demmanding at times but all is good i forget the time to time the help people give out im trying not to sound rude but im sorry if i am im also new to programming and us to the real world fast pace get there get it and get back as quickly as posible and programming to me is like frogger with a 10 lane highway with cars going 75miles an hour ...... any who back to the topic all what the code does or ....... actully should do
is



I would like when i hit left keydown the picture moves left on the screen and when i hit right keydown picture moves right and up and down the samething


the charlocation doesnt mean anything if you have ever play Dragon warrior or any console game the char...... is always in the center of the screen no matter what thats what im trying to do hope that maks sence
Feb 8 '08 #7
Killer42
8,435 Expert 8TB
So you don't want to move the picture at all, then. You want it to stay put, and everything else to move around it.

I think that we need much more detail on how you're putting this stuff on the display, and what is supposed to move and what isn't.

So far it's like taking one headlight in to a mechanic and asking him what's wrong with your car. Not enough info to work on.

Also, I'd still like you to answer my earlier questions. This is standard debugging practice - identify the problem before you try to correct it. Is the problem in the detection of the key, or the adjustment of the whatever-it-is-you're-adjusting, or in drawing your display?
Feb 8 '08 #8
i dont mean to be bothersome but al this is in 2005 express sorry about this i was woundering why the code wouldnt work sorry to wast your time but ill be glad to give you the code im working on


Expand|Select|Wrap|Line Numbers
  1.  
  2. Imports System.IO
  3.  
  4. Public Class TileTest
  5.     Inherits System.Windows.Forms.Form
  6.  
  7. #Region " Windows Form Designer generated code "
  8.  
  9.     Public Sub New()
  10.         MyBase.New()
  11.  
  12.         'This call is required by the Windows Form Designer.
  13.         InitializeComponent()
  14.  
  15.         'Add any initialization after the InitializeComponent() call
  16.  
  17.     End Sub
  18.  
  19.     'Form overrides dispose to clean up the component list.
  20.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  21.         If disposing Then
  22.             If Not (components Is Nothing) Then
  23.                 components.Dispose()
  24.             End If
  25.         End If
  26.         MyBase.Dispose(disposing)
  27.     End Sub
  28.  
  29.     'Required by the Windows Form Designer
  30.     Private components As System.ComponentModel.IContainer
  31.  
  32.     'NOTE: The following procedure is required by the Windows Form Designer
  33.     'It can be modified using the Windows Form Designer.  
  34.     'Do not modify it using the code editor.
  35.     Friend WithEvents Clock As System.Windows.Forms.Timer
  36.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  37.         Me.components = New System.ComponentModel.Container
  38.         Me.Clock = New System.Windows.Forms.Timer(Me.components)
  39.         '
  40.         'Clock
  41.         '
  42.         '
  43.         'TileTest
  44.         '
  45.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  46.         Me.ClientSize = New System.Drawing.Size(440, 469)
  47.         Me.Name = "TileTest"
  48.         Me.Text = "Tile Map"
  49.  
  50.     End Sub
  51.  
  52. #End Region
  53.  
  54.     Dim Tileset As Bitmap    'Holds the tile pictures.
  55.  
  56.     Dim Backup As Bitmap     'Backup picture holds the map that we've just drawn.
  57.     Dim GFX As Graphics     'Draws to the backup picture.
  58.  
  59.     Dim FormGFX As Graphics     'Draws to the form.
  60.  
  61.     Dim Map(,) As Integer       'The map is a 2D array of integers.
  62.  
  63.     Dim Charpics(3) As Bitmap        'Hold each frame in one of the array elements.
  64.     Dim Cyc As Integer                  'This number tells which frame to draw.
  65.  
  66.     Dim CharacterLocation As Point
  67.  
  68.     Const Tilesize As Integer = 143
  69.     Const NumofTilesBetweenCharacterAndScreenEdge As Integer = 3
  70.  
  71.  
  72.     Private Sub TileTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  73.  
  74.  
  75.         Tileset = New Bitmap("tileset.bmp")     'Put the bitmap into the tileset picture.
  76.  
  77.         Backup = New Bitmap(143 * 7, 143 * 7)           'Backup will be initialzed to an empty bitmap
  78.  
  79.         GFX = Graphics.FromImage(Backup)        'GFX draws onto the backup picture.
  80.         FormGFX = Me.CreateGraphics()           'Draw to the form.
  81.  
  82.         Dim SR As StreamReader = New StreamReader("map.txt")
  83.         Dim Ln As String
  84.         Dim LX, LY As Integer
  85.         Dim Chs As String
  86.         'The map in the file is now 24x24, so the array must be initialized as:
  87.  
  88.         Map = New Integer(9, 9) {}
  89.  
  90.         For LY = 0 To 9
  91.             Ln = SR.ReadLine()  'One row of tiles from the file.
  92.  
  93.             For LX = 0 To 9    'Goes through each column.
  94.  
  95.                 Chs = Ln.Substring(LX, 1)   'Get a string with one character in it.
  96.  
  97.                 Map(LX, LY) = Integer.Parse(Chs)  'Convert this string into its matching number.
  98.                 '"0" -> 0, "1" -> 1 ...
  99.                 'The number is put into the map.
  100.  
  101.             Next
  102.  
  103.         Next
  104.  
  105.         SR.Close()
  106.  
  107.         CharacterLocation = New Point(3, 3)
  108.  
  109.         Charpics(0) = New Bitmap("Shack.gif")
  110.         Charpics(1) = New Bitmap("Shack2.gif")
  111.         Charpics(2) = New Bitmap("Shack3.gif")
  112.         Charpics(3) = New Bitmap("Shack4.gif")
  113.         'Load each frame one by one into the array.
  114.  
  115.  
  116.  
  117.         'Allow the keypresses to go directly to the form.
  118.         Me.KeyPreview = True
  119.         'Enable the clock.
  120.         Clock.Start()
  121.  
  122.         Draw()      'Draw the map.
  123.  
  124.     End Sub
  125.  
  126.     Private Sub Draw()  'Draw map to the backup.
  127.  
  128.         'The tileset contains 8 tiles, where each tile is 16 pixels wide.
  129.         'The map is 8x8 tilewise.
  130.  
  131.         'Drawing the map requires nested for loops.
  132.  
  133.         Dim LX, LY As Integer
  134.         Dim Thisindex As Integer        'The index of the map at the point LX,LY.
  135.         Dim Tile As Rectangle    'Holds the rectangle defining which tile we will draw.
  136.  
  137.         Dim CamX, CamY As Integer
  138.  
  139.         'Preface: The character will be 3 tiles away from both edges of the display.
  140.         'Therefore the width of the display will be 2 * 3 + 1... that's 7 tiles.
  141.         'The same applies to the vertical axis.
  142.         'The camera represents the tile that will be in the upper left corner of the screen.
  143.         If CharacterLocation.X - NumofTilesBetweenCharacterAndScreenEdge < 0 Then
  144.             CamX = 0  'This keeps the camera from going off of the left edge of the map.
  145.         ElseIf CharacterLocation.X + NumofTilesBetweenCharacterAndScreenEdge >= Map.GetUpperBound(0) Then
  146.             CamX = Map.GetUpperBound(0) - 2 * NumofTilesBetweenCharacterAndScreenEdge
  147.             'Same but for the right edge of the map.
  148.         Else
  149.             'The usual... camera remains 3 tiles away from the character, until the character moves near the edge of the map.
  150.             CamX = CharacterLocation.X - NumofTilesBetweenCharacterAndScreenEdge
  151.         End If
  152.         If CharacterLocation.Y - NumofTilesBetweenCharacterAndScreenEdge < 0 Then
  153.             CamY = 0
  154.         ElseIf CharacterLocation.Y + NumofTilesBetweenCharacterAndScreenEdge >= Map.GetUpperBound(1) Then
  155.             CamY = Map.GetUpperBound(1) - 2 * NumofTilesBetweenCharacterAndScreenEdge
  156.         Else
  157.             CamY = CharacterLocation.Y - NumofTilesBetweenCharacterAndScreenEdge
  158.         End If
  159.  
  160.         For LY = 0 To NumofTilesBetweenCharacterAndScreenEdge + NumofTilesBetweenCharacterAndScreenEdge
  161.             For LX = 0 To NumofTilesBetweenCharacterAndScreenEdge + NumofTilesBetweenCharacterAndScreenEdge
  162.  
  163.                 Thisindex = Map(CamX + LX, CamY + LY)
  164.                 'Get the index from the map.
  165.                 'Since the camera changes, we need to extract the tiles from a different location in the map.
  166.                 'Everything else is drawing the tile we give it, so it doesn't change.
  167.  
  168.                 Tile = New Rectangle(Thisindex * Tilesize, 0, Tilesize, Tilesize)
  169.                 'Get the rectangle defining the tile.
  170.  
  171.                 GFX.DrawImage(Tileset, LX * Tilesize, LY * Tilesize, Tile, GraphicsUnit.Pixel)
  172.                 'And then draw the tile onto the backup.
  173.  
  174.             Next
  175.         Next
  176.  
  177.  
  178.         'Map code.
  179.         GFX.DrawImage(Charpics(Cyc), (CharacterLocation.X - CamX) * 143, (CharacterLocation.Y - CamY) * 143)
  180.         'Draw the character to the display at the location... this also changes depending on the camera location, so
  181.         'the camera location is subtracted from the character location.
  182.  
  183.         'At this point the whole map should reside on the backup bitmap.  The bitmap is not visible, so it must be made visible.
  184.         FormGFX.DrawImage(Backup, Me.ClientRectangle())
  185.  
  186.  
  187.  
  188.     End Sub
  189.  
  190.  
  191.  
  192.     Private Sub Clock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clock.Tick
  193.         Cyc = (Cyc + 1) Mod 4   'Cyc cycles through 0, 1, 2, 3, 0, 1, 2, 3, 0, 1 ...
  194.         Draw()      'Draw.
  195.     End Sub
  196.  
  197.     Private Sub TileTest_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  198.         'FormGFX.SetClip(Me.ClientRectangle)
  199.         FormGFX.DrawImage(Backup, Me.ClientRectangle())
  200.  
  201.     End Sub
  202.  
  203.     Private Sub TileTest_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
  204.  
  205.         Dim Collided As Boolean
  206.  
  207.         'This is the basic movement.  Collision detection always comes after this movement caused by the user.
  208.         If e.KeyCode = Keys.Left Then
  209.             CharacterLocation.Offset(-1, 0)
  210.         ElseIf e.KeyCode = Keys.Up Then
  211.             CharacterLocation.Offset(0, -1)
  212.         ElseIf e.KeyCode = Keys.Right Then
  213.             CharacterLocation.Offset(1, 0)
  214.         ElseIf e.KeyCode = Keys.Down Then
  215.             CharacterLocation.Offset(0, 1)
  216.         End If
  217.  
  218.         'Check for collisions after moving the character.
  219.         If CharacterLocation.X < 0 OrElse CharacterLocation.X > Map.GetUpperBound(0) OrElse CharacterLocation.Y < 0 OrElse CharacterLocation.Y > Map.GetUpperBound(1) Then
  220.             Collided = True
  221.         ElseIf Map(CharacterLocation.X, CharacterLocation.Y) = 9 Then
  222.             Collided = True
  223.  
  224.         End If
  225.  
  226.         'If we have collded, then the character needs to move back immediately.
  227.         If Collided Then
  228.             'Take all of the keycodes and reverse their operation.
  229.             If e.KeyCode = Keys.Left Then
  230.                 CharacterLocation.Offset(1, 0)
  231.             ElseIf e.KeyCode = Keys.Up Then
  232.                 CharacterLocation.Offset(0, 1)
  233.             ElseIf e.KeyCode = Keys.Right Then
  234.                 CharacterLocation.Offset(-1, 0)
  235.             ElseIf e.KeyCode = Keys.Down Then
  236.                 CharacterLocation.Offset(0, -1)
  237.             End If
  238.  
  239.         End If
  240.  
  241.  
  242.  
  243.  
  244.  
  245.         'Check for collisions after moving the character.
  246.         If CharacterLocation.X < 0 OrElse CharacterLocation.X > Map.GetUpperBound(0) OrElse CharacterLocation.Y < 0 OrElse CharacterLocation.Y > Map.GetUpperBound(1) Then
  247.             Collided = True
  248.         ElseIf Map(CharacterLocation.X, CharacterLocation.Y) = 9 Then
  249.             Collided = True
  250.  
  251.         End If
  252.  
  253.         'Note that the keydown event can fire more than once over the 100 ms that the timer is not drawing.
  254.     End Sub
  255. End Class
  256.  
  257.  
  258.  
  259.  
Feb 8 '08 #9
wow thats some serious code guys! :D
im in year 10 and iv been playin around with vb

im using vb2008 but this is how i move a picture box:

picturebox1.left = picturebox1.left + 5
picturebox1.top = picturebox1.top +5

i dont know if thats a help but i thought i may as well pitch in and give it a shot :D
Feb 8 '08 #10
Killer42
8,435 Expert 8TB
wow thats some serious code guys! :D
im in year 10 and iv been playin around with vb
Thanks for that vincey92.

I'm not sure it is particularly helpful in this case, but don't let that discourage you. We definitely need people to pitch in. There are too few who bother. This sort of forum works best as a discussion where everyone contributes what they can.
Feb 12 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: bardo | last post by:
I have a Datagrid that is inside a panel. I want to use the keyDown event to reconize the arrow keys. But I have no luck at all. The problem is that the keydown event won't fire at all, unless I...
4
by: Tony | last post by:
I'm building a simple "breakout" style video-game, and I want keyboard control of the paddle. What I need is for an event handler to respond whenever a key is pressed, regardless of what the...
2
by: Rasika WIJAYARATNE | last post by:
I have a form with a KeyDown event handler method attached to it. It has three text boxes and two buttons (buttons are attached to event handler). However when I debug, the form opens with the...
4
by: Anne | last post by:
hie again, i have 3 textbox and i would like the user to go to the next textbox by pressing the 'ENTER' key. i have tried using this: Private Sub txtRequestor_KeyDown(ByVal sender As...
1
by: fripper | last post by:
I have a VB 2005 windows app and I want to recognize keydown events. I have a form key down event handler but it does not get control when a key is depressed. In playing around I found that if I...
4
by: velasquez.m | last post by:
i'm working with visual studio 2005 and have a solution with 16 projects in it. this specific application has a base form with other forms added to it at any area of the application that the...
3
by: MLM450 | last post by:
I have a control that handles the KeyDown event but it does not seem to execute when a combination of keys is pressed - like CTRL+Z. If I press CTRL, it executes. If I press Z, it executes. But the...
3
by: win | last post by:
when the cursor is in a textbox, only coding in the keydown event of the textbox triggered, the coding in the keydown event of the form does not triggered! Problem: I need to change a VB6 program...
2
by: sush_jd via DotNetMonster.com | last post by:
I am using managed VC++ code in a Win Form App. There is a text box - txtRONumber. I have defined a KeyDown event handler (non-default) for it, like below. InitializeComponent() is being called...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.