By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,097 Members | 1,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,097 IT Pros & Developers. It's quick & easy.

interrupt infinite loops with keydown?

P: 19
hello, i am new to visual basic 2005 and i am making a few programs with the do loop function.

i am trying to make my prog exit an infinite loop when a key on the keyboard is pressed, but i have no idea how to code this. can anyone help me? i'd be very appreciative.
Apr 7 '07 #1
Share this Question
Share on Google+
10 Replies


Expert 5K+
P: 8,434
hello, i am new to visual basic 2005 and i am making a few programs with the do loop function.

i am trying to make my prog exit an infinite loop when a key on the keyboard is pressed, but i have no idea how to code this. can anyone help me? i'd be very appreciative.
One simple method is to create a global variable. I have done this a few times, creating a global boolean called UserWantsOut. You may prefer another name, of course.

In your loop, just say to drop out as soon as that variable is True, either by a while/until clause, or an If test inside the loop.

In the KeyDown or KeyPress event, if your conditions are met (specific key, or whatever) then simply set UserWantsOut = True.

Note that if things in your version still resemble VB6, you may nee to have a DoEvents inside your loop to allow Windows to go and look at the keyboard.
Apr 8 '07 #3

P: 19
One simple method is to create a global variable. I have done this a few times, creating a global boolean called UserWantsOut. You may prefer another name, of courseOne simple method is to create a global variable. I have done this a few times, creating a global boolean called UserWantsOut. You may prefer another name, of course.

In your loop, just say to drop out as soon as that variable is True, either by a while/until clause, or an If test inside the loop.

In the KeyDown or KeyPress event, if your conditions are met (specific key, or whatever) then simply set UserWantsOut = True.

Note that if things in your version still resemble VB6, you may nee to have a DoEvents inside your loop to allow Windows to go and look at the keyboard.


this is good, but i guess i forgot to ask how to properly execute a keydown/keypress event within said loop. how would i set up the doevents statement?
Apr 8 '07 #4

Expert 5K+
P: 8,434
this is good, but i guess i forgot to ask how to properly execute a keydown/keypress event within said loop. how would i set up the doevents statement?
Not quite sure what you mean. The KeyDown or KeyPress event is fired automatically by Windows, when the even happens. You don't execute it anywhere in your code.

In VB6, if your code is busy doing something, these events may not fire off. A DoEvents statement tells Windows "take a moment to check what else needs to be done". So you tend to throw them in anywhere your code is doing lots of work, to avoid holding things up. In earlier versions of Windows this was necessary to avoid locking up the whole system. These days, in XP and presumably Vista, it only prevents you from holding up your own application. You may notice sometimes an application will still be working, but its user interface freezes. DoEvents is used to allow things like the user interface to be updated.

However, I don't know whether this still applies in later versions of VB. You will need to check your documentation. Also check the doco for how to write an event procedure. In this case, probably all your event procedure needs to do is set your global variable to indicate the user wants to stop.
Apr 9 '07 #5

P: 19
Not quite sure what you mean. The KeyDown or KeyPress event is fired automatically by Windows, when the even happens. You don't execute it anywhere in your code.

In VB6, if your code is busy doing something, these events may not fire off. A DoEvents statement tells Windows "take a moment to check what else needs to be done". So you tend to throw them in anywhere your code is doing lots of work, to avoid holding things up. In earlier versions of Windows this was necessary to avoid locking up the whole system. These days, in XP and presumably Vista, it only prevents you from holding up your own application. You may notice sometimes an application will still be working, but its user interface freezes. DoEvents is used to allow things like the user interface to be updated.

However, I don't know whether this still applies in later versions of VB. You will need to check your documentation. Also check the doco for how to write an event procedure. In this case, probably all your event procedure needs to do is set your global variable to indicate the user wants to stop.
ok so this is what i have so far using your tips:

If bandw.Checked = True And infinite.Checked = True Then
Do Until X = 117
BlackandWhite()

my.application.doevents ' <--?

If UserWantsOut = True Then
Exit Do
End If

Loop

so do i set up a public sub doevents() and code userwantsout = true there? as of right now, the doevents statement does nothing when i press a key.
Apr 9 '07 #6

Expert 5K+
P: 8,434
Expand|Select|Wrap|Line Numbers
  1. If bandw.Checked = True And infinite.Checked = True Then
  2.   Do Until X = 117
  3.     BlackandWhite()
  4.     my.application.DoEvents   ' <--?
  5.     If UserWantsOut = True Then
  6.         Exit Do
  7.     End If
  8.   Loop
so do i set up a public sub doevents() and code userwantsout = true there? as of right now, the doevents statement does nothing when i press a key.
I think you had better just forget that I mentioned DoEvents. I've been doing some research at the MS website, and I don't think it even exists any more in your version. Sorry I confused things by throwing that in.

The point is, now that you have your loop going, and checking the UserWantsOut variable (which ideally should be global in scope) all you need is the KeyPress or KeyDown event procedure. You need to check the documentation for how to create this, but it is one of the most basic (no pun intended) things you will do in VB. To create a user interface, you draw forms and controls, then you put code in the event procedures to be executed when certain events occur.

So, when you work out how to create the KeyDown event procedure (or KeyPress if you prefer) for the form, just put in there a line of code to set UserWantsOut to True.

Note, it might also be a good idea to set it back to False before doing the Exit Do. Otherwise, if you need to run the loop again, you might find yourself wondering why it keep dropping out straight away. Ideally, you should clear the value of this flag before you start the loop, just to be safe.
Apr 9 '07 #7

P: 19
I think you had better just forget that I mentioned DoEvents. I've been doing some research at the MS website, and I don't think it even exists any more in your version. Sorry I confused things by throwing that in.

The point is, now that you have your loop going, and checking the UserWantsOut variable (which ideally should be global in scope) all you need is the KeyPress or KeyDown event procedure. You need to check the documentation for how to create this, but it is one of the most basic (no pun intended) things you will do in VB. To create a user interface, you draw forms and controls, then you put code in the event procedures to be executed when certain events occur.

So, when you work out how to create the KeyDown event procedure (or KeyPress if you prefer) for the form, just put in there a line of code to set UserWantsOut to True.

Note, it might also be a good idea to set it back to False before doing the Exit Do. Otherwise, if you need to run the loop again, you might find yourself wondering why it keep dropping out straight away. Ideally, you should clear the value of this flag before you start the loop, just to be safe.

i did get the application.doevents to work, and i did get my prog to respond the way i wanted it to with a mouseclick event, but the keydown event isnt working the same way.

Private Sub colorprog_KeyDown (ByVal sender As Object, ByVal e As System.Windows.Forms.KeyeventArgs) Handles Me.Keydown

Label1.text = "test" ' test label
userwantsout = true
end sub

this works when it is mouseclick instead of keydown. any tips?
Apr 9 '07 #8

Expert 5K+
P: 8,434
i did get the application.doevents to work, and i did get my prog to respond the way i wanted it to with a mouseclick event, but the keydown event isnt working the same way.

Private Sub colorprog_KeyDown (ByVal sender As Object, ByVal e As System.Windows.Forms.KeyeventArgs) Handles Me.Keydown

Label1.text = "test" ' test label
userwantsout = true
end sub

this works when it is mouseclick instead of keydown. any tips?
Um... if it was VB6 I would recommend setting the form's KeyPreview property to True. But I'm afraid you're getting into the technicalities of VB.Net now, and I'm not familiar with it.

One thing, though - do you have your options set to always require explicit variable declaration? (Note, again I'm assuming this option still exists in your version.)

If not, it's possible that this is not the same variable that is being checked in your other code. Although, if it works on a click but not a keypress, that's a bit odd.
Apr 9 '07 #9

P: 19
Um... if it was VB6 I would recommend setting the form's KeyPreview property to True. But I'm afraid you're getting into the technicalities of VB.Net now, and I'm not familiar with it.

One thing, though - do you have your options set to always require explicit variable declaration? (Note, again I'm assuming this option still exists in your version.)

If not, it's possible that this is not the same variable that is being checked in your other code. Although, if it works on a click but not a keypress, that's a bit odd.

hmm, not sure what you mean by explicit variable declaration, maybe it doesnt exist in my version, as you suggested. anything i do from here on is VB.NET stuff so we'll just end this thread here. thank you for all your help, i'm much further into solving this problem than i was. :)
Apr 10 '07 #10

Expert 5K+
P: 8,434
hmm, not sure what you mean by explicit variable declaration, maybe it doesnt exist in my version, as you suggested. anything i do from here on is VB.NET stuff so we'll just end this thread here. thank you for all your help, i'm much further into solving this problem than i was. :)
Fair enough.

It might be worth looking into the explicit declaration option, though. It can prevent a lot of problems caused by incorrect variable scope, or typing errors. Say you define a variable ABC, then in your code you accidentally use ABV. If you have not set up VB to insist that variables must be explicitly declared, then it will just go ahead and create a variable called ABV, and you'll be none the wiser until you eventually track down that pesky little bug that has been causing so much grief.

With the explicit declaration required, you would simply hit a compile error straight away, because ABV doesn't exist.

I had hoped that someone more familiar with the .Net flavour of VB might have jumped in with some comments by now.
Apr 10 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.