Results 1 to 17 of 17

Thread: KeyDown (whats wrong?)

  1. #1

    Thread Starter
    Member insaneman's Avatar
    Join Date
    Oct 2001
    Posts
    63

    KeyDown (whats wrong?)

    I am making a menu for a game, and I am useing arrows to select new game...so I decide to make a very simple system for this so I use:

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 40 Then
    arrow1.Visible = False
    arrow2.Visible = True
    End Sub

    and it wont work. I also tried arrow1_KeyDown, but that wouldnt work either. So what is wrong?

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Smile

    The form has a property called KeyPreview. You have to set it to true. If that’s not what’s wrong, you have to give us something more to look at...because your code looks fine...

  3. #3

    Thread Starter
    Member insaneman's Avatar
    Join Date
    Oct 2001
    Posts
    63
    yes, that was it, but that will cause some problems, since I have several selections. I remember that i did this before and i did the arrow1_keydown, and then it would work to have several selections, but if I do that, then it wont work, any reson why?

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I'm not sure if I understand your problem, but maybe this helps...Is arrow 1, 2 ,3 and so on controls (buttons, pictures...) then they may have the focus and they will catch the key stroke...if they do maybe you have to write something like:

    Private Sub arrow1_KeyDown(KeyCode As Integer, Shift As Integer)
    call Form_keyDown
    End Sub

    Private Sub arrow2_KeyDown(KeyCode As Integer, Shift As Integer)
    call Form_keyDown
    End Sub


    and so on....

  5. #5

    Thread Starter
    Member insaneman's Avatar
    Join Date
    Oct 2001
    Posts
    63
    nope, didnt work.
    So I'll explain it better: I have 4 Labels, and in front of each one is an arrow, arrow 1, 2, 3, and 4. Only arrow1 is visible, so when i would do:

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 40 Then
    arrow1.Visible = False
    arrow2.Visible = True
    End If
    End Sub

    it would make the second arrow visible, and the first not. But now if i press down again, it should go to the third one, so i would have to do:

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 40 Then
    arrow1.Visible = False
    arrow2.Visible = True
    End If
    If KeyCode = 40 Then
    arrow2.Visible = False
    arrow3.Visible = True
    End if
    End Sub

    and that would just skip right down to the third one. But what I remember doing before was:

    Private Sub arrow1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 40 Then
    arrow1.Visible = False
    arrow2.Visible = True
    End If
    End Sub

    Private Sub arrow2_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 40 Then
    arrow1.Visible = False
    arrow2.Visible = True
    End If
    End Sub

    and that worked. I would also stick a keycode 13 in there.

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Global index

    Declare a variable in the top of your app. That will hold what arrow is visible. You can call it Menu...

    when your app starts it should get the value 1.
    VB Code:
    1. Private Sub Form_load()
    2.   Menu = 1
    3. End sub
    And then......

    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     If KeyCode = 40 Then
    3.        if Menu = 1 then
    4.           arrow1.Visible = False
    5.           arrow2.Visible = True
    6.           menu = 2
    7.        elseif menu = 2 then
    8.           arrow2.Visible = False
    9.           arrow3.Visible = True
    10.           menu = 3
    11.        end if
    12.    end if
    13. End Sub

    and so on and so on....
    Last edited by NoteMe; Feb 9th, 2004 at 10:20 AM.

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    And why do my code look like **** when I post it...with no tabs?

  8. #8
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Nice forum making my Shi(t) go *****

  9. #9

    Thread Starter
    Member insaneman's Avatar
    Join Date
    Oct 2001
    Posts
    63

    Unhappy

    that didnt work either.

  10. #10
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    post me the app, and I will fiks it for you...I hope...

  11. #11

    Thread Starter
    Member insaneman's Avatar
    Join Date
    Oct 2001
    Posts
    63
    there, you wont see the arrow...
    Attached Files Attached Files

  12. #12
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    It did work with my code....
    Attached Files Attached Files

  13. #13

    Thread Starter
    Member insaneman's Avatar
    Join Date
    Oct 2001
    Posts
    63
    well, thanks then.

  14. #14
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    It's my pleasure form the North...

  15. #15

    Thread Starter
    Member insaneman's Avatar
    Join Date
    Oct 2001
    Posts
    63
    well, I used your code to do the menu, and it is working well, so I started the actual game...Now I like the menu system, so I kept using it. But now I have another problem:

    If KeyCode = 39 Then
    If Menu = 1 Then
    RM.Left - 20
    End If
    End If
    End Sub

    RM is the main character
    and if menu = 1, it means it is in the game, not in pause or something like that.

    But the thing is, when i press the 39 key (right), RM goes all the way to the left of the screen. Why is this?

  16. #16
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    you have to write RM.left = RM.left - 20

    you have written RM.left = 20

    and that means to the left of the screen...

  17. #17

    Thread Starter
    Member insaneman's Avatar
    Join Date
    Oct 2001
    Posts
    63
    woops.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width