Results 1 to 16 of 16

Thread: KeyAscii..it's killing me!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    16

    KeyAscii..it's killing me!

    I already posted a message that has to do with this one, but not entirely. If i set a case statement on VB and i assign the cases to different vbKeys and i press both at the same time, it will only recognize one, ignoring all others. Like.. in games you can do several keys at the same time..how..(i know C++ and other programs than VB6.0 are better for games..).
    My ex:

    Private Sub Form_KeyPress(KEYASCII As Integer)
    Select Case KEYASCII
    Case vbKeyW
    pyinc1 = -200
    Case vbKeyS
    pyinc1 = 200
    End Select
    End Sub

    basically im increasing/decreasing Y-Increments when press either W or S, say i want to press S and W at the same time, only one will be taken. What if i want to move it diagonally with W and D for example.. it wouldn't... should I use another statement?

  2. #2
    Member
    Join Date
    May 2003
    Posts
    59
    You were pretty close... but the KeyAscii value will do you no good because it is limited to a single value; An integer that corresponds to a standard numeric ANSI keycode.

    To handle simultaneous keystrokes you'll have to get out of the "VB Comfort Zone" and call on the Windows API GetKeyboardStatus function, although we'll still use the Form_KeyPress event to trap standard key presses.

    VB Code:
    1. 'GetKeyboardState API: Returns the current status of the entire keyboard...
    2. 'this can include upper ASCII characters for which there is no key,
    3. 'and even the three mouse buttons.
    4. Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    5.  
    6. 'keycode constants... you'll have to lookup the other 250+ on the internet :-)
    7. Const VK_A = &H41       'virtual keycode for A (or a)
    8. Const VK_D = &H44       'virtual keycode for D (or d)
    9. Const VK_S = &H53       'virtual keycode for S (or s)
    10. Const VK_W = &H57       'virtual keycode for W (or w)
    11. Const KeyIsDown = &H80  'bit which indicates key is currently down
    12.  
    13. 'This event occurs when the user presses or releases an ANSI key.
    14. 'Keep in mind that function keys, editing keys, navigation keys,
    15. 'and any combination of these are not recognized by the KeyPress
    16. 'event. Also remember to set the form's KeyPreview property to True.
    17. Private Sub Form_KeyPress(KeyAscii As Integer)
    18.  
    19.     'dimension needed variables
    20.     Dim KeyboardState(0 To 255) As Byte         'byte array to hold keyboard status
    21.     Dim retval As Long                          'return value for GetKeyboardState
    22.    
    23.     'call API
    24.     retval = GetKeyboardState(KeyboardState(0))
    25.     If retval = 0 Then                          '0 indicates API call error
    26.         MsgBox ("Keyboard Error")               'notify user of error
    27.         Exit Sub                                'stop processing this keystroke
    28.     End If
    29.    
    30.     'process keystroke
    31.     If KeyboardState(VK_A) And KeyIsDown Then           'if A is down
    32.         If KeyboardState(VK_D) And KeyIsDown Then           'if D is down
    33.             Label1.Caption = "AD"                           '   - or -
    34.         ElseIf KeyboardState(VK_S) And KeyIsDown Then       'if S is down
    35.             Label1.Caption = "AS"                           '   - or -
    36.         ElseIf KeyboardState(VK_W) And KeyIsDown Then       'if W is down
    37.             Label1.Caption = "AW"
    38.         Else
    39.             Label1.Caption = "A"
    40.         End If
    41.     ElseIf KeyboardState(VK_D) And KeyIsDown Then           'if D is down
    42.         If KeyboardState(VK_A) And KeyIsDown Then
    43.             Label1.Caption = "DA"
    44.         ElseIf KeyboardState(VK_S) And KeyIsDown Then
    45.             Label1.Caption = "DS"
    46.         ElseIf KeyboardState(VK_W) And KeyIsDown Then
    47.             Label1.Caption = "DW"
    48.         Else
    49.             Label1.Caption = "D"
    50.         End If
    51.     ElseIf KeyboardState(VK_S) And KeyIsDown Then           'if S is down
    52.         If KeyboardState(VK_A) And KeyIsDown Then
    53.             Label1.Caption = "SA"
    54.         ElseIf KeyboardState(VK_D) And KeyIsDown Then
    55.             Label1.Caption = "SD"
    56.         ElseIf KeyboardState(VK_W) And KeyIsDown Then
    57.             Label1.Caption = "SW"
    58.         Else
    59.             Label1.Caption = "S"
    60.         End If
    61.     ElseIf KeyboardState(VK_W) And KeyIsDown Then           'if W is down
    62.         If KeyboardState(VK_A) And KeyIsDown Then
    63.             Label1.Caption = "WA"
    64.         ElseIf KeyboardState(VK_D) And KeyIsDown Then
    65.             Label1.Caption = "WD"
    66.         ElseIf KeyboardState(VK_S) And KeyIsDown Then
    67.             Label1.Caption = "WS"
    68.         Else
    69.             Label1.Caption = "W"
    70.         End If
    71.     End If
    72.    
    73. End Sub

    BTW: Who says C++ is better for games?!
    Share what you know... Know what you share

    R.G. Brown
    Senior Software Engineer
    Intel Corporation

  3. #3
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176
    C++ is faster but its much easier to write games in VB

    in my opinion anyway
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    16
    !!!!! Thx Man!!!!!!

    Well, actually i forgot to mention I was assuming C++ is better. Because in a lot of games I've seen they apply or require C++. So I would think now that it depends on the programmer right?

  5. #5
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    You don't have to use APIs to make your keyboard manage more then one press at the same time....look at this simple method I was suing in this thread...

    http://vbforums.com/showthread.php?s=&threadid=246477

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    16
    Anyone know any good webpages where they list the codes for the keycode constants?

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Do you have MSDN?


    Constant Value Description
    vbKeyLButton 0x1 Left mouse button
    vbKeyRButton 0x2 Right mouse button
    vbKeyCancel 0x3 CANCEL key
    vbKeyMButton 0x4 Middle mouse button
    vbKeyBack 0x8 BACKSPACE key
    vbKeyTab 0x9 TAB key
    vbKeyClear 0xC CLEAR key
    vbKeyReturn 0xD ENTER key
    vbKeyShift 0x10 SHIFT key
    vbKeyControl 0x11 CTRL key
    vbKeyMenu 0x12 MENU key
    vbKeyPause 0x13 PAUSE key
    vbKeyCapital 0x14 CAPS LOCK key
    vbKeyEscape 0x1B ESC key
    vbKeySpace 0x20 SPACEBAR key
    vbKeyPageUp 0x21 PAGE UP key
    vbKeyPageDown 0x22 PAGE DOWN key
    vbKeyEnd 0x23 END key
    vbKeyHome 0x24 HOME key
    vbKeyLeft 0x25 LEFT ARROW key
    vbKeyUp 0x26 UP ARROW key
    vbKeyRight 0x27 RIGHT ARROW key
    vbKeyDown 0x28 DOWN ARROW key
    vbKeySelect 0x29 SELECT key
    vbKeyPrint 0x2A PRINT SCREEN key
    vbKeyExecute 0x2B EXECUTE key
    vbKeySnapshot 0x2C SNAPSHOT key
    vbKeyInsert 0x2D INSERT key
    vbKeyDelete 0x2E DELETE key
    vbKeyHelp 0x2F HELP key
    vbKeyNumlock 0x90 NUM LOCK key


    The A key through the Z key are the same as the ASCII equivalents A – Z:

    Constant Value Description
    vbKeyA 65 A key
    vbKeyB 66 B key
    vbKeyC 67 C key
    vbKeyD 68 D key
    vbKeyE 69 E key
    vbKeyF 70 F key
    vbKeyG 71 G key
    vbKeyH 72 H key
    vbKeyI 73 I key
    vbKeyJ 74 J key
    vbKeyK 75 K key
    vbKeyL 76 L key
    vbKeyM 77 M key
    vbKeyN 78 N key
    vbKeyO 79 O key
    vbKeyP 80 P key
    vbKeyQ 81 Q key
    vbKeyR 82 R key
    vbKeyS 83 S key
    vbKeyT 84 T key
    vbKeyU 85 U key
    vbKeyV 86 V key
    vbKeyW 87 W key
    vbKeyX 88 X key
    vbKeyY 89 Y key
    vbKeyZ 90 Z key


    The 0 key through 9 key are the same as their ASCII equivalents 0 – 9:

    Constant Value Description
    vbKey0 48 0 key
    vbKey1 49 1 key
    vbKey2 50 2 key
    vbKey3 51 3 key
    vbKey4 52 4 key
    vbKey5 53 5 key
    vbKey6 54 6 key
    vbKey7 55 7 key
    vbKey8 56 8 key
    vbKey9 57 9 key


    The following constants represent keys on the numeric keypad:

    Constant Value Description
    vbKeyNumpad0 0x60 0 key
    vbKeyNumpad1 0x61 1 key
    vbKeyNumpad2 0x62 2 key
    vbKeyNumpad3 0x63 3 key
    vbKeyNumpad4 0x64 4 key
    vbKeyNumpad5 0x65 5 key
    vbKeyNumpad6 0x66 6 key
    vbKeyNumpad7 0x67 7 key
    vbKeyNumpad8 0x68 8 key
    vbKeyNumpad9 0x69 9 key
    vbKeyMultiply 0x6A MULTIPLICATION SIGN (*) key
    vbKeyAdd 0x6B PLUS SIGN (+) key
    vbKeySeparator 0x6C ENTER key
    vbKeySubtract 0x6D MINUS SIGN (–) key
    vbKeyDecimal 0x6E DECIMAL POINT (.) key
    vbKeyDivide 0x6F DIVISION SIGN (/) key


    The following constants represent function keys:

    Constant Value Description
    vbKeyF1 0x70 F1 key
    vbKeyF2 0x71 F2 key
    vbKeyF3 0x72 F3 key
    vbKeyF4 0x73 F4 key
    vbKeyF5 0x74 F5 key
    vbKeyF6 0x75 F6 key
    vbKeyF7 0x76 F7 key
    vbKeyF8 0x77 F8 key
    vbKeyF9 0x78 F9 key
    vbKeyF10 0x79 F10 key
    vbKeyF11 0x7A F11 key
    vbKeyF12 0x7B F12 key
    vbKeyF13 0x7C F13 key
    vbKeyF14 0x7D F14 key
    vbKeyF15 0x7E F15 key
    vbKeyF16 0x7F F16 key

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    16
    thats a lot of keycodes thx!

  9. #9
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Yes I think that was all of them...

  10. #10

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    16
    Sigh... I tried applying the elseif's.. and the GetKeyboardState dnrodrigo gave me... but it seems i'm appying it wrong to my Pong program...

    Ok... so I guess i'll attach it and you can see for yourself. If anyone has a bit of their time to dedicate to this n00b programmer..please do.
    HERE IT GOES

  11. #11
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Why don't you just forget about that API for a while, and look at the example I gave you. It is easier to understand for a noob.......

  12. #12

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    16
    ...
    Attached Files Attached Files

  13. #13

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    16
    guess u r right..

    so i just set booleans to see which keycombos are hit? ill give it a try.

  14. #14
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Yes that is the way to do it...

  15. #15

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    16
    GOD! I made IT!..... THX for your help man... and your keys program... all i needed was the KeyArray(KeyCode) line.

    Now that I see it... it makes sense...

  16. #16
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Yeah....good to hear....took me a while to figgure it out. But Now I am using it in all my small games. Keep up the good work Shin...

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