: 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!