TGC Codebase Backup



Detect Mouse Double by skyser

15th Sep 2003 11:59
Summary

See whether the user has dbl-clicked btn 1...



Description

comments are in the source.
basically define the globals and constants and the isDoubleClick() function
set sensitivity to your taste. call function from within your main loop.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `***************************************************
` mouse double-click detection code
`***************************************************
` courtesy of skyser from Electric Funstuff
` http://www.electricfunstuff.com
` if you use this code in a real product please give EFS
` a shout out somewhere. thx!
`***************************************************


`for our purposes here we use this definition...
`  a dbl-click = a press/release/press of mouse button 1
`  within the specified interval

` you need to define these vars
GLOBAL clickStartTime = -1
GLOBAL inClickCycle = 0
GLOBAL isBtnDown = 0

`sensitivity in milliseconds - adjust this to taste
`lower for less sensitive, higher for more
#CONSTANT DBL_CLICK_INTERVAL 350


GLOBAL dblClkCount = 0   ` for testing purposes




do
   cls
   if isDoubleClick() = 1 then inc dblClickCount
   if escapekey()=1 then exit

   text 10,10,"dbl click count: " + str$(dblClickCount)

loop
end


` call this from your own code in your main loop
` func returns 1 if a dbl click happened, 0 otherwise
function isDoubleClick()
    curClickTime = TIMER()
    clickCode = mouseclick()

    `if no click and not in cycle then exit
     if (clickCode <> 1) AND (inClickCycle=0) then exitfunction 0

     select clickCode
        case 0:   `no buttons down
            `if in cycle and btn was down, then release
            if (inClickCycle = 1) AND (isBtnDown = 1)
                isBtnDown = 0
            endif : endcase     `ignore no btns if not in cycle
        case 1:   `left button down
            if inClickCycle=0 `not in cycle, so start new cycle
                clickStartTime = curClickTime
                inClickCycle = 1
                isBtnDown = 1
                exitfunction 0
            else
                `we're in a click cycle and the button is down
                if isBtnDown = 1 then exitfunction 0  `ignore if already down

                `so we know now btn is not down, so this is a new click in the cycle
                `therefore it's a potential dbl-click

                elapsedTime = curClickTime - clickStartTime

                `reset the cycle
                inClickCycle = 0

                `if this all happened within the right time span then
                `call it a dbl click
                if elapsedTime <= DBL_CLICK_INTERVAL
                    exitfunction 1
                endif
            endif : endcase
     endselect
endfunction 0