Mouse states by Fluffy Paul5th Feb 2010 16:52
|
---|
Summary A short subroutine which will let you detect separate mousedown, mousepress and mouse up events, as well as double clicks. Description This code features a subroutine which detects changes in the mouseclick() value, buffers them and sets up the following variables: Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com Rem Mouse States Component Rem Variable to store the max time between clicks. Rem If you click the same button twice within this time, you'll trigger a double click. MOUSE_doubleClickSpeed = 750 Rem Call this sub every cycle _get_mouse_state: rem differentiate mouse down and mouse up rem firstly, is the mouse being clicked? if mouseclick() if MOUSE_mousepress=mouseclick() MOUSE_mousedown=0 else MOUSE_mousedown = mouseclick() endif MOUSE_mousepress=mouseclick() else rem catch mouseup rem also resets mouseup to zero if there's no mouse action for a frame. MOUSE_mouseup=MOUSE_mousepress rem save the last mouseup to check for double click rem N.B. you could change this to use mousedown or mouse press instead, if it doesn't feel right to you if MOUSE_mouseup>0 rem store the time this mouseup took place MOUSE_lastClickTime=MOUSE_thisClickTime MOUSE_thisClickTime=timer() MOUSE_clickSpeed = MOUSE_thisClickTime - MOUSE_lastClickTime rem check to see if there was another one recently rem check interval between clicks and that they were the same button rem double clicking is signified by the button number being negative rem e.g. mouseup=1 means button 1 was clicked. mouseup=-1 means button 1 was double clicked. if MOUSE_clickSpeed<=MOUSE_doubleClickSpeed and MOUSE_lastMouseUp=MOUSE_mouseup MOUSE_mouseup=0-MOUSE_mouseup endif MOUSE_lastMouseUp=MOUSE_mouseup endif MOUSE_mousedown=0 MOUSE_mousepress=0 endif return |