• Print

Author Topic: A quick and easy mouse button checker  (Read 553 times)


  • Hero Member
  • *****
  • Posts: 2414
    • Email
A quick and easy mouse button checker
« on: April 16, 2012, 08:17:56 PM »
I've found that typing the same Do while _mouseinput .... loop commands gets quite tedious after a bit.  My solution:  Make a function, include it in an easy to load library, and use it to forever make checking the mouse easier.

Code: [Select]

    mbs = mouseclick 'mbs is short for mousebuttonstatus.  This statement checks to see what is going on with the mouse buttons.
    LOCATE 1, 1
        CASE 0: PRINT "left mouse clicked                       "
        CASE 1: PRINT "right mouse clicked                      "
        CASE 2: PRINT "left button double clicked               "
        CASE 3: PRINT "right button double clicked              "
        CASE 4: PRINT "pressed the middle button                "
        CASE 5: PRINT "middle button double pressed             "
        CASE 6: PRINT "scrolled mouse wheel ";: scroll% = scroll% + 1: PRINT scroll%; "times   "
        CASE 7: PRINT "scrolled mouse wheel ";: scroll% = scroll% - 1: PRINT scroll%; "times    "

    LOCATE 10, 1: PRINT "Press <ANY KEY> to quit"
    a$ = INKEY$
LOOP UNTIL a$ <> ""


'*     Copy from here down and
'* save and place in a library file
'* for a quick mouse button function
'* if you thiink this is useful

FUNCTION mouseclick%
mouseclick% = -1

DO WHILE _MOUSEINPUT 'check mouse status
    lb% = _MOUSEBUTTON(1): rb% = _MOUSEBUTTON(2) 'see if left or right button are pushed down
    mb% = _MOUSEBUTTON(3) 'if mouse has 3 buttons, this will check the middle one
    scroll% = scroll% + _MOUSEWHEEL ' if scrollwheel changes, watch the change here

DO WHILE lb% 'check for leftbutton release
    i% = _MOUSEINPUT
    lb% = _MOUSEBUTTON(1)
    mouseclick% = 0

DO WHILE rb% 'check for right button release
    i% = _MOUSEINPUT
    rb% = _MOUSEBUTTON(2)
    mouseclick% = 1

DO WHILE mb% 'check for middle button release
    i% = _MOUSEINPUT
    mb% = _MOUSEBUTTON(3)
    mouseclick% = 4

IF mouseclick% > -1 THEN 'if button was pressed and released
    t! = TIMER + .25
    DO WHILE TIMER < t! 'check for a second press within .25 seconds
        i% = _MOUSEINPUT
        IF _MOUSEBUTTON(1) AND mouseclick% = 0 THEN mouseclick% = 2: EXIT DO
        IF _MOUSEBUTTON(2) AND mouseclick% = 1 THEN mouseclick% = 3: EXIT DO
        IF _MOUSEBUTTON(3) AND mouseclick% = 4 THEN mouseclick% = 5: EXIT DO

IF scroll% < 0 THEN mouseclick% = 6
IF scroll% > 0 THEN mouseclick% = 7


Simply do a   MBS% = mouseclick%, and the return result is whatever is going on with the mousebuttons.   I've only got this set up for 3 mouse buttons, and the scroll wheel (as that's all I have myself on my mouse), but it'd be easy enough to expand the function to give a result for more mouse buttons, if your program is something that needs a more clickable mouse.

Personally, I think all QB64 libraries need a quick funtion like this for easy mouse button checking.  ;)
http://bit.ly/TextImage -- Library of QB64 code to manipulate text and images, as a BM library.
http://bit.ly/Color32 -- A set of color CONST for use in 32 bit mode, as a BI library.

http://bit.ly/DataToDrive - A set of routines to quickly and easily get data to and from the disk.  BI and BM files


  • Hero Member
  • *****
  • Posts: 16440
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: A quick and easy mouse button checker
« Reply #1 on: April 16, 2012, 09:14:44 PM »
Technically that is not a Library as it does not use DECLARE LIBRARY or a DLL.

You could use it as an $INCLUDE text library file however.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

  • Print