Results 1 to 10 of 10

Thread: Keypress Function

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    1

    Unhappy

    Hey dudes~

    Im a young Vb programmer and Im now taking a few courses to get me into the language. I was wondering, how do you use the Keypress function anyhow? I already know the ascii# for all the keys i just dont know how to use the function
    Private Sub Form_KeyPress(KeyAscii As Integer), where do you input the ascii key?

    If anyone could help I would appreciate it.

    -TacoTycoon

  2. #2
    Guest
    Whatever you place in this sub will be executed everytime any key of the keyboard is pressed. Keyascii is the ASCII value of the pressed key.
    If, for example, you wish for your application to "Beep" every time you press the "a" key, you will have to insert code similar to this:

    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer)
    
          if chr(Keyascii) = "a" or chr(Keyascii) = "A" then
                Beep
          end if
    end sub

    I hope that's clear enough.

  3. #3
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    That's not correct. The KeyPress function only processes the ASCII keys pressed, ie. A E X . and so on..

    If you want to get all keys (especially the arrows ) you should use the KeyDown function which really gets all keys:

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        Select Case KeyCode
        Case vbKeyLeft
            MsgBox "Pressed the LEFT key"
        Case vbKeyRight
            MsgBox "Pressed the RIGHT key"
        Case vbKeyUp
            MsgBox "Pressed the UP key"
        Case vbKeyDown
            MsgBox "Pressed the DOWN key"
        Case Else
            MsgBox "Pressed key, code = " & KeyCode
        End Select
    End Sub

  4. #4
    Guest

    Damn Fox!

    Yeah, Fox is right. As usual...

    The Keypress event does not work for ALL the keys on your keyboard, only the ones that can actually print something on screen, like letters and numbers for example. Any other key will be accessed through the KyDown or KeyUp event.


  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    HEhe, even Keydown doesn't receive all events from keys pressed, if you want all, and that means exactly all keys including printscreen and ctrl-alt-numlock-C-D-E-uparrow then you should use getasynckeystate api
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    ...use DInput

  7. #7
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625

    hehe

    what r u all fighting about??? TacoTycoon just wants to know how to use the keypress function and he's just a bigenner so dont confuse ppl...

    I do suggest though that keydown will get the job done if ur a bigenner.

    all this api cals and stuff will get u confused... i bet TacoTycoon doesnt even know what api is... I didnt know it until i finished my first vb course!!!

    good luck, to you
    TacoTycoon
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  8. #8
    Guest
    I'm with Fox on this; DInput works more well than GetAsyncKeyState, however, you need to include the DX type library if you want to use DInput -- So there's a down side and an up side.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Hey hey, what are we fighting about? I'll suggest just have a go with Keydown event and you don't need to put any extra dependencies in youar app
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Kedaman is right... listen to him for once!!!

    after all, he's a guru!!!
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

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