?
Solved

Interrupting an endless loop when a user presses a key

Posted on 2003-03-08
24
Medium Priority
?
381 Views
Last Modified: 2010-05-18
Hey guys hope you can help me on this, it's probably a lot easier than I'm making it.

I've been using QBasic for a while and now that I moved up to VB6, I miss the INKEY$ function.
Is there a way to recreate the following code snippet in VB6? When I try it, I just get endless loops.

ReactionTime& = 0                                    
response$ = INKEY$

DO UNTIL response$ = CHR$(121) OR response$ = CHR$(110)
      response$ = INKEY$
      ReactionTime& = ReactionTime& + 1
LOOP

Appreciate the help guys,

Moustro
0
Comment
Question by:Moustro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 6
  • +1
24 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8095610
try something like:



set the keypreview property of your form to true.

private looping as boolean
private ReactionTime as integer

public sub Form_Load()
  looping = true
  ReactionTime = 0
  do while looping
     ReactionTime = ReactionTime + 1
     DoEvents
  loop

end sub


public Sub Form_KeyPress(keyAscii as integer)
  if keyAscii = 121 then '' q
  ElseIf keyAscii = 110 then '' q
      looping = false
end sub
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8095922
0
 

Author Comment

by:Moustro
ID: 8100254
hey objects, thanks for the help, your code looks good. However, I need to have this question refunded so I can post it in the VB section. Please respond if this is ok.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:SpideyMod
ID: 8100275
A request for refund and deletion has been made.  I intend to honor this request within 72 hours unless I hear from the experts involved.  Thanks.

SpideyMod
Community Support Moderator @Experts Exchange
0
 
LVL 92

Expert Comment

by:objects
ID: 8100296
> your code looks good.

Did you try it?
Why need to repost if it does the job?

0
 

Author Comment

by:Moustro
ID: 8107911
ok, i got to try the code, but I'm calling this function very often and am getting a stack overflow error. Is there a way to clear the stack once the loop is interrupted or am i missing something?
0
 

Author Comment

by:Moustro
ID: 8107939
ok, i got to try the code, but I'm calling this function very often and am getting a stack overflow error. Is there a way to clear the stack once the loop is interrupted or am i missing something?
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 0 total points
ID: 8108010
Moustro, I think it is fair that you get your question exposed to more VB expert in VB forum. You can go ahead and post your question there, your points will be refuned to you. objects can help you from there if he want too.
0
 
LVL 92

Expert Comment

by:objects
ID: 8108012
the loop doesn't get interrupted, it simply ends.
0
 

Author Comment

by:Moustro
ID: 8108042
ok, will repost
0
 
LVL 92

Expert Comment

by:objects
ID: 8108043
Am I breaking the rules again? :)
0
 
LVL 92

Expert Comment

by:objects
ID: 8108054
Oh that seems fair??
0
 

Expert Comment

by:SpideyMod
ID: 8108077
Moustro,
You've just lost 120 points by selecting the comment from  kennethxu.  I was already on this thread and you simply needed to ask for a refund.  I am refunding the points to you now.
Make sure after you re-ask the question in the VB area, you post the new question's URL back in this question so objects can continue to assist.

Thanks.

All 120 points returned to questioner on this question.

0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8108093
>> Oh that seems fair??
don't worry, I will get points refunded.
0
 

Expert Comment

by:SpideyMod
ID: 8108094
objects,

If you aren't invited to a question in the VB area with the original 120 points within 72 hours, let me know and I'll make sure you get points for your assistance here.

SpideyMod
Community Support Moderator @Experts Exchange
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8108101
thanks Spidey to refund the points.
0
 

Expert Comment

by:SpideyMod
ID: 8123733
Moustro,
I asked you to invite objects to your new question thread once you created it by posting the new link in this question so objects would be notified.  You haven't done that and that does not make me happy.  On top of that, it seems plenty of experts have tried to assist you with your new question and you haven't provided them with a single bit of feedback.  You are quickly irritating those that are trying to assist you, experts and a moderator alike now.  I am posting in your new question and will monitor it's progress.  If you need help figuring this site out, just drop a note here and I'm happy to provide.

objects,  the new question is located at (if you dare to try and help again):
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20545773.html

SpideyMod
Community Support Moderator @Experts Exchange
0
 
LVL 92

Expert Comment

by:objects
ID: 8123778
SpideyMod,

I've had enough of it, and the comments getting posted look basically the same as what I posted here.
Have given my 2c worth and it's been ignored so am moving on.

Thanks gor your help.
0
 

Expert Comment

by:SpideyMod
ID: 8123846
objects,
I know how you feel.  Tell ya what (if you'd like), if you subscribe to the other question and find that the answer selected matches your answer or close to it (assuming Moustro does indeed select an answer), let me know in either thread and I'm happy to award you the 120 points and an A for it.  My deepest apologies.

kennethxu,
In the future if someone is to be redirected to another area, it should be done before any expert responses have been made.  If responses have been made, we can tell them to post a 20 point question that "redirects" the experts in the other area to the "real" question, and that they will get their 20 points back as long as they indicate points will only be awarded in the original question.

Moustro,
In the future, ask someone how to proceed before committing to an action if you are unsure.  Try the experts in your question first.  If no experts have responded to your question or you don't like what the experts are saying, post in community support and a moderator will assist you.
Also, read my last comment if you haven't done so.

I hope this clears up this whole mess.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8132209
>> the loop doesn't get interrupted, it simply ends.
1. what happens when DoEvents, some event handling code calls this same routine again? stack overflow.

2. and your suggested code doesn't count the number of keypress in ReactionTime correctly, your code counts all events, not just keypress.

3. asker want the code end when y or n is pressed, not the otherway round.

here is what I think it should be:

Private Sub Form_KeyPress(KeyAscii As Integer)
response = KeyAscii
End Sub

Private Sub Form_Load()
Form1.KeyPreview = True
Me.Show
Call Routine
End Sub

Private Sub Routine()
ReactionTime& = 0
Do Until response = 121 Or response = 110
   response = 0
   if inProcess > 0 then return
   inProcess = 1
   DoEvents
   inProcess = 0
   if response > 0 then ReactionTime& = ReactionTime& + 1
Loop
Stop
End Sub
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8132361
>> In the future if someone is to be redirected to another area, it should be done before any expert responses have been made.

Spidey, OK! I'll take this as an order. But here are my opinions:

1. Asker posted question in wrong forum because he's new to EE. A professional expert should have let asker know about that in the first place. So the asker's question can be evaluated and answered by more VB experts and he can pick the best answer.

2. Posting 20 point referring question is unfair to experts in VB forum, they don't participate in JSP forum. Points they gain from JSP forum doesn’t contribute to their VB forum rating.

3. To experts in JSP forum, not much of them know VB programming as Objects and I do. Isn’t it an unfair competition?

4. EE is the only forum site I have visited that cannot move a discussion from one forum to another. I think this situation should be improved.

5. EE mods are judges, but unfortunately, we don’t have laws. It is so common that I get different instructions from different mods. I cannot understand that why EE cannot post clear instruction to such common problems, procedures. EE is no longer a new site.

6. I think we can be much friendlier to our new members like Moustro. Hope he isn’t scared and will come back.

Thanks,

Kenneth
0
 

Expert Comment

by:SpideyMod
ID: 8132456
kennethxu,
Not trying to make it sound like an order, just trying to avoid this in the future.  I don't think there's a need to take it as an order either, just trying to guide.

1) I concur
2) I concur again, however, it is the questioner that determines where the question should go.  I realize new questioners sometimes post in the wrong area and if the first expert comes along and helps them see where they could be posting the question, great.  However, I've also seen new members request a refund after 4 experts have answered, stating they want to re-ask the question and either ehty do not, or they do but don't invite the experts helping them to join.
3) I don't now what you mean by unfair competition, but I think we've all run across a question posted in the wrong area and tried to help.  It happens.  Granted you tried to do the right thing by guiding them to the correct location and it ended up going bizarre on us.  Perhaps if you still want to guide them to another area, you can include the "PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER" disclaimer.
4) I agree, please put it in the suggestions area: http://www.experts-exchange.com/Community_Support/Suggestions/
5) This is being worked on as we speak.  The mod program is still young and as it is a volunteer basis, there's high turn-around.  The community is growing and we are trying to respond as best we can.
6) I hope so too.  There's much talk about the top experts needing to take a certain level of responsibility to help the new.  Some of the problem is that a good percentage of new people have burned the top experts.  They should know it's all part of the package, but some do not.  We've got good and bad members and we've got good and bad experts.

I think we're mostly on the same page.  I'm just trying to work withing the framework that we have at this site and prevent conflicts.  I hope you understand.
0
 
LVL 92

Expert Comment

by:objects
ID: 8132699
kennethxu,

1. If you believe the question was posted in the wrong forum then why are you now posting a comment about it here and not with the new question in the vb forum?

2. Often Java questions are posted here which have nothing to do with JSP, but you still regularily comment on. Shouldn't you instead be redirecting these questions also to the correct forum.

It's not black and white, if someone asks a question and I have information I feel may help them then I post it.
After all isn't it the purpose of this site to help people.

:)
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8148437
Spidey,
I have posted the suggestion. Yes, you are right, we're mostly on the same page. I appreciate your help and explanation.
I just want to make my point clear that I strongly believe askers should be given the freedom to move their question to the right place if it was mis-posted in a completely irrelevant forum. Yes, we have accidents, but we are stilling driving cars. The community need new members.

Objects,
To your questions,
1. it's not I believe, it's truth. no matter where I post my comment, asker will see it.
2. JSP is Java Server Page
>> if someone asks a question and I have information I feel may help them then I post it.
yes, we all do.
>> After all isn't it the purpose of this site to help people.
then, what's wrong let asker post his question in the right place so he/she gets more help :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New style of hardware planning for Microsoft Exchange server.
Check out the latest tech news, community articles, and expert highlights in August's newsletter.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question