Beginner VB

Moderators: None (Apply to moderate this forum)
Number of threads: 1233
Number of posts: 2978

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

Report
Keyboard controls Posted by Runt1128 on 2 Apr 2002 at 8:39 AM
When my program runs, i need to be able to pause, start, and exit it whenever needed by using the keyboard. I know i have to use the KeyPress command and im guessing ascii to use which ever keys needed. But how do i go about setting this up? i read the msdn site and i need a little more info.
Report
Re: Keyboard controls Posted by hobby on 2 Apr 2002 at 9:37 AM
: When my program runs, i need to be able to pause, start, and exit it whenever needed by using the keyboard. I know i have to use the KeyPress command and im guessing ascii to use which ever keys needed. But how do i go about setting this up? i read the msdn site and i need a little more info.
:

Use the Form_KeyPress(KeyAscii as integer) Event. It's called whenever the form has focus and a key is pressed. (Note, if a textbox or picturebox or other control has the focus on the form, then the form will NOT receive the keypress, but that control will receive the keypress event). The KeyAscii is the Ascii number (A=68).

For example:
Private Sub Form_KeyPress(KeyAscii as Integer)
    Debug.print Chr(KeyAscii)  ' Output the keypress
    If Lcase(Chr(KeyAscii)) = "p"
        ' Code here to pause
    Elseif LCase(Chr(KeyAscii)) = "x"
        ' Code here to exit
    Elseif KeyAscii = 68 ' A is pressed, checking number instead of character.
        ' Code here to do something
    End if
End Sub


Report
Re: Keyboard controls Posted by Runt1128 on 2 Apr 2002 at 10:00 AM
Well i understand the syntax of the keypress command, but can you tell me how i would go about pausing a program using a single key? like the number 1.
Report
Re: Keyboard controls Posted by HardCoded on 2 Apr 2002 at 10:34 AM
: Well i understand the syntax of the keypress command, but can you tell me how i would go about pausing a program using a single key? like the number 1.
:
Well, I guess that you can use a little api. Search for it in the API text viewer:
Sleep - and the argument is in miliseconds.


Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
'This is to declare the function in the declaration section of the form

Private Sub Form_KeyPress(KeyAscii as Integer)
    If Chr(KeyAscii) = "1"
        Sleep 3000 'pauses the app for 3secs
    End if
End Sub


Hope this helps - This includes API, but its simple.

HardCoded

Report
Re: Keyboard controls Posted by Runt1128 on 2 Apr 2002 at 10:44 AM
I know i can use the API but from what im seeing i can pause it for a certain amount of time. Sorry i wasn't clear. I need to press a key to pause the program, stop it in its tracks.....then press another key to start it up again from where it left off.

: Well, I guess that you can use a little api. Search for it in the API text viewer:
: Sleep - and the argument is in miliseconds.
:
:
:
: Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
: 'This is to declare the function in the declaration section of the form
: 
: Private Sub Form_KeyPress(KeyAscii as Integer)
:     If Chr(KeyAscii) = "1"
:         Sleep 3000 'pauses the app for 3secs
:     End if
: End Sub
: 

:
: Hope this helps - This includes API, but its simple.
:
: HardCoded
:
:

Report
Re: Keyboard controls Posted by HardCoded on 2 Apr 2002 at 11:21 AM
: I know i can use the API but from what im seeing i can pause it for a certain amount of time. Sorry i wasn't clear. I need to press a key to pause the program, stop it in its tracks.....then press another key to start it up again from where it left off.
:
: : Well, I guess that you can use a little api. Search for it in the API text viewer:
: : Sleep - and the argument is in miliseconds.
: :
: :
: :
: : Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
: : 'This is to declare the function in the declaration section of the form
: : 
: : Private Sub Form_KeyPress(KeyAscii as Integer)
: :     If Chr(KeyAscii) = "1"
: :         Sleep 3000 'pauses the app for 3secs
: :     End if
: : End Sub
: : 

: :
: : Hope this helps - This includes API, but its simple.
: :
: :
Ok.I got it.In that case you can is put a timer runnig, with an api function that goes checking what keys are being pressed (you can find the code for this and adaptit in a keylogger tut) and then aply the func Sleep. This func doens stop every thing.

HardCoded
Report
Re: Keyboard controls Posted by Runt1128 on 2 Apr 2002 at 11:26 AM
: : : Well, I guess that you can use a little api. Search for it in the API text viewer:
: : : Sleep - and the argument is in miliseconds.
: : :
: : :
: : :
: : : Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
: : : 'This is to declare the function in the declaration section of the form
: : : 
: : : Private Sub Form_KeyPress(KeyAscii as Integer)
: : :     If Chr(KeyAscii) = "1"
: : :         Sleep 3000 'pauses the app for 3secs
: : :     End if
: : : End Sub
: : : 

: : :
: : : Hope this helps - This includes API, but its simple.
: : :
: : :
: Ok.I got it.In that case you can is put a timer runnig, with an api function that goes checking what keys are being pressed (you can find the code for this and adaptit in a keylogger tut) and then aply the func Sleep. This func doens stop every thing.
:
: HardCoded
:
Ok so use the same api function as stated above? And how do i know how long the timer should run for? never had to use one before?

Report
Re: Keyboard controls Posted by Runt1128 on 2 Apr 2002 at 11:32 AM
I think what im trying to say is....Does the timer have to be specified? or does it run continuously until the user ends the program?
Report
Re: Keyboard controls Posted by Runt1128 on 2 Apr 2002 at 12:29 PM
this works:

Dim mloop As Boolean

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKey2 Then 'pause
mloop = True
ElseIf KeyAscii = vbKey4 Then 'continue
mloop = False
ElseIf KeyAscii = vbKeyReturn Then
End
End If
End Sub

Private Sub Timer1_Timer()
Do While mloop = True
Debug.Print "looping"
DoEvents
DoEvents
DoEvents
DoEvents
If mloop = False Then
Exit Sub
End If
Loop
End Sub

Private Sub Form_Load()
Label1.TabIndex = 0
End Sub


Report
Re: Keyboard controls Posted by KDivad Leahcim on 3 Apr 2002 at 6:32 AM
: When my program runs, i need to be able to pause, start, and exit it whenever needed by using the keyboard. I know i have to use the KeyPress command and im guessing ascii to use which ever keys needed. But how do i go about setting this up? i read the msdn site and i need a little more info.
:

If there is a specific control that will always have the focus, put this code in it's KeyPress event. Otherwise, set the form's KeyPreview property to True and put this in the form's KeyPress event:
If LCase$(Chr$(KeyAscii)) = "p" Then
    Paused = Not Paused
ElseIf LCase$(Chr$(KeyAscii)) = "x" Then
    'exit code here
End If

If there is only one form, put this in it's general declarations section:
Private Paused As Boolean

Otherwise, change Private to Public and put it in a .bas module instead.

In any event (or loop) that controls the game, check the value of Paused to see if you need to execute the code. Here's a small psuedocode example:
Do
    If Not Paused Then
        UpdateEnemyPositions
        UpdatePlayerPosition
        RedrawScreen
    Else
        Sleep 100
    End If
    DoEvents
Loop

Or perhaps you would simply use it to ignore keypresses or mouse events.

Either way, using Sleep will freeze your app for the indicated amount of time. So use a small value for Sleep so your app can check to see if it needs to be unpaused. (Sleep will also cause your app to give up it's slice of CPU time so it doesn't bog down the system when it's paused.)

Hope this helps!



 

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.