Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Repeating mouse check
#1
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
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#2
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.
Reply
#3
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.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#4
@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
Reply
#5
(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.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply




Users browsing this thread: 4 Guest(s)