using the keydown function in a timer-VBForums
Results 1 to 7 of 7

Thread: using the keydown function in a timer

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    12

    using the keydown function in a timer

    hi, I am making a game that involves using the arrow keys I have been using the keydown function for the form but when i use it now all the timers seem to stop. this was my previous code for the arrow keys using the keydown function:
    Code:
    If e.keydata = keys.up Then
    If pbxship.Top <= 77 Then
    pbxship.Top += 10
    Else
    pbxship.Top -= 10
    End If
    ElseIf e.keydate = keys.down Then
    If pbxship.Top >= 800 Then
    PbxLazer.Top -= 10
    Else
    pbxship.Top += 10
    End If
    now i am trying to use a timer to use the arrow keys and i am using this
    Code:
    Private Declare Function GetKeyState Lib "user32" (ByVal keyCode As Integer) As Short
    
    If GetKeyState(Keys.Up) Then
    If pbxship.Top <= 77 Then
    pbxship.Top += 10
    Else
    pbxship.Top -= 10
    End If
    ElseIf GetKeyState(Keys.Down) Then
    If pbxship.Top >= 800 Then
    PbxLazer.Top -= 10
    Else
    pbxship.Top += 10
    End If
    but this does not do anything like the keys are not pressed, does anyone know how to make it work or a different way to do this in a timer

    thanks in advance
    Last edited by samqweqwe3; Feb 16th, 2012 at 07:25 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,474

    Re: using the keydown function in a timer

    try this:

    vb Code:
    1. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    12

    Re: using the keydown function in a timer

    it didnt do anything
    i think it is because it does not have a value to determine if it is pressed or not, it just jets the key state. it needs a value like 0, 1 or true, false (these did not work). do you know what value they should be?
    Last edited by samqweqwe3; Feb 15th, 2012 at 04:56 AM.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,474

    Re: using the keydown function in a timer

    can you post the full code?

    we should be able to see the problem

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    12

    Re: using the keydown function in a timer

    what do you mean by all the code? do you mean all the code on moving or just all the code

    here is the moving code:

    Code:
     Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Long
        Private Sub Tmrmove_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tmrmove.Tick
            'to move up and down
            If GetAsyncKeyState(Keys.Up) = 1 Then
                If pbxship.Top <= 77 Then
                    pbxship.Top += 10
                Else
                    pbxship.Top -= 10
                End If
            ElseIf GetAsyncKeyState(Keys.Down) = 1 Then
                If pbxship.Top >= 800 Then
                    PbxLazer.Top -= 10
                Else
                    pbxship.Top += 10
                End If
    end sub
    was that what you meant by all the code?
    Last edited by samqweqwe3; Feb 16th, 2012 at 07:24 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    12

    Re: using the keydown function in a timer

    IT WORKS!!!!
    i made a stupid mistake, i forgot to enable the timer

    thanks for helping

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,474

    Re: using the keydown function in a timer

    ahh. that'd help....

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.