_MOUSEBUTTON - mkilgore/QB64pe GitHub Wiki

The _MOUSEBUTTON function returns the button status of a specified mouse button when read after _MOUSEINPUT.

Syntax

buttonStatus%% = _MOUSEBUTTON(buttoNumber)

Parameters

  • INTEGER buttoNumber designates the mouse button to read (See _DEVICES for more than 3).
    • 1 = Left mouse button
    • 2 = Right mouse button
    • 3 = Center or scroll button

Description

  • Returns -1 if the corresponding buttoNumber is pressed or zero when released.
  • Read _MOUSEINPUT first to return the current button up or down status. (See Example 2)
  • Button clicks and mouse movements will be remembered and should be cleared after an INPUT statement or other interruption.
  • To clear unread mouse input, use a _MOUSEINPUT loop that loops until it returns 0.
  • Use _DEVICE$ to find the "[MOUSE]" _DEVICES number to find the number of buttons available using _LASTBUTTON.
  • Note: The center mouse button can also be read as _BUTTON(2) on _DEVICEINPUT(2) when a mouse is present.

Examples

Example 1: Finding the number of mouse buttons available in QB64. This could also be used for other controller devices.

 '' ''
FOR...NEXT d = 1 TO _DEVICES  'number of input devices found
  dev$ = _DEVICE$(d)
  IF...THEN INSTR(dev$, "[MOUSE]") THEN buttons = _LASTBUTTON(d): EXIT FOR...NEXT
NEXT
PRINT buttons; "mouse buttons available" '' ''

Example 2: How to monitor when a button is down or wait until a mouse button is not held down.

PRINT "DONE!" '' ''

Example 3: Checking for a click or a double-click by the user.

  DO...LOOP WHILE _MOUSEINPUT                'check mouse status
    buttondown = _MOUSEBUTTON(1)
  LOOP
  DO...LOOP WHILE buttondown                 'check for button release
    i = _MOUSEINPUT
    buttondown = _MOUSEBUTTON(1)
    Click = 1
  LOOP

  IF...THEN Click = 1 THEN                   'if button was pressed and released
    t = TIMER + .3
    DO...LOOP WHILE TIMER < t      'check for a second press within .3 seconds
      i = _MOUSEINPUT
      IF...THEN _MOUSEBUTTON(1) THEN Click = 2: EXIT DO
    LOOP
    IF...THEN Click = 2 THEN PRINT "Double click" ELSE PRINT "Click"
  END IF
  Click = 0: buttondown = 0            'reset where needed
LOOP UNTIL INKEY$ = CHR$(27) '' ''
Explanation: To find the current button status read _MOUSEINPUT repeatedly. The TIMER loop looks for a second click.
Example 4: Verifying that a user clicked and released a mouse button on a program button.
DO...LOOP
  Mouser mx, my, mb
  IF...THEN mb THEN
    IF...THEN mx >= 250 AND (boolean) my >= 250 AND (boolean) mx <= 300 AND (boolean) my <= 300 THEN 'button down
      DO...LOOP WHILE mb 'wait for button release
        Mouser mx, my, mb
      LOOP
      'verify mouse still in box area
      IF...THEN mx >= 250 AND (boolean) my >= 250 AND (boolean) mx <= 300 AND (boolean) my <= 300 THEN PRINT "Click verified on yellow box!"
    END IF
  END IF
LOOP

SUB Mouser (x, y, b)
mi = _MOUSEINPUT
b = _MOUSEBUTTON(1)
x = _MOUSEX
y = _MOUSEY
END SUB '' ''
Explanation: The mouse SUB has no internal _MOUSEINPUT loop so that no button presses, releases or moves are missed.
If the above read procedure goes to another one, it may be advisable to skip over unread input in a _MOUSEINPUT only loop.
[[SUB|SUB]] Catchup
[[DO...LOOP|DO]] [[WHILE|WHILE]] [[_MOUSEINPUT|_MOUSEINPUT]]: [[LOOP |LOOP ]]
[[END SUB|END SUB]] '' ''
The above procedure can be used to catch up after INPUT, LINE INPUT or INPUT$ delays when mouse input may accumulate.
Example 5: Combining mouse button or keyboard selections in a menu or test:
  DO...LOOP: _LIMIT 10 'get user answer loop
    DO...LOOP WHILE _MOUSEINPUT: LOOP 'read mouse
    K$ = UCASE$(INKEY$) 'read keypresses also
    x% = _MOUSEX
    y% = _MOUSEY
    Lclick = _MOUSEBUTTON(1)

    LOCATE 20, 10: PRINT x%, y%, Lclick 'only used to find mouse coordinates
    IF...THEN x% = 10 AND (boolean) y% = 10 AND (boolean) Lclick THEN 'position clicked
      DO
        i = _MOUSEINPUT
        x% = _MOUSEX
        y% = _MOUSEY
      LOOP WHILE _MOUSEBUTTON(1)
      IF...THEN x% = 10 AND (boolean) y% = 10 THEN K$ = "A" 'position released
    END IF
    IF...THEN x% = 10 AND (boolean) y% = 12 AND (boolean) Lclick THEN 'position clicked
      DO
        i = _MOUSEINPUT
        x% = _MOUSEX
        y% = _MOUSEY
      LOOP WHILE _MOUSEBUTTON(1)
      IF...THEN x% = 10 AND (boolean) y% = 12 THEN K$ = "B" 'position released
    END IF
    IF...THEN x% = 10 AND (boolean) y% = 14 AND (boolean) Lclick THEN 'position clicked
      DO
        i = _MOUSEINPUT
        x% = _MOUSEX
        y% = _MOUSEY
      LOOP WHILE _MOUSEBUTTON(1)
      IF...THEN x% = 10 AND (boolean) y% = 14 THEN K$ = "C" 'position released
    END IF
  LOOP UNTIL K$ = "A" OR (boolean) K$ = "B" OR (boolean) K$ = "C" 'GOTO next question

  IF...THEN LEN(K$) THEN 'DEMO ONLY
    LOCATE 22, 35: PRINT "  Answer = "; K$ 'display user answer at location
    _DELAY 2 'allow time for user to view answer
    LOCATE 22, 35: PRINT "SELECT AGAIN"
    K$ = "" 'reset K$
  END IF
LOOP 'DEMO only loop use red X box to quit '' ''
Code by Ted Weissgerber
Explanation: User can cancel letter selection by moving pointer off letter before releasing the left mouse button.

See also


Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
⚠️ **GitHub.com Fallback** ⚠️