Results 1 to 4 of 4

Thread: GetAsyncKeyState errors?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    47

    GetAsyncKeyState errors?

    Hi, I'm trying to make an app for another application using GetAsyncKeyState.

    I coded what I thought to be good and clicked "Build". When I opened it, I tested it in notepad. When I hit NumPad2, it should return
    Code:
    `camera_control 0
    or
    Code:
    `camera_control 1
    Instead, it returns:
    Code:
    `camera_control 0`camera_control 1`camera_control 0`camera_control 1`camera_control 0
    or
    Code:
    `camera_control 1`camera_control 0`camera_control 1`camera_control 0`camera_control 1
    This is my code:

    Code:
        Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
        Public camcont As Boolean = False
        Public curplayer As Integer = 0
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Timer1.Enabled = True Then
                Timer1.Interval = 1
                If GetAsyncKeyState(Keys.NumPad2) Then
                    If camcont = False Then
                        SendKeys.Send("`")
                        SendKeys.Send("camera_control 1")
                        camcont = True
                    Else
                        SendKeys.Send("`")
                        SendKeys.Send("camera_control 0")
                        camcont = False
                    End If
                End If
                If GetAsyncKeyState(Keys.NumPad1) Then
                    If camcont = True Then
                        curplayer = curplayer + 1
                        SendKeys.Send("`")
                        SendKeys.Send("if (unit (list_get (players) " & curplayer & ")) (camera_set_first_person (unit (list_get (players) " & curplayer & ")))")
                        'SendKeys.Send("camera_set_first_person (unit (list_get (players) " & curplayer & "))")
                    End If
                End If
                If GetAsyncKeyState(Keys.NumPad3) Then
                    If camcont = True Then
                        curplayer = curplayer - 1
                        SendKeys.Send("`")
                        SendKeys.Send("if (unit (list_get (players) " & curplayer & ")) (camera_set_first_person (unit (list_get (players) " & curplayer & ")))")
                        'SendKeys.Send("camera_set_first_person (unit (list_get (players) " & curplayer & "))")
                    End If
                End If
    
            End If
        End Sub
    How would I make it return as it should?

    I am using VS 2010 with .NET framework 4.0 Beta 1.
    Thanks for any help,
    -Arightwizard
    The following statements are true. The following statement is false. The first statement is true.

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: GetAsyncKeyState errors?

    The function returns 1 if the key is still down after you have already checked it. Check instead against Integer.MaxVal to see if the key is down. It's returning these keys because you are holding down the key long enough for the Timer to detect more than one keypress.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    47

    Re: GetAsyncKeyState errors?

    Thanks, fixed.
    The following statements are true. The following statement is false. The first statement is true.

  4. #4
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: GetAsyncKeyState errors?

    Please mark threads Resolved if you have no more questions.

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
  •  



Click Here to Expand Forum to Full Width