I need to make 3 keys (1,2,3) to pause the program while it is running, start it again, and exit when its done. I figured i'd have to use keypress but i dont know where to start.
Printable View
I need to make 3 keys (1,2,3) to pause the program while it is running, start it again, and exit when its done. I figured i'd have to use keypress but i dont know where to start.
Is this what you wanted?
VB Code:
Private Sub Form_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case Is = vbKey1 MsgBox "Pressed 1" 'code for KeyPressed = 1 Case Is = vbKey2 MsgBox "Pressed 2" 'code for KeyPressed = 2 Case Is = vbKey3 MsgBox "Pressed 3" 'code for KeyPressed = 3 End Select End Sub
i dunno what your program does, but if there a lot of process,
you might want to think of putting a least one DoEvents
to let the user do a KeyPress!
Here's the deal with the program. This program runs tests on power supply units. The information from the test is stored in a text box. I need Key 1 to Pause the program, Key 2 to Restart it, and Key 3 to Exit. Im working with old QBasic code, about 13 yrs old, and i am converting it to VB and there's no notes anywhere on the code so im having some trouble. The person who wrote the QB code set the keys as:
On Key(1) GoSub CSOF
On Key(2) GoSub PAUSE
On Key(3) GoSub MANUAL_INPUT
On Key(4) GoSub ADJUST
On Key(5) GoSub RESTART
CSOF being a line to jump to, the same for pause, etc.
CSOF is the exit code.
I figured i could rewrite these lines into sub routines but i don't know how to get the Keys to work properly. I tried running a test for using the number key 1 as an exit button with little success.
Private Sub Form1_Keypress(Ascii As Integer)
If KeyAscii = vbKeyReturn Then
Unload Me
End If
End Sub
I don't know how i would go about pausing the program or restarting it. Any thing else you'd like to know?
here is an API call. I've never used it but it is supposed to
pause program execution.
Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
i need it to pause until i hit a key to restart the program.
How are you defining PAUSE.
If it is program execution, you can handle it in the Form_KeyPress event or
If you want to clear the screen use a loop in the event to set
visible = false for all the controls on the form
This does not work because, eitherQuote:
Private Sub Form1_Keypress(Ascii As Integer)
If KeyAscii = vbKeyReturn Then
Unload Me
End If
End Sub
(a) You do not have the KeyPreview property of the form set to true
or
(b) The first focus on load is a command button
If the latter, I suggest you set the tabindex of label on your form to 0, so that the focus begins from there.
Hope I am making some sense
makes a ton of sense, but nothing still. no command button, BUT you were right about the KeyPreview property being false. I set that to true and nothing still. But thats ok, im sure i will figure that out....im more bothered by the pausing of the program and starting it from where it left off again......
If your app does intensive work, as Sebs mentioned, have well spaced DoEvents, to capture the Keystroke.
Once the keystroke is captured, the easiest (and the least preferrable) way is to simulate a single "CTRL+ALT+DEL" using Sendkeys or something else. This effectively suspends (pauses) the OS. Of course if you want to run other apps while fixing equipment, hooking up cables, etc are done, then of course......
This is the old QBasic code with the pause being called:
On Key(2) GoSub PAUSE 'At beginning of program
PAUSE:
ST11! = Timer
Call KY
ST! = ST11!
Return
On Key(5) GoSub RESTART
RESTART:
START_FL = 5
Return
RESTART1:
Close
TOTAL_STATUS$ = "Failed"
Call SOF
Color 14, 0
CLS
Call Init
TI! = Timer
GoTo 108
Now i know about the timer but it doesn't really tell mehow it's used, also it would help if i knew more QBasic. but this is basically how its done in the old program and im having trouble replicating it.
Tried using the timer, Gets stuck!!!
A point: If the purpose of the pause is to give you time to do some physical changes to the Machine, Can you provide a long
enough PAUSE interval, say 5 minutes, or something, if so then you could "pause"
execution on receiving a keystroke like so
VB Code:
Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long Sub Pause(Duration As Double) 'Pause (300) 'pauses for 5 minutes Dim start As Double start# = GetTickCount 'store milliseconds since system start Do: DoEvents On Error Resume Next Loop Until GetTickCount - start# >= (Duration# * 1000) 'loop until the actual time (minus stored time) is greater than or equal to the duration (seconds * 1000 = milliseconds) End Sub
But if you finish your job in less than 5 mins, you still have to wait till this loop gets through.
helping you?
I was thinking about that.....ill have to find out the average time a pause lasts and just use that for the time being. I have other things i need to deal with at the moment. Someone mentioned to me using an API function and a sleep function on top of that. Haven't looked into it yet. Thanks KayJay ill try that out for now but i know i will have to change it eventually. Any other ideas would be great.
This is a long thinking-off-head post. So please bear with me!. It seems to work on my machine.
You have TWO apps running
One is your application: APP 1
The other has two buttons, "PAUSE", "RESTART": APP 2
In APP 1 have timer set to, say, 1 second.
Every second, in the timer event, check for a string value in a file "c:\pause.txt"
If the value is "False", it exits the subroutine
When you click the "PAUSE" button in APP 2, the "False" value is changed to "TRUE"
When the timer event fires again it reads a "TRUE" value and runs an indefinte loop.
When you press the "RESTART" button in APP 2, the value is reset to "FALSE".
The Timer event now reads a "False" value and exits the sub routine
Think this will work for you?
That may work, ill have to try it out. Can you implement that same idea in one app? cause it makes a lot of sense. Say press pause, it jumps to the timer event somewhere in the App and gets looped, then the second you hit restart, it jumps back to where it left off. I think we're getting somewhere. haha. im gonna try this.
Ok so i can't think right now my mind is going way to nuts, cant even think of how to code this at the moment. Ill give it a shot when i go home tonight. Thanks a ton kayjay. I shall post my results later.
This is working for me fine now. I dont know why, but 4 DoEvents seem to do the trick
VB Code:
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
Check it out and let me know
Take care
Hey thanks. I just tried it out and it works for me so far. I still wanna try the timer idea. Also....tell me what you think of this?
What if i have Key 2 create a message box to pop up with just a simple OK. Wouldn't this freeze the program until ok is clicked again? just a quick thought.
Message box freeze the program when it is run not compiled but once compiled it will only stop the module from where is displayed but not timers or other procedures already running.Quote:
Originally posted by runt1128
What if i have Key 2 create a message box to pop up with just a simple OK. Wouldn't this freeze the program until ok is clicked again? just a quick thought.