|
-
Apr 2nd, 2002, 11:31 AM
#1
Thread Starter
Lively Member
Keyboard
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.
-
Apr 2nd, 2002, 11:48 AM
#2
Frenzied Member
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
-
Apr 2nd, 2002, 11:54 AM
#3
Frenzied Member
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!
-
Apr 2nd, 2002, 12:06 PM
#4
Thread Starter
Lively Member
here it is
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?
-
Apr 2nd, 2002, 12:14 PM
#5
Hyperactive Member
api
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
-
Apr 2nd, 2002, 12:17 PM
#6
Thread Starter
Lively Member
api
i need it to pause until i hit a key to restart the program.
-
Apr 2nd, 2002, 12:41 PM
#7
Frenzied Member
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
-
Apr 2nd, 2002, 12:54 PM
#8
Frenzied Member
Private Sub Form1_Keypress(Ascii As Integer)
If KeyAscii = vbKeyReturn Then
Unload Me
End If
End Sub
This does not work because, either
(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
-
Apr 2nd, 2002, 01:07 PM
#9
Thread Starter
Lively Member
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......
-
Apr 2nd, 2002, 01:07 PM
#10
Frenzied Member
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......
-
Apr 2nd, 2002, 01:15 PM
#11
Thread Starter
Lively Member
Maybe this can help.....
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.
-
Apr 2nd, 2002, 01:40 PM
#12
Frenzied Member
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?
-
Apr 2nd, 2002, 01:49 PM
#13
Thread Starter
Lively Member
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.
-
Apr 2nd, 2002, 01:58 PM
#14
Frenzied Member
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?
-
Apr 2nd, 2002, 02:07 PM
#15
Thread Starter
Lively Member
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.
-
Apr 2nd, 2002, 02:24 PM
#16
Thread Starter
Lively Member
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.
-
Apr 2nd, 2002, 02:24 PM
#17
Frenzied Member
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
-
Apr 2nd, 2002, 02:29 PM
#18
Frenzied Member
-
Apr 2nd, 2002, 02:32 PM
#19
Thread Starter
Lively Member
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.
-
Apr 2nd, 2002, 04:03 PM
#20
Hyperactive Member
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.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|