Visual Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 18013
Number of posts: 55386

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
VB6 Detect Keypress Posted by michael_p2234 on 18 Jun 2004 at 3:45 PM
I was wondering what the code is to detect a keypress. I need it to start practicing making games using VB6.
Report
Re: VB6 Detect Keypress Posted by MihailVD on 19 Jun 2004 at 4:00 AM
: I was wondering what the code is to detect a keypress. I need it to start practicing making games using VB6.
:
Interesting question.
I think I can help you on this one.
The first, easy, but not so good-resulted method is to use the events of your Visual Basic project form:
---
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyUp Then MovePlayerUp
If KeyAscii = vbKeyDown Then MovePlayerDown
If KeyAscii = vbKeyLeft Then MovePlayerLeft
If KeyAscii = vbKeyRight Then MovePlayerRight
End Sub
---
And if you want to detect when the user RELEASES the key:
---
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
'Code Here
End Sub
---
The bad thing on using this method is that you cannot detect two keys
pressed at a time.

Now...the really good method, the second method.
Put this in a module to define the key pressing function of the WinAPI:
---
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
---
And then, when you want to detect if a key is pressed:
---
If GetAsyncKeyState(vbKeyUp) < 0 Then MovePlayerUp
If GetAsyncKeyState(vbKeyDown) < 0 Then MovePlayerDown
If GetAsyncKeyState(vbKeyLeft) < 0 Then MovePlayerLeft
If GetAsyncKeyState(vbKeyRight) < 0 Then MovePlayerRight
---

Good Luck!
---------------
Mihail Dimitrov
ICQ : 110064098

Report
Re: VB6 Detect Keypress Posted by KDivad Leahcim on 19 Jun 2004 at 8:51 AM
: : I was wondering what the code is to detect a keypress. I need it to start practicing making games using VB6.
: :
: Interesting question.
: I think I can help you on this one.
: The first, easy, but not so good-resulted method is to use the events of your Visual Basic project form:
: ---
: Private Sub Form_KeyPress(KeyAscii As Integer)
: If KeyAscii = vbKeyUp Then MovePlayerUp
: If KeyAscii = vbKeyDown Then MovePlayerDown
: If KeyAscii = vbKeyLeft Then MovePlayerLeft
: If KeyAscii = vbKeyRight Then MovePlayerRight
: End Sub
: ---
: And if you want to detect when the user RELEASES the key:
: ---
: Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
: 'Code Here
: End Sub
: ---
: The bad thing on using this method is that you cannot detect two keys
: pressed at a time.
:
: Now...the really good method, the second method.
: Put this in a module to define the key pressing function of the WinAPI:
: ---
: Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
: ---
: And then, when you want to detect if a key is pressed:
: ---
: If GetAsyncKeyState(vbKeyUp) < 0 Then MovePlayerUp
: If GetAsyncKeyState(vbKeyDown) < 0 Then MovePlayerDown
: If GetAsyncKeyState(vbKeyLeft) < 0 Then MovePlayerLeft
: If GetAsyncKeyState(vbKeyRight) < 0 Then MovePlayerRight
: ---
:
: Good Luck!
: ---------------
: Mihail Dimitrov
: ICQ : 110064098
:
:


Two cents:

KeyDown, not KeyPress. And you can use KeyDown for multiple keys. In fact, you can do so easier and more reliably than with GetAsyncKeyState because the API needs a loop or timer.
Report
Re: VB6 Detect Keypress Posted by rubie on 31 Jan 2010 at 4:05 AM
With timer is better, because if the computers gets faster, the control will react stronger. If you have a timer, this is no problem



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.