Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,190 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,154 people online right now. Registration is fast and FREE... Join Now!




Help with KeyUp & KeyDown

 
Reply to this topicStart new topic

Help with KeyUp & KeyDown, i am an ambitious newbie, with big probl

rabbit_slayer
30 Sep, 2007 - 07:15 AM
Post #1

D.I.C Head
**

Joined: 30 Sep, 2007
Posts: 66


My Contributions
plz help me some ne with the darn key down and key up.
i am trying to make a game in VB and i need to know everything about key down and key up.
i need them to activate some "animation timers" when i hit up/down/right/left like so:

CODE

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            timer1.Enabled = True
End Sub


HELP blink.gif


btw, if any of u want my game it will be done by ianuary 2008, if u guys help me with this, 40% of the work is done:D

*Always use code blocks, like it says in the textarea you posted this topic in smile.gif

This post has been edited by PsychoCoder: 30 Sep, 2007 - 07:36 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Help With KeyUp & KeyDown
30 Sep, 2007 - 07:43 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
What is the problem with that code, are you getting an error? What's it doing compared to what you want done? I just added this code to a VB6 Application and I got my messages with both vbKeyUp and vbKeyDown, in both the KeyUp and KeyDown event of the form:

CODE

'KeyUp Event
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            MsgBox ("KeyUp in Select Case!")
        Case vbKeyDown
            MsgBox ("KeyDown in Select Case!")
    End Select
End Sub

'KeyDown Event
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            MsgBox ("KeyUp in Select Case!")
        Case vbKeyDown
            MsgBox ("KeyDown in Select Case!")
    End Select
End Sub


So whats the problem you're having?
User is offlineProfile CardPM
+Quote Post

rabbit_slayer
RE: Help With KeyUp & KeyDown
30 Sep, 2007 - 11:44 PM
Post #3

D.I.C Head
**

Joined: 30 Sep, 2007
Posts: 66


My Contributions
THANK A LOT, i don't know what i was doing wrong....anyway thanks again u saved me:D
User is offlineProfile CardPM
+Quote Post

rabbit_slayer
RE: Help With KeyUp & KeyDown
1 Oct, 2007 - 12:03 AM
Post #4

D.I.C Head
**

Joined: 30 Sep, 2007
Posts: 66


My Contributions
ok, my happiness was 4 a short while, if i put a button in the form, the key up and key down events stop working, i am trying to move that button through the key up and key donw events...like so
CODE

'KeyUp Event
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            Timer1.Enabled = False
        Case vbKeyDown
            Timer1.Enabled = True
    End Select
End Sub

'KeyDown Event
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            Timer2.Enabled = False
        Case vbKeyDown
            Timer2.Enabled = True
    End Select
End Sub

Private Sub Timer1_Timer()
    Command1.Top = Command1.Top + 20
End Sub

Private Sub Timer2_Timer()
    Command1.Top = Command1.Top - 20
End Sub


This post has been edited by rabbit_slayer: 1 Oct, 2007 - 12:04 AM
User is offlineProfile CardPM
+Quote Post

rabbit_slayer
RE: Help With KeyUp & KeyDown
1 Oct, 2007 - 10:40 PM
Post #5

D.I.C Head
**

Joined: 30 Sep, 2007
Posts: 66


My Contributions
ok, i kinda figured out the whole thing, but it doesn't work and i don't understand why...
look:
CODE

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            Text1.Text = "UP"
        Case vbKeyDown
            Text1.Text = "DOWN"
        Case vbKeyLeft
            Text1.Text = "LEFT"
        Case vbKeyRight
            Text1.Text = "RIGHT"
    End Select
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            Text1.Text = "neutral"
        Case vbKeyDown
            Text1.Text = "neutral"
        Case vbKeyLeft
            Text1.Text = "neutral"
        Case vbKeyRight
            Text1.Text = "neutral"
    End Select
End Sub

Private Sub Timer1_Timer()
   cmd1.Top = cmd1.Top - 20
End Sub

Private Sub Timer2_Timer()
    cmd1.Top = cmd1.Top + 20
End Sub

Private Sub Timer3_Timer()
    cmd1.Left = cmd1.Left - 20
End Sub

Private Sub Timer4_Timer()
    cmd1.Top = cmd1.Top + 20
End Sub

Private Sub Timer5_Timer()
    If Text1.Text = "UP" Then
    Timer1.Enabled = True
    
    If Text1.Text = "DOWN" Then
    Timer2.Enabled = True
    
    If Text1.Text = "LEFT" Then
    Timer3.Enabled = True
    
    If Text1.Text = "RIGHT" Then
    Timer4.Enabled = True
    
    If Text1.Text = "neutral" Then
    Timer1.Enabled = False
    If Text1.Text = "neutral" Then
    Timer2.Enabled = False
    If Text1.Text = "neutral" Then
    Timer3.Enabled = False
    If Text1.Text = "neutral" Then
    Timer4.Enabled = False
End If
End If
End If
End If
End If
End If
End If
End If

End Sub



it only works for vbkeyup...left, right and down are not responding!

plz help
User is offlineProfile CardPM
+Quote Post

inso
RE: Help With KeyUp & KeyDown
7 Oct, 2007 - 04:37 AM
Post #6

New D.I.C Head
*

Joined: 7 Oct, 2007
Posts: 1


My Contributions
QUOTE(rabbit_slayer @ 1 Oct, 2007 - 11:40 PM) *

ok, i kinda figured out the whole thing, but it doesn't work and i don't understand why...
look:
CODE

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            Text1.Text = "UP"
        Case vbKeyDown
            Text1.Text = "DOWN"
        Case vbKeyLeft
            Text1.Text = "LEFT"
        Case vbKeyRight
            Text1.Text = "RIGHT"
    End Select
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            Text1.Text = "neutral"
        Case vbKeyDown
            Text1.Text = "neutral"
        Case vbKeyLeft
            Text1.Text = "neutral"
        Case vbKeyRight
            Text1.Text = "neutral"
    End Select
End Sub

Private Sub Timer1_Timer()
   cmd1.Top = cmd1.Top - 20
End Sub

Private Sub Timer2_Timer()
    cmd1.Top = cmd1.Top + 20
End Sub

Private Sub Timer3_Timer()
    cmd1.Left = cmd1.Left - 20
End Sub

Private Sub Timer4_Timer()
    cmd1.Top = cmd1.Top + 20
End Sub

Private Sub Timer5_Timer()
    If Text1.Text = "UP" Then
    Timer1.Enabled = True
    
    If Text1.Text = "DOWN" Then
    Timer2.Enabled = True
    
    If Text1.Text = "LEFT" Then
    Timer3.Enabled = True
    
    If Text1.Text = "RIGHT" Then
    Timer4.Enabled = True
    
    If Text1.Text = "neutral" Then
    Timer1.Enabled = False
    If Text1.Text = "neutral" Then
    Timer2.Enabled = False
    If Text1.Text = "neutral" Then
    Timer3.Enabled = False
    If Text1.Text = "neutral" Then
    Timer4.Enabled = False
End If
End If
End If
End If
End If
End If
End If
End If

End Sub



it only works for vbkeyup...left, right and down are not responding!

plz help



Hi, Im no expert but Ive done a couple of simple vbgames where you move around objects with the arrowkeys. I suppose that is what you're trying to do, moving around 'cmd1'. I would write the code like this:

CODE

'A variable for each of the directions
dim vxr as integer, vxl, vyu, vyd


Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
'Sets the variables to 20 or -20 when you press the keys
    Select Case KeyCode
        Case vbKeyUp
            vyu = -20
        Case vbKeyDown
            vyd = 20
        Case vbKeyLeft
            vxl = -20
        Case vbKeyRight
            vxr = 20
    End Select
End Sub


Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
'Sets the variable to 0 when you release the keys
    Select Case KeyCode
        Case vbKeyUp
            vyu=0
        Case vbKeyDown
            vyd=0
        Case vbKeyLeft
            vxl=0
        Case vbKeyRight
            vxr=0
    End Select
End Sub

Private Sub Timer1_Timer()
' One timer who moves it to all directions at the same time,, pressing two buttons ie left and right will make the total change to 0 making it stand still in that direction
  
    cmd1.Top = cmd1.Top + vyu
    cmd1.Top = cmd1.Top + vyd
    cmd1.left = cmd1.left + vxl
    cmd1.left = cmd1.left + vxr

End Sub



I think that should work, here you have one timer which controls all the directions and it's enabled all the time.
I understand that there's probably lots of better ways to write this but Im quite new to programming,, I hope I could help

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 01:25AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month