Mouse Button Status (MBS)
#1
Code: (Select All)
_Title "MBS (Mouse Button Status) by Steve" ' 12-17-2020 // updated 4/23/2022

Do
    Cls
    held$ = ""
    result = MBS
    left = left - (result And 8) \ 8
    right = right - (result And 16) \ 16
    middle = middle - (result And 32) \ 32
    If result And 64 Then held$ = "Left held"
    If result And 128 Then held$ = "Right held"
    If result And 256 Then held$ = "Middle held"
    If result And 512 Then scroll = scroll + 1
    If result And 1024 Then scroll = scroll - 1

    Print "MouseX: "; _MouseX
    Print "MouseY: "; _MouseY
    Print "Left down     : "; result And 1
    Print "Right down     : "; result And 2
    Print "Middle down     : "; result And 4
    Print "Left pressed  : "; left
    Print "Right pressed : "; right
    Print "Middle pressed: "; middle
    Print "Mouse Wheel Scrolled: "; scroll
    Print
    Print "Last held event started at X/Y :"; Mouse_StartX, Mouse_StartY
    Print "Last held event ended at X/Y   :"; Mouse_EndX, Mouse_EndY
    Print held$
    _Limit 60
Loop


Function MBS% 'Mouse Button Status
    Static StartTimer As _Float
    Static ButtonDown As Integer
    Static ClickCount As Integer
    Const ClickLimit## = 0.2 'Less than 1/4th of a second to down, up a key to count as a CLICK.
    '                          Down longer counts as a HOLD event.
    Shared Mouse_StartX, Mouse_StartY, Mouse_EndX, Mouse_EndY
    While _MouseInput 'Remark out this block, if mouse main input/clear is going to be handled manually in main program.
        Select Case Sgn(_MouseWheel)
            Case 1: tempMBS = tempMBS Or 512
            Case -1: tempMBS = tempMBS Or 1024
        End Select
    Wend


    If _MouseButton(1) Then tempMBS = tempMBS Or 1
    If _MouseButton(2) Then tempMBS = tempMBS Or 2
    If _MouseButton(3) Then tempMBS = tempMBS Or 4


    If StartTimer = 0 Then
        If _MouseButton(1) Then 'If a button is pressed, start the timer to see what it does (click or hold)
            ButtonDown = 1: StartTimer = Timer(0.01)
            Mouse_StartX = _MouseX: Mouse_StartY = _MouseY
        ElseIf _MouseButton(2) Then
            ButtonDown = 2: StartTimer = Timer(0.01)
            Mouse_StartX = _MouseX: Mouse_StartY = _MouseY
        ElseIf _MouseButton(3) Then
            ButtonDown = 3: StartTimer = Timer(0.01)
            Mouse_StartX = _MouseX: Mouse_StartY = _MouseY
        End If
    Else
        BD = ButtonDown Mod 3
        If BD = 0 Then BD = 3
        If Timer(0.01) - StartTimer <= ClickLimit Then 'Button was down, then up, within time limit.  It's a click
            If _MouseButton(BD) = 0 Then tempMBS = 4 * 2 ^ ButtonDown: ButtonDown = 0: StartTimer = 0
        Else
            If _MouseButton(BD) = 0 Then 'hold event has now ended
                tempMBS = 0: ButtonDown = 0: StartTimer = 0
                Mouse_EndX = _MouseX: Mouse_EndY = _MouseY
            Else 'We've now started the hold event
                tempMBS = tempMBS Or 32 * 2 ^ ButtonDown
            End If
        End If
    End If
    MBS = tempMBS
End Function

I had one of these somewhere before, but I'll be danged if I can find it, so I rolled another one...

A simple routine to check the mouse buttons and to give us information on up/down, click, and hold statuses, as well as hold start/stop positions.   Results are all stored in a single binary integer, and basically break down to:

1 -- left down
2 -- right down
4 -- middle down
8 -- left clicked
16 -- right clicked
32 -- middle clicked
64 -- left held
128 -- right held
256 -- middle held
512 -- scroll down
1024 -- scroll up

Starting X/Y and Ending X/Y positions are available in the shared Mouse_ variables. 

Note, HOLD and CLICK events are independent of each other.  We don't register a free click with each hold event.  Windows tends to count first down events as clicks, so all hold events start with a click event and then transition into a hold event.  I didn't need that for my purposes, so this will either give you a hold event OR a click event; not both. 
Reply
#2
This is one of my favorite go to functions for mouse work. When they fixed the recursive bug, it stopped working, so I just removed all the 'MBS OR' clauses from the code, and while I wasn't sure if that work around ruined your intended functioning, it continued to work well in my programs. My mouse use is not too complex so that might have kept me from having problems. I'll be changing to this version ASAP. Thanks for the update.
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply
#3
Always glad when I find out someone can make use of something I shared. If you have any other routines of mine which glitched out with you during the fix to recursive function, post them somewhere and @me and I'll be happy to take a look and fix them for you. Wink
Reply
#4
I can remember having all sorts of gremlins with mouse routines.

I ended up with differing routines for windows and linux versions of my programs

reading this? I am noting the "windows does this..." exception, which sounds like what my issue might have been.
Reply




Users browsing this thread: 1 Guest(s)