MOUSEBUTTON: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "{{DISPLAYTITLE:_MOUSEBUTTON}} The _MOUSEBUTTON function returns the button status of a specified mouse button when read after _MOUSEINPUT. {{PageSyntax}} : {{Parameter|buttonStatus%%}} = _MOUSEBUTTON({{Parameter|buttoNumber}}) {{Parameters}} * INTEGER {{Parameter|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 {{PageDescription}}...")
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 7: Line 7:




{{Parameters}}
{{PageParameters}}
* [[INTEGER]] {{Parameter|buttoNumber}} designates the mouse button to read (See [[_DEVICES]] for more than 3).
* [[INTEGER]] {{Parameter|buttoNumber}} designates the mouse button to read (See [[_DEVICES]] for more than 3).
** 1 = Left mouse button
** 1 = Left mouse button
** 2 = Right mouse button
** 2 = Right mouse button
** 3 = Center or scroll button  
** 3 = Center or scroll button




{{PageDescription}}
{{PageDescription}}
* Returns -1 if the corresponding {{Parameter|buttoNumber}} is pressed or zero when released.  
* Returns -1 if the corresponding {{Parameter|buttoNumber}} is pressed or zero when released.
* Read [[_MOUSEINPUT]] first to return the current button up or down status. (See Example 2)
* 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.  
* 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.
* 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]].
* Use [[_DEVICE$]] to find the "[MOUSE]" [[_DEVICES]] number to find the number of buttons available using [[_LASTBUTTON]].
Line 25: Line 25:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' Finding the number of mouse buttons available in QB64. This could also be used for other controller devices.
''Example 1:'' Finding the number of mouse buttons available in QB64. This could also be used for other controller devices.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|FOR...NEXT|FOR}} d = 1 {{Cl|TO}} {{Cl|_DEVICES}}  'number of input devices found
{{Cl|FOR...NEXT|FOR}} d = 1 {{Cl|TO}} {{Cl|_DEVICES}}  'number of input devices found
   dev$ = {{Cl|_DEVICE$}}(d)
   dev$ = {{Cl|_DEVICE$}}(d)
   {{Cl|IF...THEN|IF}} {{Cl|INSTR}}(dev$, "[MOUSE]") {{Cl|THEN}} buttons = {{Cl|_LASTBUTTON}}(d): {{Cl|EXIT}} {{Cl|FOR...NEXT|FOR}}
   {{Cl|IF...THEN|IF}} {{Cl|INSTR}}(dev$, "[MOUSE]") {{Cl|THEN}} buttons = {{Cl|_LASTBUTTON}}(d): {{Cl|EXIT}} {{Cl|FOR...NEXT|FOR}}
{{Cl|NEXT}}
{{Cl|NEXT}}
{{Cl|PRINT}} buttons; "mouse buttons available" '' ''
{{Cl|PRINT}} buttons; "mouse buttons available"
{{CodeEnd}}
{{CodeEnd}}




''Example 2:'' How to monitor when a button is down or wait until a mouse button is not held down.
''Example 2:'' How to monitor when a button is down or wait until a mouse button is not held down.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|PRINT}} "Hold down the left mouse button until you want to quit!"
{{Cl|PRINT}} "Hold down the left mouse button until you want to quit!"
DO
DO
Line 45: Line 45:
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|NOT}} {{Cl|_MOUSEBUTTON}}(1) '                      button is released
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|NOT}} {{Cl|_MOUSEBUTTON}}(1) '                      button is released


{{Cl|PRINT}} "DONE!" '' ''
{{Cl|PRINT}} "DONE!"
{{CodeEnd}}
{{CodeEnd}}




''Example 3:'' Checking for a click or a double-click by the user.
''Example 3:'' Checking for a click or a double-click by the user.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DO...LOOP|DO}}  'main program loop
{{Cl|DO...LOOP|DO}}  'main program loop


Line 63: Line 63:


   {{Cl|IF...THEN|IF}} Click = 1 {{Cl|THEN}}                  'if button was pressed and released
   {{Cl|IF...THEN|IF}} Click = 1 {{Cl|THEN}}                  'if button was pressed and released
     t = {{Cl|TIMER}} + .3
     t = {{Cl|TIMER (function)|TIMER}} + .3
     {{Cl|DO...LOOP|DO}} {{Cl|WHILE}} {{Cl|TIMER}} < t      'check for a second press within .3 seconds
     {{Cl|DO...LOOP|DO}} {{Cl|WHILE}} {{Cl|TIMER (function)|TIMER}} < t      'check for a second press within .3 seconds
       i = {{Cl|_MOUSEINPUT}}
       i = {{Cl|_MOUSEINPUT}}
       {{Cl|IF...THEN|IF}} {{Cl|_MOUSEBUTTON}}(1) {{Cl|THEN}} Click = 2: {{Cl|EXIT DO}}
       {{Cl|IF...THEN|IF}} {{Cl|_MOUSEBUTTON}}(1) {{Cl|THEN}} Click = 2: {{Cl|EXIT DO}}
Line 71: Line 71:
   {{Cl|END IF}}
   {{Cl|END IF}}
   Click = 0: buttondown = 0            'reset where needed
   Click = 0: buttondown = 0            'reset where needed
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) '' ''
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27)
{{CodeEnd}}
{{CodeEnd}}
: ''Explanation:'' To find the current button status read [[_MOUSEINPUT]] repeatedly. The [[TIMER]] loop looks for a second click.
: ''Explanation:'' To find the current button status read [[_MOUSEINPUT]] repeatedly. The [[TIMER (function)|TIMER]] loop looks for a second click.




''Example 4:'' Verifying that a user clicked and released a mouse button on a program button.
''Example 4:'' Verifying that a user clicked and released a mouse button on a program button.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} 12
{{Cl|SCREEN}} 12
{{Cl|LINE}} (250, 250)-(300, 300), 14, BF
{{Cl|LINE}} (250, 250)-(300, 300), 14, BF
Line 99: Line 99:
x = {{Cl|_MOUSEX}}
x = {{Cl|_MOUSEX}}
y = {{Cl|_MOUSEY}}
y = {{Cl|_MOUSEY}}
{{Cl|END SUB}} '' ''
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}
: ''Explanation:'' The mouse SUB has no internal [[_MOUSEINPUT]] loop so that no button presses, releases or moves are missed.
: ''Explanation:'' The mouse SUB has no internal [[_MOUSEINPUT]] loop so that no button presses, releases or moves are missed.
Line 105: Line 105:
{{TextStart}}{{Cb|SUB}} Catchup
{{TextStart}}{{Cb|SUB}} Catchup
{{Cb|DO...LOOP|DO}} {{Cb|WHILE}} {{Cb|_MOUSEINPUT}}: {{Cb|LOOP }}
{{Cb|DO...LOOP|DO}} {{Cb|WHILE}} {{Cb|_MOUSEINPUT}}: {{Cb|LOOP }}
{{Cb|END SUB}} '' ''
{{Cb|END SUB}}
{{TextEnd}}
{{TextEnd}}
: The above procedure can be used to catch up after [[INPUT]], [[LINE INPUT]] or [[INPUT$]] delays when mouse input may accumulate.
: The above procedure can be used to catch up after [[INPUT]], [[LINE INPUT]] or [[INPUT$]] delays when mouse input may accumulate.
Line 111: Line 111:


''Example 5:'' Combining mouse button or keyboard selections in a menu or test:
''Example 5:'' Combining mouse button or keyboard selections in a menu or test:
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DO...LOOP|DO}} 'main program loop in demo only
{{Cl|DO...LOOP|DO}} 'main program loop in demo only
   {{Cl|LOCATE}} 10, 10: {{Cl|PRINT}} "A" 'position A, B & C in same position on every question
   {{Cl|LOCATE}} 10, 10: {{Cl|PRINT}} "A" 'position A, B & C in same position on every question
Line 157: Line 157:
     K$ = "" 'reset K$
     K$ = "" 'reset K$
   {{Cl|END IF}}
   {{Cl|END IF}}
{{Cl|LOOP}} 'DEMO only loop use red X box to quit '' ''
{{Cl|LOOP}} 'DEMO only loop use red X box to quit
{{CodeEnd}} {{small|Code by Ted Weissgerber}}
{{CodeEnd}}
{{Small|Code by Ted Weissgerber}}
: ''Explanation:'' User can cancel letter selection by moving pointer off letter before releasing the left mouse button.
: ''Explanation:'' User can cancel letter selection by moving pointer off letter before releasing the left mouse button.


Line 167: Line 168:
* [[_MOUSESHOW]], [[_MOUSEHIDE]]
* [[_MOUSESHOW]], [[_MOUSEHIDE]]
* [[_DEVICES]], [[_DEVICE$]], [[_LASTBUTTON]]
* [[_DEVICES]], [[_DEVICE$]], [[_LASTBUTTON]]
* [[_BUTTON]], [[_BUTTONCHANGE]] {{text|([[DEVICES|devices]])}}
* [[_BUTTON]], [[_BUTTONCHANGE]]
* [[Controller Devices]]
* [[Controller Devices]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 16:58, 24 February 2023

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 d = 1 TO _DEVICES  'number of input devices found
  dev$ = _DEVICE$(d)
  IF INSTR(dev$, "[MOUSE]") THEN buttons = _LASTBUTTON(d): EXIT FOR
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 "Hold down the left mouse button until you want to quit!"
DO
    i = _MOUSEINPUT ' read #1
    IF _MOUSEBUTTON(1) THEN PRINT "Left button down!": EXIT DO
LOOP
DO '                                                      need to wait
    i = _MOUSEINPUT '  read #2                         until the mouse
LOOP UNTIL NOT _MOUSEBUTTON(1) '                       button is released

PRINT "DONE!"


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

DO  'main program loop

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

  IF Click = 1 THEN                   'if button was pressed and released
    t = TIMER + .3
    DO WHILE TIMER < t      'check for a second press within .3 seconds
      i = _MOUSEINPUT
      IF _MOUSEBUTTON(1) THEN Click = 2: EXIT DO
    LOOP
    IF 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.

SCREEN 12
LINE (250, 250)-(300, 300), 14, BF

DO
  Mouser mx, my, mb
  IF mb THEN
    IF mx >= 250 AND my >= 250 AND mx <= 300 AND my <= 300 THEN 'button down
      DO WHILE mb 'wait for button release
        Mouser mx, my, mb
      LOOP
      'verify mouse still in box area
      IF mx >= 250 AND my >= 250 AND mx <= 300 AND 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 Catchup
DO WHILE _MOUSEINPUT: LOOP 
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 'main program loop in demo only
  LOCATE 10, 10: PRINT "A" 'position A, B & C in same position on every question
  LOCATE 12, 10: PRINT "B"
  LOCATE 14, 10: PRINT "C" 'demo only

  DO: _LIMIT 10 'get user answer loop
    DO 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 x% = 10 AND y% = 10 AND Lclick THEN 'position clicked
      DO
        i = _MOUSEINPUT
        x% = _MOUSEX
        y% = _MOUSEY
      LOOP WHILE _MOUSEBUTTON(1)
      IF x% = 10 AND y% = 10 THEN K$ = "A" 'position released
    END IF
    IF x% = 10 AND y% = 12 AND Lclick THEN 'position clicked
      DO
        i = _MOUSEINPUT
        x% = _MOUSEX
        y% = _MOUSEY
      LOOP WHILE _MOUSEBUTTON(1)
      IF x% = 10 AND y% = 12 THEN K$ = "B" 'position released
    END IF
    IF x% = 10 AND y% = 14 AND Lclick THEN 'position clicked
      DO
        i = _MOUSEINPUT
        x% = _MOUSEX
        y% = _MOUSEY
      LOOP WHILE _MOUSEBUTTON(1)
      IF x% = 10 AND y% = 14 THEN K$ = "C" 'position released
    END IF
  LOOP UNTIL K$ = "A" OR K$ = "B" OR K$ = "C" 'GOTO next question

  IF 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:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link