Home All Groups Group Topic Archive Search About

KeyPress event help needed

Author
2 Feb 2006 5:43 PM
RC Bob
I am using VB6 to write programs to control some lab equipment. I am trying
to read a keypress to end a routine in a click event. What I want is to have
a label that says "Press 'E' to end the calibration"  and return to the Form1
main screen. The operator would then click another button to start another
routine.  How can I read a KeyPress and return the ascii code to use in this
manner?  I have used a sub to read a keypress on Form1 using

Private Sub Form_Keypress(KeyAscii As Integer)
    Print KeyAscii
    If KeyAscii=69 Then End
End Sub

This works but doesn't help me.

When I try to use the KeyAscii in my click event it won't do anything.

Can this be done?

Thanks,

RC Bob

Author
2 Feb 2006 5:51 PM
Karl E. Peterson
RC Bob wrote:
Show quoteHide quote
> I am using VB6 to write programs to control some lab equipment. I am
> trying to read a keypress to end a routine in a click event. What I
> want is to have a label that says "Press 'E' to end the calibration"
> and return to the Form1 main screen. The operator would then click
> another button to start another routine.  How can I read a KeyPress
> and return the ascii code to use in this manner?  I have used a sub
> to read a keypress on Form1 using
>
> Private Sub Form_Keypress(KeyAscii As Integer)
>     Print KeyAscii
>     If KeyAscii=69 Then End
> End Sub
>
> This works but doesn't help me.
>
> When I try to use the KeyAscii in my click event it won't do anything.
>
> Can this be done?

Normally, the KeyPress event would be fired in the control that has focus at
the time.  To see it at the form level, as you've attempted to do here, set
the form's KeyPreview property to True.
--
Working without a .NET?
http://classicvb.org/
Author
2 Feb 2006 6:22 PM
Saga
There are two fundamental problems with your algorithm.

The first is that once you have the keypress you do not
stop the process, rather you end the entire program.

Second, if the user presses E it is fine, but if s/he presses
e? You are not taking into account upper lower case
entries.

When you say that you want to "end a routine in a click event",
do you mean that when you click the button (assumiing it is a
button) the process starts and won't end until you press the E?
Or do you want to end the program when you press E? If you
want to end the program, is the process in the click event still
running or has it finishedor you simply don't care?

If all you want to do is end the program, then try these changes:

Private Sub Form_Keypress(KeyAscii As Integer)
    Print KeyAscii
    If (KeyAscii and 223) = 69 Then
       unload me
    end if
End Sub

If the process is still going, that is code is still executing in the
click
event, then you need something a little more elaborate:

private blnEndProcess as boolean


Private Sub Form_Keypress(KeyAscii As Integer)
    Print KeyAscii
    If (KeyAscii and 223) = 69 Then
       blnEndProcess = true
    end if

End Sub

This sets the flag that will tell the process to stop.


Now in the click event...


'You might something like

   do
     'Calibration code here. Somewhere in here you'll
     'set the blnCalibrationDone flag, if at all.


     'Now before the loop end do this:
     doevents
     'The above allows processing of other events,
     'like for instance, the key press.
   loop until blnCalibrationDone or blnEndProcess

   'Now the question as to what you want to do after the calibration
   'process is stopped. If you want to the program to wait till the
   'user starts another calibration, then leave the next line commented.
   'unload me

If you want the app to exit after calibration is ended, then remove the
comment from the last line. Note that the use of the End statement is
not recommended to end a program.

I hope this is useful to you, as there are some issues that you don't
mention, as the questions above indicate.

All of this is in addtion to what Karl recommends <g>

Good luck!!
Saga


Show quoteHide quote
"RC Bob" <RC***@discussions.microsoft.com> wrote in message
news:E60C94AF-E494-4EE3-A2C6-76161CF6D0E3@microsoft.com...
>I am using VB6 to write programs to control some lab equipment. I am
>trying
> to read a keypress to end a routine in a click event. What I want is
> to have
> a label that says "Press 'E' to end the calibration"  and return to
> the Form1
> main screen. The operator would then click another button to start
> another
> routine.  How can I read a KeyPress and return the ascii code to use
> in this
> manner?  I have used a sub to read a keypress on Form1 using
>
> Private Sub Form_Keypress(KeyAscii As Integer)
>    Print KeyAscii
>    If KeyAscii=69 Then End
> End Sub
>
> This works but doesn't help me.
>
> When I try to use the KeyAscii in my click event it won't do anything.
>
> Can this be done?
>
> Thanks,
>
> RC Bob
Author
2 Feb 2006 6:35 PM
Karl E. Peterson
Saga wrote:
> There are two fundamental problems with your algorithm.
>
> The first is that once you have the keypress you do not
> stop the process, rather you end the entire program.

<shudder>

Didn't even notice that.
--
Working without a .NET?
http://classicvb.org/
Author
2 Feb 2006 6:56 PM
Saga
yeah, those Ends are sneaky <g>

Saga

Show quoteHide quote
"Karl E. Peterson" <k***@mvps.org> wrote in message
news:OsBX7dCKGHA.1288@TK2MSFTNGP09.phx.gbl...
> Saga wrote:
>> There are two fundamental problems with your algorithm.
>>
>> The first is that once you have the keypress you do not
>> stop the process, rather you end the entire program.
>
> <shudder>
>
> Didn't even notice that.
> --
> Working without a .NET?
> http://classicvb.org/
>
>
Author
2 Feb 2006 7:06 PM
Karl E. Peterson
Saga wrote:
> yeah, those Ends are sneaky <g>

Best avoided "religiously", eh?
--
Working without a .NET?
http://classicvb.org/
Author
2 Feb 2006 7:10 PM
Rick Rothstein [MVP - Visual Basic]
> > yeah, those Ends are sneaky <g>
>
> Best avoided "religiously", eh?

Don't let David Mark hear you say that. <vbg>

Rick
Author
2 Feb 2006 7:16 PM
Karl E. Peterson
Rick Rothstein [MVP - Visual Basic] wrote:
>>> yeah, those Ends are sneaky <g>
>>
>> Best avoided "religiously", eh?
>
> Don't let David Mark hear you say that. <vbg>

Heavens forbid...
--
Working without a .NET?
http://classicvb.org/
Author
2 Feb 2006 7:21 PM
RC Bob
Well, I wasn't very clear.  I have 3 command buttons on my Form1. Button 1
goes to a calibration routine (valves open, air flows, and a massflowmeter is
calibrated by the user in the click event).  When the flowmeter is
calibrated, the user has to have a way to stop the click event and return to
Form1.  Then, he would click button 2 to run a product test, then have a
conditional yes/no to return to Form1 again.  Third button would contain
shutdown code and end program.
So what I need is to end the command 1 click event by reading a keypress,
end return to Form1 again. I don't know how to get the E key recognized (I
would account for lower case e). I had the form keypress set to true.  Hope
that is clearer. Can the KeyPress be read within a click event?  I am not a
conventional VB programmer, just a microbiologist that has written some
process control programs within in a single click event.

Thanks.

Show quoteHide quote
"Karl E. Peterson" wrote:

> Saga wrote:
> > There are two fundamental problems with your algorithm.
> >
> > The first is that once you have the keypress you do not
> > stop the process, rather you end the entire program.
>
> <shudder>
>
> Didn't even notice that.
> --
> Working without a .NET?
> http://classicvb.org/
>
>
>
Author
2 Feb 2006 7:44 PM
Mike Williams
"RC Bob" <RC***@discussions.microsoft.com> wrote in message
news:6B3F69FB-8A1E-45A2-BB79-0C825F82A047@microsoft.com...

> So what I need is to end the command 1 click event by
> reading a keypress, end return to Form1 again.

What code have you got in the Command1 Click event? VB runs in a single
thread and if you have code in there running in a closed loop then the
user's actions (clicks and things) will be ignored. There are various ways
to overcome this little problem (for example issue a "DoEvents" every so
often within the closed loop). Post a sample of your code.

Mike
Author
2 Feb 2006 8:09 PM
J French
On Thu, 2 Feb 2006 11:21:30 -0800, =?Utf-8?B?UkMgQm9i?=
<RC***@discussions.microsoft.com> wrote:

>Well, I wasn't very clear.  I have 3 command buttons on my Form1. Button 1
>goes to a calibration routine (valves open, air flows, and a massflowmeter is
>calibrated by the user in the click event).  When the flowmeter is
>calibrated, the user has to have a way to stop the click event and return to
>Form1.  Then, he would click button 2 to run a product test, then have a
>conditional yes/no to return to Form1 again.  Third button would contain
>shutdown code and end program.
>So what I need is to end the command 1 click event by reading a keypress,
>end return to Form1 again. I don't know how to get the E key recognized (I
>would account for lower case e). I had the form keypress set to true.  Hope
>that is clearer. Can the KeyPress be read within a click event?  I am not a
>conventional VB programmer, just a microbiologist that has written some
>process control programs within in a single click event.

Ah !

When you are in a 'tight loop' like frantically running code in a
'click event' then your App goes deaf to Windows Messages
- like 'someone has pressed a key'

To get round that you use DoEvents
- typically in a While/Wend  Do/Until  For/Next Loop

That raises the problem of re-entrancy
- which is easily solved
- but it allows your App to respond to clicks ... and key presses

Close your current App and run up a really simple test program
- say two Buttons, one looping a few 100,000 times, Form key
previewing and see what happens

You'll get the picture