|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
KeyPress event help neededto 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 RC Bob wrote:
Show quoteHide quote > I am using VB6 to write programs to control some lab equipment. I am Normally, the KeyPress event would be fired in the control that has focus at> 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? the time. To see it at the form level, as you've attempted to do here, set the form's KeyPreview property to True. 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 Saga wrote:
> There are two fundamental problems with your algorithm. <shudder>> > The first is that once you have the keypress you do not > stop the process, rather you end the entire program. Didn't even notice that. 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/ > > Saga wrote:
> yeah, those Ends are sneaky <g> Best avoided "religiously", eh?> > yeah, those Ends are sneaky <g> Don't let David Mark hear you say that. <vbg>> > Best avoided "religiously", eh? Rick Rick Rothstein [MVP - Visual Basic] wrote:
>>> yeah, those Ends are sneaky <g> Heavens forbid...>> >> Best avoided "religiously", eh? > > Don't let David Mark hear you say that. <vbg> 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/ > > > "RC Bob" <RC***@discussions.microsoft.com> wrote in message What code have you got in the Command1 Click event? VB runs in a single 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. 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 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 Ah !>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. 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
module
Best way to extract a word from a sentence Thesaurus & internationalisation Does this serial port device exist? Text box - Change/Lost Focus use VB6 Installing VB5 Application Causing Hardware Problems? color format Outlook Style Date Grouping Function result differs from same code for subroutine VB6 problem with IE7 |
|||||||||||||||||||||||