Results 1 to 18 of 18

Thread: help with VK keys

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    13

    help with VK keys

    Hey, first of all, is VK key the best way to go? I use it in C++ a litte, seems to work. Anyways, heres what I have, isn't working.

    Dim checkkeys As Boolean
    Private Declare Function GetAsyncKeyState _
    Lib "user32" (ByVal vKey As Long) As Integer
    Private Sub Form_Load()
    checkstuff
    End Sub
    Private Sub checkstuff()
    checkkeys = True
    Do While checkkeys = True
    If GetAsyncKeyState(VK_DOWN) Then
    MsgBox "YESSUM!"
    Else
    MsgBox "NOPE"
    End If
    Loop
    End Sub
    Msgbox "MUAHAHA"

  2. #2
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Huh try putting a DoEvents inside the loop... this way it won't stop the program to run the loop. You must also use Me.Show before the loop or it won't display the form properly
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    13

    still nope

    still freezes. well, any help is appreciated.
    Msgbox "MUAHAHA"

  4. #4
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Man that's not a freeze, it's executing the loop just like you told it to

    The problem is that every milisecond or so it checks if the key is pressed, and the messagebox will stop the loop until you press OK and then the loop will start again just a few miliseconds later... and I don't think you can press the down key that fast

    Try this instead. I only did the modifications I told you about, and replaced the messagebox thingy with a line that will simply set the form's caption to the return value.

    VB Code:
    1. Dim checkkeys As Boolean
    2. Private Declare Function GetAsyncKeyState _
    3. Lib "user32" (ByVal vKey As Long) As Integer
    4.  
    5. Private Sub Form_Load()
    6.     Me.Show
    7.     checkstuff
    8. End Sub
    9.  
    10. Private Sub checkstuff()
    11.     checkkeys = True
    12.     Do While checkkeys = True
    13.         If GetAsyncKeyState(VK_DOWN) Then
    14.             Me.Caption = "YESSUM!"
    15.         Else
    16.             Me.Caption "NOPE"
    17.         End If
    18.         DoEvents
    19.     Loop
    20. End Sub

    (Btw, you can use [ vbcode ] and [/ vbcode ] tags to make your code look better when you post it )
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    13

    lol that doesnt work cuz too fast.

    is there any way to tell it to wait 100 ms before checking again? I tried to use a timer but I couldnt figure out how to use it the way I need.
    Msgbox "MUAHAHA"

  6. #6
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Yes, you can limit the loop by using then GetTickCount() API or the QueryPerformanceCounter() API. For example:[Highlight=VB]Private Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long

    Dim checkkeys As Boolean
    Private Declare Function GetAsyncKeyState _
    Lib "user32" (ByVal vKey As Long) As Integer

    Private Sub Form_Load()
    Me.Show
    checkstuff
    End Sub

    Private Sub checkstuff()
    Dim T As Long
    T = GetTickCount
    checkkeys = True
    Do While checkkeys = True
    If GetTickCount - T => 100 Then 'If 100ms has passed...
    If GetAsyncKeyState(VK_DOWN) Then
    Me.Caption = "YESSUM!"
    Else
    Me.Caption "NOPE"
    End If
    T = GetTickCount
    End If
    DoEvents
    Loop
    End Sub
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    13

    nope... hmmm

    um its still not working.. this tick count thing works! but.. its not catching the key down..
    could it be because I have a usb keyboard? I don't know.. well plz help!
    Msgbox "MUAHAHA"

  8. #8
    Addicted Member Janus's Avatar
    Join Date
    Aug 2001
    Location
    California
    Posts
    221
    That's making it only check every 100ms, so it'd be harder to catch keypreses that way.
    "1 4m 4 1337 #4xz0r!'
    Janus

  9. #9
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Heh don't worry, I'll give you the code when I get home
    (Just make sure yhou post here again so this thing e-mails me and I don't forget about it )
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  10. #10
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    VB Code:
    1. Dim checkkeys As Boolean
    2. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    3. Private Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
    4.  
    5. Private Sub Form_Load()
    6.     Me.Show
    7.     checkstuff
    8. End Sub
    9.  
    10. Private Sub checkstuff()
    11.     Dim tmpL&, bolOn as boolean
    12.  
    13.     checkkeys = True
    14.     Do While checkkeys = True
    15.         if not bolOn then bolOn =GetAsyncKeyState(VK_DOWN)
    16.  
    17.         if (tmpl-gettickcount) >=100 then
    18.             If bolOn Then
    19.                 Me.Caption = "YESSUM!"
    20.                 bolOn = false
    21.              Else
    22.                  Me.Caption "NOPE"
    23.              End If
    24.              tmpl=gettickcount
    25.         End if
    26.         DoEvents
    27.     Loop
    28. End Sub

    This will still check every few milliseconds. If it is off, then it will set it to the key state. If it is on, it will leave it on. Then, every 100 milliseconds, it will check the state of the boolean. It will also do the doevents every few milliseconds.

    The spelling may not be correct.
    I am at school, and can't test the code right now.
    Involved in: Sentience

  11. #11

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    13

    nope...

    I dunno, I think its just not detecting the key.. even the VB key stuff doesnt work. Well I have XP and a USB keyboard, can this effect the well being of the code? In anycase it isn't working so again any help as well as the help I have been getting is welcome and thanked. Thanks guys.
    Msgbox "MUAHAHA"

  12. #12
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    Oops, I had the gettickcount and tmpl revesed. Heres the fixed code:

    VB Code:
    1. Option Explicit
    2.  
    3. Dim checkkeys As Boolean
    4. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    5. Private Declare Function GetTickCount Lib "kernel32" () As Long
    6.  
    7. Private Sub Form_Load()
    8.     Me.Show
    9.     DoEvents
    10.     checkstuff
    11. End Sub
    12.  
    13. Private Sub checkstuff()
    14.     Dim tmpL&, bolOn As Boolean
    15.  
    16.     checkkeys = True
    17.     Do While checkkeys = True
    18.         If Not bolOn Then bolOn = GetAsyncKeyState(vbKeyA)
    19.        
    20.         If (GetTickCount - tmpL) >= 100 Then
    21.             If bolOn Then
    22.                 Me.Caption = "YESSUM!"
    23.                 bolOn = False
    24.              Else
    25.                  Me.Caption = "NOPE"
    26.              End If
    27.              tmpL = GetTickCount
    28.         End If
    29.         DoEvents
    30.     Loop
    31.    
    32.     Unload Me
    33. End Sub
    34.  
    35. Private Sub Form_Unload(Cancel As Integer)
    36.  If checkkeys Then
    37.   checkkeys = False
    38.   Cancel = -1
    39.  End If
    40. End Sub

    This code will also allow the close button to work too.
    Involved in: Sentience

  13. #13
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    Also, I switched the vbKeyDown to vbKeyA, switch it back if you wish...

    So like this:

    VB Code:
    1. Option Explicit
    2.  
    3. Dim checkkeys As Boolean
    4. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    5. Private Declare Function GetTickCount Lib "kernel32" () As Long
    6.  
    7. Private Sub Form_Load()
    8.     Me.Show
    9.     DoEvents
    10.     checkstuff
    11. End Sub
    12.  
    13. Private Sub checkstuff()
    14.     Dim tmpL&, bolOn As Boolean
    15.  
    16.     checkkeys = True
    17.     Do While checkkeys = True
    18.         If Not bolOn Then bolOn = GetAsyncKeyState(vbKeyDown)
    19.        
    20.         If (GetTickCount - tmpL) >= 100 Then
    21.             If bolOn Then
    22.                 Me.Caption = "YESSUM!"
    23.                 bolOn = False
    24.              Else
    25.                  Me.Caption = "NOPE"
    26.              End If
    27.              tmpL = GetTickCount
    28.         End If
    29.         DoEvents
    30.     Loop
    31.    
    32.     Unload Me
    33. End Sub
    34.  
    35. Private Sub Form_Unload(Cancel As Integer)
    36.  If checkkeys Then
    37.   checkkeys = False
    38.   Cancel = -1
    39.  End If
    40. End Sub
    Involved in: Sentience

  14. #14

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    13

    sweet thanks!

    Ur the man! Hey what does option explicit do?
    Msgbox "MUAHAHA"

  15. #15
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    And heres even more advanced code, which allows for any number of keys (it is set for the four arrow keys).

    VB Code:
    1. Option Explicit
    2.  
    3. Dim checkkeys As Boolean
    4. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    5. Private Declare Function GetTickCount Lib "kernel32" () As Long
    6.  
    7. Private Sub Form_Load()
    8.     Me.Show
    9.     DoEvents
    10.     checkstuff
    11. End Sub
    12.  
    13. Private Sub checkstuff()
    14.  Dim tmpL&, bolDown As Boolean, bolUp As Boolean, bolLeft As Boolean, bolRight As Boolean
    15.  Dim tmpS$, tmpL2&
    16.  
    17.  checkkeys = True
    18.  Do While checkkeys = True
    19.   If Not bolDown Then bolDown = GetAsyncKeyState(vbKeyDown)
    20.   If Not bolUp Then bolUp = GetAsyncKeyState(vbKeyUp)
    21.   If Not bolLeft Then bolLeft = GetAsyncKeyState(vbKeyLeft)
    22.   If Not bolRight Then bolRight = GetAsyncKeyState(vbKeyRight)
    23.        
    24.   If (GetTickCount - tmpL) >= 100 Then
    25.    tmpS = ""
    26.    If bolDown Then
    27.     tmpS = tmpS & "Down"
    28.     bolDown = False
    29.    End If
    30.    If bolUp Then
    31.     tmpS = tmpS & " Up"
    32.     bolUp = False
    33.    End If
    34.    If bolLeft Then
    35.     tmpS = tmpS & " Left"
    36.     bolLeft = False
    37.    End If
    38.    If bolRight Then
    39.     tmpS = tmpS & " Right"
    40.     bolRight = False
    41.    End If
    42.    If tmpS = "" Then
    43.     tmpS = "None"
    44.    End If
    45.    tmpS = Trim$(tmpS)
    46.    
    47.    tmpL = GetTickCount
    48.   End If
    49.  
    50.   Me.Caption = tmpS
    51.   DoEvents
    52.  Loop
    53.    
    54.  Unload Me
    55. End Sub
    56.  
    57. Private Sub Form_Unload(Cancel As Integer)
    58.  If checkkeys Then
    59.   checkkeys = False
    60.   Cancel = -1
    61.  End If
    62. End Sub
    Involved in: Sentience

  16. #16
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    Option Explicit is to make sure that all the varibles are defined. It is a good idea to set it to defualt. Look under tools-options-editor-require varible declaration. That is where it is in 6.0. It is to prevent errors from the wrong types of varibles being declared. Such as you need a string, and it defines it as an interger, or you need a long and it sets it to a single. It also increases speed, as you are less likely to have vareints in the program.
    Involved in: Sentience

  17. #17
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Heh sorry for the delay
    Yeah my code is kinda like that, except that I use a 256 boolean array instead of a variable for each key
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  18. #18
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    I was going to do that, but I figured that it would be too much work to convert it over. And if he really needed that he could ask.
    Involved in: Sentience

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