Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do Loop, Sleep and Mouse Button
#1
I have code laid out as follows:

Do

   Do
     ...some code
   Loop
     Sleep
 
  Do
    ....some more code
   Loop
      Sleep
Loop Until _MouseButton(1)

What happens with this code is that I can loop between two screens of data results ad infinitum and linger on the screens as long as I like before triggering the 2nd screen. The plan was to exit using a Left Button click alone, but what's happening is that I need to hold down any key and then the Left Button click. That holding down any key has the screens rapidly flipping until the Left Button click.  I believe I need a different mechanism than "Sleep" to allow each Screen of data to display as long as the user likes before presenting the next screen of data. I don't think _Delay or _Limit would work here???
Reply
#2
What gets you out of the 2 inner loops? Does "some code" and "some more code" have an Exit Do condition?

How bout

'data screen
sleep
'data screen
sleep
.
.
.
'end
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#3
Code: (Select All)
While -1
    Do
        While _MouseInput: Wend
        '...some code
        Locate 1, 1: Print "Hi, I'm in screen 1. Press 2 then any key for screen 2 or left click to exit."
        If InKey$ = "2" Then Cls: Exit Do
        If _MouseButton(1) Then Exit While
    Loop
    Sleep
    Do
        While _MouseInput: Wend
        '....some more code
        Locate 1, 1: Print "Hi, I'm in screen 2. Press 1 then any key for screen 1 or left click to exit."
        If InKey$ = "1" Then Cls: Exit Do
        If _MouseButton(1) Then Exit While
    Loop
    Sleep
Wend

Keep in mind with this structure you still need to press "any key" after pressing a 1 or 2 to switch screens, but when the prompt is visible, a left mouse click will exit the outer wend loop. Also note that if you press 1 and don't press "any key" the mouse is no longer enabled as coded. You are in a dead zone until "any key" is pressed as SLEEP doesn't poll the mouse.

Pete
Reply
#4
Code: (Select All)
Do
Do
x = x + 1
Locate 1, 1: Print x
Loop Until x Mod 100 = 0 'a natural exit condition for this loop
If PauseForInput = -1 Then Exit Do

Do
y = y + 1
Locate 2, 1: Print y
Loop Until y Mod 100 = 0 'again a way for this loop to end
If PauseForInput = -1 Then Exit Do
Loop


Function PauseForInput
_KeyClear
Do
k = _KeyHit: If k > 0 Then PauseForInput = k: Exit Function
While _MouseInput: Wend: If _MouseButton(1) Then PauseForInput = -1: Exit Function
_Delay .05 'not a high priority loop here; play nice with background processes
Loop
End Function


My solution to this type little problem.  See if it makes sense to you and if it's simple enough to understand.  Wink

Instead of your SLEEP statement, I swapped in a simple little pause routine that reports if the mouse was clicked or if a key was pressed. On a key press, it simply moves on to the next loop. On a mouseclick (-1), it exits the loop. It's that easy of a process.
Reply
#5
That's a nice example, basically what my routines do but without using a full keyboard and mouse library.

Honestly, Dim, if you don't mind studying the code a bit, in the long run abandoning SLEEP and replacing it with Steve's routine will pay dividends far into the future.

Pete
Reply
#6
A Pause Function - cool. Perhaps we should add _Pause to our lexicon of QB64PE syntax. Thanks for that suggestion Steve

bplus, each of the inner Do Loops are drawing data from two different files, so the Do is actually Do While Not EOF(x)

Pete, I did have something similar using Inkey$ but I detected the MouseButton at the end of the main outer loop so things didn't go well. As mentioned the MouseButton didn't seem to be recognized. I should have had the Inkey$ and MouseButton work in tandem as you suggested. Not sure why I didn't think of that...too many beers. 

I very much appreciate the help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Setting mouse to ignore outside of _NewImage PhilOfPerth 11 710 12-18-2025, 07:20 PM
Last Post: Pete
  Exiting sub while inside a loop PhilOfPerth 5 501 12-05-2025, 09:40 AM
Last Post: PhilOfPerth
  Using modulo to loop through lists fistfullofnails 3 705 09-03-2025, 11:50 PM
Last Post: fistfullofnails
  Stopping repeated mouse-key press PhilOfPerth 8 845 09-02-2025, 11:28 PM
Last Post: Pete
  _MessageBox ignores default button johngreening 2 597 04-27-2025, 07:34 PM
Last Post: hsiangch_ong

Forum Jump:


Users browsing this thread: 1 Guest(s)