Results 1 to 9 of 9

Thread: Better key detection

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    2 miles from everywhere
    Posts
    80

    Better key detection

    Whats a good way to detect key presses? Using things like if KeyCode = vbKeyDown Then leaves this annoying little delay between pressing the key and doing stuff.
    Last edited by Drakon; Jun 1st, 2002 at 10:36 PM.
    Do you know if you will answer no to this question?
    If we've never seen something happen, we can't know if its impossible.
    If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
    If someone orders you to disobey all of their orders, do you obey or disobey?

  2. #2
    Zaei
    Guest
    I find that the easiest way is to keep an array of booleans, like so:
    Code:
    Public keys(256) as Boolean
    
    Private Sub Form_KeyDown(keyAscii As Integer)
       keys(keyAscii) = True
    End Sub
    
    Private Sub Form_KeyUp(keyAscii As Integer)
       keys(keyAscii) = False
    End Sub
    
    Private Sub DoKeys()
      If keys(...) Then
         '... do stuff
      End If
    End Sub
    Just call DoKeys at each iteration of your main loop.

    Z.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    2 miles from everywhere
    Posts
    80
    Where can I find a list of the key's ascii numbers?
    Do you know if you will answer no to this question?
    If we've never seen something happen, we can't know if its impossible.
    If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
    If someone orders you to disobey all of their orders, do you obey or disobey?

  4. #4
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    there is constants called
    vbKeyA for example...

    I would rather use getkeystate though...
    Sanity is a full time job

    Puh das war harter Stoff!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    2 miles from everywhere
    Posts
    80
    how do I use getkeystate?

    and I get an error when I use only keyAscii as Integer in the parentheses after Private Sub Form_KeyUp and KeyDown.
    Do you know if you will answer no to this question?
    If we've never seen something happen, we can't know if its impossible.
    If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
    If someone orders you to disobey all of their orders, do you obey or disobey?

  6. #6
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    VB Code:
    1. If GetKeyState(vbKeyA) <> 1 Then Form1.Caption = "Key a pressed"

    but that code is not supposed to go into a key down event or something, but needs to be put in a loop!
    Sanity is a full time job

    Puh das war harter Stoff!

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    2 miles from everywhere
    Posts
    80
    I have a feeling I did something wrong somewhere... The program starts and it instantly does the code in the GetKeyState thing.
    Where is GetKeyState declared, and as what?
    Last edited by Drakon; Jun 2nd, 2002 at 01:21 PM.
    Do you know if you will answer no to this question?
    If we've never seen something happen, we can't know if its impossible.
    If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
    If someone orders you to disobey all of their orders, do you obey or disobey?

  8. #8
    Zaei
    Guest
    Originally posted by Drakon
    and I get an error when I use only keyAscii as Integer in the parentheses after Private Sub Form_KeyUp and KeyDown.
    There are probably other parameters that I dont remember. The code I posted was off the top of my head.

    Z.

  9. #9
    Addicted Member Bazza81's Avatar
    Join Date
    Apr 2001
    Location
    Nottingham, UK
    Posts
    203
    getkeystate will return a non zero value if the key was pressed any time before the previous call to getkeystate. What I usually do is run through a loop of all the keys 1 to 255 with getkeystate when my prog starts, this will then void any key presses prior to program execution and later on during the application.
    Who needs rhetorical questions anyway?


    Bazza NET - The place you want to be!

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