Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » Visual Basic (VB 4/5/6) » General Visual Basic » Use more keys than one at a time?

12 posts, Page 1 of 2. 12»»

Use more keys than one at a time?Expand / Collapse
Author
Message
Posted 5/8/2008 9:21:48 AM


Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 6/11/2008 2:28:02 AM
Posts: 33, Visits: 44
I am creating a game and i need to find a way to enable the user to use more than one keys at a time

The case is...

I hold down right and the "character" moves right, then i press space to shoot (while keep holding right) and the character shoots the "fire" BUT he stops moving

Also the same happens when I press Up for jump

The char stops moving to the right and only moves up (he should keep moving while on air, like games such as super mario bros)

These are parts of the commands I use:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyRight Then
player.Left = player.Left + 100
End If

If KeyCode = vbKeySpace Then
fire.Left = player.Left + 1000
fire.Top = player.Top + 700
fire.Visible = True
Timer1.Enabled = True
End If

end sub

Post #24847
Posted 5/13/2008 1:00:10 PM


Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 6/11/2008 2:28:02 AM
Posts: 33, Visits: 44
bump

Post #24903
Posted 5/13/2008 1:17:41 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 8:00:49 AM
Posts: 1,376, Visits: 3,098
Try putting a few DoEvents in the fire code.

If KeyCode = vbKeySpace Then
fire.Left = player.Left + 1000
fire.Top = player.Top + 700

DoEvents
fire.Visible = True
Timer1.Enabled = True
End If

Is fire and player just pictures then? It doesn't actually fire anything.


Keith

http://www.martin2k.co.uk/forums/

I've been programming with VB for 12 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
Post #24904
Posted 5/15/2008 6:32:42 AM


Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 6/11/2008 2:28:02 AM
Posts: 33, Visits: 44
yeah they're both pics

btw i dont see how the DoEvents is gonna solve anything

what I want to achieve is keep moving while shooting

While holding down 'right' if I press 'space', the 'right' key commands stop working (even though I'm still pressing it)

Post #24919
Posted 5/15/2008 10:44:51 AM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 6/11/2008 6:46:16 PM
Posts: 40, Visits: 244
Yea I'm having the same problem too.

I'm stilll trying to find a solution for it and if I do I will let you know so you can use it too.

I've been coding in VB6,PHP,HTML, and MYSQL but that don't make me the best coder around.Way I see it each of us are the best.

Post #24924
Posted 5/15/2008 11:05:55 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 8:00:49 AM
Posts: 1,376, Visits: 3,098
Panik (5/15/2008)
yeah they're both pics

btw i dont see how the DoEvents is gonna solve anything

DoEvents - Yields execution so that the operating system can process other events.

If you have one object moving and then you try to move another object at the same time then it probably won't. DoEvents will switch between objects/processes.

Have you tried it?

Keith

http://www.martin2k.co.uk/forums/

I've been programming with VB for 12 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
Post #24925
Posted 5/15/2008 11:25:15 AM
Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 6:57:34 PM
Posts: 872, Visits: 4,801
You can use the GetAsyncKeyState API. It can be used to catch 2 keys.

Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As LongAs Integer
Private Const VK_DOWN = &H28
Private Const VK_UP = &H26
Private Const VK_LEFT = &H25
Private Const VK_RIGHT = &H27
Private Const VK_SPACE = &H20

Private Sub Form_Load()
    Timer1.Interval = 100
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    If GetAsyncKeyState(VK_UP) <> 0 Then Picture1.Top = Picture1.Top - 30
    If GetAsyncKeyState(VK_DOWN) <> 0 Then Picture1.Top = Picture1.Top + 30
    If GetAsyncKeyState(VK_LEFT) <> 0 Then Picture1.Left = Picture1.Left - 30
    If GetAsyncKeyState(VK_RIGHT) <> 0 Then Picture1.Left = Picture1.Left + 30
    If GetAsyncKeyState(VK_SPACE) <> 0 Then Me.Caption = Val(Me.Caption) + 1
End Sub
Post #24926
Posted 5/15/2008 5:08:14 PM


Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 6/11/2008 2:28:02 AM
Posts: 33, Visits: 44
man you saved my life! that's just perfect!!

thanks Mark

Post #24931
Posted 5/16/2008 1:28:49 AM


Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 6/11/2008 2:28:02 AM
Posts: 33, Visits: 44
oh, one more thing

where can I find a list of the all the codes for the keys?

Post #24934
Posted 5/16/2008 7:13:05 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 8:00:49 AM
Posts: 1,376, Visits: 3,098
Panik (5/16/2008)

where can I find a list of the all the codes for the keys?

VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT and VK_SPACE you will find them in API Viewer under Constants in API Type combo.

Private Const VK_DOWN = &H28
Private Const VK_UP = &H26
Private Const VK_LEFT = &H25
Private Const VK_RIGHT = &H27
Private Const VK_SPACE = &H20

The normal VB keycodes you can find in VB6 help under keycode contants.

Do you have the MSDN help?

Do you have API Viewer installed?

Keith

http://www.martin2k.co.uk/forums/

I've been programming with VB for 12 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.