Author Topic: MBS (Mouse Button Status)  (Read 3938 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
MBS (Mouse Button Status)
« on: December 17, 2020, 09:17:37 am »
Code: QB64: [Select]
  1. _TITLE "MBS (Mouse Button Status) by Steve" ' 12-17-2020
  2.  
  3.     CLS
  4.     held$ = ""
  5.     result = MBS
  6.     left = left - (result AND 8) \ 8
  7.     right = right - (result AND 16) \ 16
  8.     middle = middle - (result AND 32) \ 32
  9.     IF result AND 64 THEN held$ = "Left held"
  10.     IF result AND 128 THEN held$ = "Right held"
  11.     IF result AND 256 THEN held$ = "Middle held"
  12.     IF result AND 512 THEN scroll = scroll + 1
  13.     IF result AND 1024 THEN scroll = scroll - 1
  14.  
  15.     PRINT "MouseX: "; _MOUSEX
  16.     PRINT "MouseY: "; _MOUSEY
  17.     PRINT "Left down     : "; result AND 1
  18.     PRINT "Right down     : "; result AND 2
  19.     PRINT "Middle down     : "; result AND 4
  20.     PRINT "Left pressed  : "; left
  21.     PRINT "Right pressed : "; right
  22.     PRINT "Middle pressed: "; middle
  23.     PRINT "Mouse Wheel Scrolled: "; scroll
  24.     PRINT
  25.     PRINT "Last held event started at X/Y :"; Mouse_StartX, Mouse_StartY
  26.     PRINT "Last held event ended at X/Y   :"; Mouse_EndX, Mouse_EndY
  27.     PRINT held$
  28.     _LIMIT 60
  29.  
  30.  
  31. FUNCTION MBS% 'Mouse Button Status
  32.     STATIC StartTimer AS _FLOAT
  33.     STATIC ButtonDown AS INTEGER
  34.     STATIC ClickCount AS INTEGER
  35.     CONST ClickLimit## = 0.2 'Less than 1/4th of a second to down, up a key to count as a CLICK.
  36.     '                          Down longer counts as a HOLD event.
  37.     SHARED Mouse_StartX, Mouse_StartY, Mouse_EndX, Mouse_EndY
  38.     WHILE _MOUSEINPUT 'Remark out this block, if mouse main input/clear is going to be handled manually in main program.
  39.         SELECT CASE SGN(_MOUSEWHEEL)
  40.             CASE 1: MBS = MBS OR 512
  41.             CASE -1: MBS = MBS OR 1024
  42.         END SELECT
  43.     WEND
  44.  
  45.  
  46.     IF _MOUSEBUTTON(1) THEN MBS = MBS OR 1
  47.     IF _MOUSEBUTTON(2) THEN MBS = MBS OR 2
  48.     IF _MOUSEBUTTON(3) THEN MBS = MBS OR 4
  49.  
  50.  
  51.     IF StartTimer = 0 THEN
  52.         IF _MOUSEBUTTON(1) THEN 'If a button is pressed, start the timer to see what it does (click or hold)
  53.             ButtonDown = 1: StartTimer = TIMER(0.01)
  54.             Mouse_StartX = _MOUSEX: Mouse_StartY = _MOUSEY
  55.         ELSEIF _MOUSEBUTTON(2) THEN
  56.             ButtonDown = 2: StartTimer = TIMER(0.01)
  57.             Mouse_StartX = _MOUSEX: Mouse_StartY = _MOUSEY
  58.         ELSEIF _MOUSEBUTTON(3) THEN
  59.             ButtonDown = 3: StartTimer = TIMER(0.01)
  60.             Mouse_StartX = _MOUSEX: Mouse_StartY = _MOUSEY
  61.         END IF
  62.     ELSE
  63.         BD = ButtonDown MOD 3
  64.         IF BD = 0 THEN BD = 3
  65.         IF TIMER(0.01) - StartTimer <= ClickLimit THEN 'Button was down, then up, within time limit.  It's a click
  66.             IF _MOUSEBUTTON(BD) = 0 THEN MBS = 4 * 2 ^ ButtonDown: ButtonDown = 0: StartTimer = 0
  67.         ELSE
  68.             IF _MOUSEBUTTON(BD) = 0 THEN 'hold event has now ended
  69.                 MBS = 0: ButtonDown = 0: StartTimer = 0
  70.                 Mouse_EndX = _MOUSEX: Mouse_EndY = _MOUSEY
  71.             ELSE 'We've now started the hold event
  72.                 MBS = MBS OR 32 * 2 ^ ButtonDown
  73.             END IF
  74.         END IF
  75.     END IF
  76.  

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. 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: MBS (Mouse Button Status)
« Reply #1 on: December 17, 2020, 01:29:58 pm »
This is interesting, I be looking for one mouse and keypress handler for multiple list boxes when I get around to my own File Explorer now that we've got the listing of Folders and Files working in Windows and Linux (thanks to Johnno's report and it must work someway otherwise QB64 couldn't do it). Scrolling or mousewheel is a must, Mouse and Keypress probably key to setting up an events sort of app.
« Last Edit: December 17, 2020, 01:58:11 pm by bplus »

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: MBS (Mouse Button Status)
« Reply #2 on: December 18, 2020, 08:22:44 pm »
That makes about a dozen files in my QB64 directory that start with the word "steve" - high
quality stuff for immediate use or that I'm sure to need, someday.  I've probably got much
more of his code, but only had the sense to start naming things after the source late in my
downloading.  If I could go without a cigarette for 12 hours, I'd fly to Oz and eat his brain.
Bet it's mega tasty.
It works better if you plug it in.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: MBS (Mouse Button Status)
« Reply #3 on: December 18, 2020, 08:58:36 pm »
Chocolate caramel flavored!
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: MBS (Mouse Button Status)
« Reply #4 on: December 19, 2020, 07:19:47 pm »
All outstanding, and I definitely need to study the "last held event" start and end part, that looks highly useful for rubber band box type stuff.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: MBS (Mouse Button Status)
« Reply #5 on: December 19, 2020, 10:38:10 pm »
Now it is easy to see how the mouse works.

Code works perfect.  Good diagnostic to see if mouse is working etc.