QB64 Phoenix Edition
Repeating mouse check - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: Repeating mouse check (/showthread.php?tid=2880)



Repeating mouse check - PhilOfPerth - 07-24-2024

I need to check my mouse-click positions several times, but seem to be stuck with its first location each time.
This is a simplified version of what I'm using; What am I doing wrong? (pardon the GoTo's)

Code: (Select All)
GetFirst: '                                            to find first mouse position
Locate 20, 30: Print "Choose first location"
m% = _MouseInput '                                     prepare mouse
If _MouseButton(1) Then '                              get left-mouse
    MX = _MouseX: MY = _MouseY '                       get horiz and vert position
    Print MX, MY
    GoTo GetSecond '                                   and go to GetSecond
Else GoTo GetFirst '                                   if left-mouse not pressed go back and try again
End If


GetSecond: '                                           to find second mouse position
While _MouseButton(1): Wend '                          wait for left-mouse to restore
Print "Choose second location"
'm% = _MouseInput '                                     prepare mouse (is this needed again?)
If _MouseButton(1) Then '                              get left-mouse again
    MX = _MouseX: MY = _MouseY '                       get new horiz and vert position
    Print MX, MY
    Sleep
Else GoTo GetSecond '                                  if left-mouse not pressed go back and try again
End If
Sleep



RE: Repeating mouse check - DSMan195276 - 07-24-2024

The issue is your `While _MouseButton(1): Wend`, it needs a `m% = _MouseInput` inside to keep it from being an infinite loop. (Ideally also a `_Limit` to avoid high CPU usage).

The way to think of the mouse handling is that every change to the mouse state (button press/release, position change, etc.) has to go to the back of a queue line and wait, they don't get immediately shown to the program. All the functions like `_MouseButton(x)`, `_MouseX`, `_MouseY`, etc. only show you the state that is currently first in the queue line. Unless you advance the queue line to the next state that is waiting, those function will continue to return the same values regardless of what the user does with the mouse (since any mouse changes simply add more entries to the back of the queue line, they don't change what is currently first)

Calling `_MouseInput` is how you advance the queue line to the next state, when it gets called those functions I mentioned (`_MouseButton(x)`, etc.) will start showing whatever the next event in the queue line is (or the same values, if there was nothing else in the queue line at the time). The return value of `_MouseInput` tells you whether anything was in the queue (so if it returns zero, there were no new changes to process).

The purpose of this system is so that you can't miss mouse events, regardless of how slow you check for them. If the user clicks really quickly or moves really quickly your program does not need to be checking the mouse hundreds of times a second to ensure you don't miss it. Rather, you can't miss the mouse event because the actual state changes are just put into the queue line, you'll eventually see them when processing through the queue using `_MouseInput` at whatever speed you want.


RE: Repeating mouse check - PhilOfPerth - 07-24-2024

ThanksDSMan.
That all sounded very encouraging, and I'm sure your right. 
I raced off to give it a try.
But when I inserted the m%=_mouseinput, as you suggested, and tried it, I got the first location, then an infinite loop of calls for my next mouse input. I'm still missing something (in the programme, I mean).

Edit: Ahah, got it! I was "GoTo-ing" back too far. I only needed to got back to after the While/wend loop.
I'll clean it up a bit and should be set to go. Thanks for the help.


RE: Repeating mouse check - Pete - 07-24-2024

@PhilOfPerth

GOTO gone...

Code: (Select All)
Cls: Locate 20, 30: Print "Choose first location: ";
Do
    While _MouseInput: Wend
    MX = _MouseX: MY = _MouseY
    If lb_status% Then If _MouseButton(1) = 0 Then lb_status% = 0 ' Mouse button released.
    If _MouseButton(1) And lb_status% = 0 Then
        lb_status% = 1 ' Mouse button down
        toggle` = toggle` - 1
        Select Case toggle`
            Case -1: Print MX; MY: Locate 21, 30: Print "Choose second location:";
            Case 0: Print MX; MY: Sleep: Cls: Locate 20, 30: Print "Choose first location: ";
        End Select
    End If
Loop
End

You need to press a key after the second coordinates are displayed. See SLEEP statement. You could change that to a delay or even a blank mouse click to get it to recycle.

GOTO Pete


RE: Repeating mouse check - PhilOfPerth - 07-24-2024

(07-24-2024, 09:08 PM)Pete Wrote: @PhilOfPerth

GOTO gone...

Code: (Select All)
Cls: Locate 20, 30: Print "Choose first location: ";
Do
    While _MouseInput: Wend
    MX = _MouseX: MY = _MouseY
    If lb_status% Then If _MouseButton(1) = 0 Then lb_status% = 0 ' Mouse button released.
    If _MouseButton(1) And lb_status% = 0 Then
        lb_status% = 1 ' Mouse button down
        toggle` = toggle` - 1
        Select Case toggle`
            Case -1: Print MX; MY: Locate 21, 30: Print "Choose second location:";
            Case 0: Print MX; MY: Sleep: Cls: Locate 20, 30: Print "Choose first location: ";
        End Select
    End If
Loop
End

You need to press a key after the second coordinates are displayed. See SLEEP statement. You could change that to a delay or even a blank mouse click to get it to recycle.

GOTO Pete

Thanks Pete. Nice and compact. I intended to get rid of the GOTO's, but you've gone a step further and reduced the steps quite a lot.
My BASIC skills have now been advanced to BASIC-102 level.