PDA

Click to See Complete Forum and Search --> : Keypress Function


TacoTycoon
Nov 1st, 2000, 07:53 AM
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

Nov 1st, 2000, 08:14 AM
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:



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.

Fox
Nov 1st, 2000, 08:39 AM
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:


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

Nov 1st, 2000, 03:26 PM
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.

kedaman
Nov 2nd, 2000, 07:24 AM
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 :)

Fox
Nov 2nd, 2000, 07:45 AM
...use DInput ;)

MoMad
Nov 2nd, 2000, 04:19 PM
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

Nov 2nd, 2000, 04:53 PM
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.

kedaman
Nov 3rd, 2000, 08:53 AM
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 :D

MoMad
Nov 3rd, 2000, 11:23 AM
Kedaman is right... listen to him for once!!!

after all, he's a guru!!!