Keydown event in visual basic 6.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dracitus
    New Member
    • Feb 2008
    • 7

    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
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by Dracitus
    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.
    [CODE=vb]Select Case KeyCode
    Case vbKeyDown
    '[Code for moving down]
    Case vbKeyUp
    '[Code for moving up]
    Case vbKeyLeft
    '[Code for moving left]
    Case vbKeyRight
    '[Code for moving right]
    End Select[/CODE]

    HTH

    Comment

    • VACEPROGRAMER
      Banned
      New Member
      • Nov 2007
      • 167

      #3
      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 KeyCodeConstant s
      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*

      Comment

      • Dracitus
        New Member
        • Feb 2008
        • 7

        #4
        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







        Code:
         If e.KeyCode = Keys.Left Then
                    CharacterLocation.Offset(-1, 0)
                ElseIf e.KeyCode = Keys.Up Then
                    CharacterLocation.Offset(0, -1)
                ElseIf e.KeyCode = Keys.Right Then
                    CharacterLocation.Offset(1, 0)
                ElseIf e.KeyCode = Keys.Down Then
                    CharacterLocation.Offset(0, 1)
                End If


        this is put into the keydown event but i wont move the picture anwhere

        Comment

        • Dracitus
          New Member
          • Feb 2008
          • 7

          #5
          is their anyone who can help me please

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            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 CharacterLocati on.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.

            Comment

            • Dracitus
              New Member
              • Feb 2008
              • 7

              #7
              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

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                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?

                Comment

                • Dracitus
                  New Member
                  • Feb 2008
                  • 7

                  #9
                  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


                  Code:
                  Imports System.IO
                  
                  Public Class TileTest
                      Inherits System.Windows.Forms.Form
                  
                  #Region " Windows Form Designer generated code "
                  
                      Public Sub New()
                          MyBase.New()
                  
                          'This call is required by the Windows Form Designer.
                          InitializeComponent()
                  
                          'Add any initialization after the InitializeComponent() call
                  
                      End Sub
                  
                      'Form overrides dispose to clean up the component list.
                      Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                          If disposing Then
                              If Not (components Is Nothing) Then
                                  components.Dispose()
                              End If
                          End If
                          MyBase.Dispose(disposing)
                      End Sub
                  
                      'Required by the Windows Form Designer
                      Private components As System.ComponentModel.IContainer
                  
                      'NOTE: The following procedure is required by the Windows Form Designer
                      'It can be modified using the Windows Form Designer.  
                      'Do not modify it using the code editor.
                      Friend WithEvents Clock As System.Windows.Forms.Timer
                      <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
                          Me.components = New System.ComponentModel.Container
                          Me.Clock = New System.Windows.Forms.Timer(Me.components)
                          '
                          'Clock
                          '
                          '
                          'TileTest
                          '
                          Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
                          Me.ClientSize = New System.Drawing.Size(440, 469)
                          Me.Name = "TileTest"
                          Me.Text = "Tile Map"
                  
                      End Sub
                  
                  #End Region
                  
                      Dim Tileset As Bitmap    'Holds the tile pictures.
                  
                      Dim Backup As Bitmap     'Backup picture holds the map that we've just drawn.
                      Dim GFX As Graphics     'Draws to the backup picture.
                  
                      Dim FormGFX As Graphics     'Draws to the form.
                  
                      Dim Map(,) As Integer       'The map is a 2D array of integers.
                  
                      Dim Charpics(3) As Bitmap        'Hold each frame in one of the array elements.
                      Dim Cyc As Integer                  'This number tells which frame to draw.
                  
                      Dim CharacterLocation As Point
                  
                      Const Tilesize As Integer = 143
                      Const NumofTilesBetweenCharacterAndScreenEdge As Integer = 3
                  
                  
                      Private Sub TileTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                  
                  
                          Tileset = New Bitmap("tileset.bmp")     'Put the bitmap into the tileset picture.
                  
                          Backup = New Bitmap(143 * 7, 143 * 7)           'Backup will be initialzed to an empty bitmap
                  
                          GFX = Graphics.FromImage(Backup)        'GFX draws onto the backup picture.
                          FormGFX = Me.CreateGraphics()           'Draw to the form.
                  
                          Dim SR As StreamReader = New StreamReader("map.txt")
                          Dim Ln As String
                          Dim LX, LY As Integer
                          Dim Chs As String
                          'The map in the file is now 24x24, so the array must be initialized as:
                  
                          Map = New Integer(9, 9) {}
                  
                          For LY = 0 To 9
                              Ln = SR.ReadLine()  'One row of tiles from the file.
                  
                              For LX = 0 To 9    'Goes through each column.
                  
                                  Chs = Ln.Substring(LX, 1)   'Get a string with one character in it.
                  
                                  Map(LX, LY) = Integer.Parse(Chs)  'Convert this string into its matching number.
                                  '"0" -> 0, "1" -> 1 ...
                                  'The number is put into the map.
                  
                              Next
                  
                          Next
                  
                          SR.Close()
                  
                          CharacterLocation = New Point(3, 3)
                  
                          Charpics(0) = New Bitmap("Shack.gif")
                          Charpics(1) = New Bitmap("Shack2.gif")
                          Charpics(2) = New Bitmap("Shack3.gif")
                          Charpics(3) = New Bitmap("Shack4.gif")
                          'Load each frame one by one into the array.
                  
                  
                  
                          'Allow the keypresses to go directly to the form.
                          Me.KeyPreview = True
                          'Enable the clock.
                          Clock.Start()
                  
                          Draw()      'Draw the map.
                  
                      End Sub
                  
                      Private Sub Draw()  'Draw map to the backup.
                  
                          'The tileset contains 8 tiles, where each tile is 16 pixels wide.
                          'The map is 8x8 tilewise.
                  
                          'Drawing the map requires nested for loops.
                  
                          Dim LX, LY As Integer
                          Dim Thisindex As Integer        'The index of the map at the point LX,LY.
                          Dim Tile As Rectangle    'Holds the rectangle defining which tile we will draw.
                  
                          Dim CamX, CamY As Integer
                  
                          'Preface: The character will be 3 tiles away from both edges of the display.
                          'Therefore the width of the display will be 2 * 3 + 1... that's 7 tiles.
                          'The same applies to the vertical axis.
                          'The camera represents the tile that will be in the upper left corner of the screen.
                          If CharacterLocation.X - NumofTilesBetweenCharacterAndScreenEdge < 0 Then
                              CamX = 0  'This keeps the camera from going off of the left edge of the map.
                          ElseIf CharacterLocation.X + NumofTilesBetweenCharacterAndScreenEdge >= Map.GetUpperBound(0) Then
                              CamX = Map.GetUpperBound(0) - 2 * NumofTilesBetweenCharacterAndScreenEdge
                              'Same but for the right edge of the map.
                          Else
                              'The usual... camera remains 3 tiles away from the character, until the character moves near the edge of the map.
                              CamX = CharacterLocation.X - NumofTilesBetweenCharacterAndScreenEdge
                          End If
                          If CharacterLocation.Y - NumofTilesBetweenCharacterAndScreenEdge < 0 Then
                              CamY = 0
                          ElseIf CharacterLocation.Y + NumofTilesBetweenCharacterAndScreenEdge >= Map.GetUpperBound(1) Then
                              CamY = Map.GetUpperBound(1) - 2 * NumofTilesBetweenCharacterAndScreenEdge
                          Else
                              CamY = CharacterLocation.Y - NumofTilesBetweenCharacterAndScreenEdge
                          End If
                  
                          For LY = 0 To NumofTilesBetweenCharacterAndScreenEdge + NumofTilesBetweenCharacterAndScreenEdge
                              For LX = 0 To NumofTilesBetweenCharacterAndScreenEdge + NumofTilesBetweenCharacterAndScreenEdge
                  
                                  Thisindex = Map(CamX + LX, CamY + LY)
                                  'Get the index from the map.
                                  'Since the camera changes, we need to extract the tiles from a different location in the map.
                                  'Everything else is drawing the tile we give it, so it doesn't change.
                  
                                  Tile = New Rectangle(Thisindex * Tilesize, 0, Tilesize, Tilesize)
                                  'Get the rectangle defining the tile.
                  
                                  GFX.DrawImage(Tileset, LX * Tilesize, LY * Tilesize, Tile, GraphicsUnit.Pixel)
                                  'And then draw the tile onto the backup.
                  
                              Next
                          Next
                  
                  
                          'Map code.
                          GFX.DrawImage(Charpics(Cyc), (CharacterLocation.X - CamX) * 143, (CharacterLocation.Y - CamY) * 143)
                          'Draw the character to the display at the location... this also changes depending on the camera location, so
                          'the camera location is subtracted from the character location.
                  
                          'At this point the whole map should reside on the backup bitmap.  The bitmap is not visible, so it must be made visible.
                          FormGFX.DrawImage(Backup, Me.ClientRectangle())
                  
                  
                  
                      End Sub
                  
                  
                  
                      Private Sub Clock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clock.Tick
                          Cyc = (Cyc + 1) Mod 4   'Cyc cycles through 0, 1, 2, 3, 0, 1, 2, 3, 0, 1 ...
                          Draw()      'Draw.
                      End Sub
                  
                      Private Sub TileTest_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
                          'FormGFX.SetClip(Me.ClientRectangle)
                          FormGFX.DrawImage(Backup, Me.ClientRectangle())
                  
                      End Sub
                  
                      Private Sub TileTest_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
                  
                          Dim Collided As Boolean
                  
                          'This is the basic movement.  Collision detection always comes after this movement caused by the user.
                          If e.KeyCode = Keys.Left Then
                              CharacterLocation.Offset(-1, 0)
                          ElseIf e.KeyCode = Keys.Up Then
                              CharacterLocation.Offset(0, -1)
                          ElseIf e.KeyCode = Keys.Right Then
                              CharacterLocation.Offset(1, 0)
                          ElseIf e.KeyCode = Keys.Down Then
                              CharacterLocation.Offset(0, 1)
                          End If
                  
                          'Check for collisions after moving the character.
                          If CharacterLocation.X < 0 OrElse CharacterLocation.X > Map.GetUpperBound(0) OrElse CharacterLocation.Y < 0 OrElse CharacterLocation.Y > Map.GetUpperBound(1) Then
                              Collided = True
                          ElseIf Map(CharacterLocation.X, CharacterLocation.Y) = 9 Then
                              Collided = True
                  
                          End If
                  
                          'If we have collded, then the character needs to move back immediately.
                          If Collided Then
                              'Take all of the keycodes and reverse their operation.
                              If e.KeyCode = Keys.Left Then
                                  CharacterLocation.Offset(1, 0)
                              ElseIf e.KeyCode = Keys.Up Then
                                  CharacterLocation.Offset(0, 1)
                              ElseIf e.KeyCode = Keys.Right Then
                                  CharacterLocation.Offset(-1, 0)
                              ElseIf e.KeyCode = Keys.Down Then
                                  CharacterLocation.Offset(0, -1)
                              End If
                  
                          End If
                  
                  
                  
                  
                  
                          'Check for collisions after moving the character.
                          If CharacterLocation.X < 0 OrElse CharacterLocation.X > Map.GetUpperBound(0) OrElse CharacterLocation.Y < 0 OrElse CharacterLocation.Y > Map.GetUpperBound(1) Then
                              Collided = True
                          ElseIf Map(CharacterLocation.X, CharacterLocation.Y) = 9 Then
                              Collided = True
                  
                          End If
                  
                          'Note that the keydown event can fire more than once over the 100 ms that the timer is not drawing.
                      End Sub
                  End Class

                  Comment

                  • vincey92
                    New Member
                    • Feb 2008
                    • 11

                    #10
                    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.lef t = picturebox1.lef t + 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

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by vincey92
                      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.

                      Comment

                      Working...