Code archives/User Input/Extended Mouse Click Events

This code has been declared by its author to be Public Domain code.

Download source code

Extended Mouse Click Events by Spencer2010
For Blitz3D
**Work in Progress**
The cMouseEvents.bb code handles all mouse clicking events for a three-button mouse. Include the cMouseEvents.bb file in your project and you'll be able to check for mouse button events: Click, DoubleClick, Down, and Up. The cMouseEvents functions are intended to replace the standard Blitz3D functions MouseHit and MouseDown and provide additional double-click and button-up events. At the TOP of your game loop you'll need to call cMouseEvents_Update() which will capture the states and allow the other cMouseEvents functions to work. For example you might place this function call after you call Cls(). If you include this code, it is recommended that you replace calls to MouseHit and MouseDown with cMouseEvents_ButtonClick and cMouseEvents_ButtonDown. Like MouseHit and MouseDown, the cMouseEvents functions require a button parameter. (1=Left button, 2=Right button, 3=Middle button) There are nine cMouseEvents functions:

1) cMouseEvents_ButtonClick(<ButtonNumber>)
2) cMouseEvents_ButtonDoubleClick(<ButtonNumber>)
3) cMouseEvents_ButtonDown(<ButtonNumber>)
4) cMouseEvents_ButtonUp(<ButtonNumber>)
5) cMouseEvents_SetDoubleClickTimeWindow(<Milliseconds>)
6) cMouseEvents_GetDoubleClickTimeWIndow()
7) cMouseEvents_Initialize()
8) cMouseEvents_Dispose()
9) cMouseEvents_Update()

The first four functions are similar to Blitz3D’s MouseHit and MouseDown: Pass in an button number and the function will TRUE if the condition is met.
Functions five and six get and set the window of time within which the user has to double click. By default this time is set to 200 milliseconds. You may want to adjust this depending on your project
Functions seven and eight setup and dispose of the cMouseEvent object of type cMouseEvents. You probably won’t have to use these functions. cMouseEvents_Initialize() is called from within cMouseEvents.bb to initialize the cMouseEvent object. However, if you call cMouseEvent_Dispose() or delete the cMouseEvent object, you’ll need a way to get it back, hence cMouseEvents_Initialize().
Function nine, cMouseEvents_Update(), updates the cMouseEvent object representing the various states of the three buttons. Call this function at the top of your game loop, or any loop inside which you are checking for a mouse button event. As seen in the example program below, the cMouseEvents_Update() function is called at the top of the loop after Cls is called.

Below is a simple example Program that includes the cMouseEvents.bb code and checks for a double left click.

Graphics3D 800, 600, 0, 2
Include "cMouseEvents.bb"

While Not KeyHit(1)
Cls
cMouseEvents_Update()

Text 10, 10, "Double Left Click to Exit, or Press Escape"

If cMouseEvents_ButtonDoubleClick(1) Then
Exit
EndIf

Flip
Wend
;-------------------------------------------------------------------------------------------------
; Mouse Events 
;To use, save this code in your project's folder as "cMouseEvents.bb" 
;Then type Include "cMouseEvents.bb" at the top of your code
;-------------------------------------------------------------------------------------------------
cMouseEvents_Initalize()

Global cMouseEvent.cMouseEvents
Const  cMouseEvents_DefaultDoubleClickTimeWindow = 200
Global cMouseEvents_DoubleClickTimeWindowInMillisecs = cMouseEvents_DefaultDoubleClickTimeWindow

Type cMouseEvents
    Field LeftDown
    Field LeftUp
    Field LeftClick
    Field LeftDoubleClick
    Field LastTimeUserLeftClicked
    Field RightDown
    Field RightUp
    Field RightClick
    Field RightDoubleClick
    Field LastTimeUserRightClicked
    Field MiddleDown
    Field MiddleUp
    Field MiddleClick
    Field MiddleDoubleClick
    Field LastTimeUserMiddleClicked
End Type

Function cMouseEvents_Initalize()
	If cMouseEvent = Null Then
		cMouseEvent.cMouseEvents = New cMouseEvents
		cMouseEvent\LeftDown = 0
		cMouseEvent\LeftUp = 0
		cMouseEvent\LeftClick = 0
		cMouseEvent\LeftDoubleClick = 0
		cMouseEvent\LastTimeUserLeftClicked = 0
		cMouseEvent\RightDown = 0
		cMouseEvent\RightUp = 0
		cMouseEvent\RightClick = 0
		cMouseEvent\RightDoubleClick = 0
		cMouseEvent\LastTimeUserRightClicked = 0
		cMouseEvent\MiddleDown = 0
		cMouseEvent\MiddleUp = 0
		cMouseEvent\MiddleClick = 0
		cMouseEvent\MiddleDoubleClick = 0
		cMouseEvent\LastTimeUserMiddleClicked = 0
	EndIf
End Function

Function cMouseEvents_Dispose()
    Delete cMouseEvent
End Function

Function cMouseEvents_SetDoubleClickTimeWindow(MilliSeconds)
	If MilliSeconds > 0 Then
		cMouseEvents_DoubleClickTimeWindowInMillisecs = MilliSeconds
	Else
		cMouseEvents_DoubleClickTimeWindowInMillisecs = cMouseEvents_DefaultDoubleClickTimeWindow
	EndIf
End Function

Function cMouseEvents_GetDoubleClickTimeWindow()
	Return cMouseEvents_DoubleClickTimeWindowInMillisecs
End Function

Function cMouseEvents_Update()
    Local LeftClick             = MouseHit(1)
    Local RightClick            = MouseHit(2)
    Local MiddleClick           = MouseHit(3)
    Local LeftDown              = MouseDown(1)
    Local RightDown             = MouseDown(2)
    Local MiddleDown            = MouseDown(3)
    Local LeftUp                = ( (cMouseEvent\LeftDown=1  ) And (LeftDown=0  ) )
    Local RightUp               = ( (cMouseEvent\RightDown=1 ) And (RightDown=0 ) )
    Local MiddleUp              = ( (cMouseEvent\MiddleDown=1) And (MiddleDown=0) )
    Local TimeUserLeftClicked   = LeftClick   * MilliSecs()
    Local TimeUserRightClicked  = RightClick  * MilliSecs()
    Local TimeUserMiddleClicked = MiddleClick * MilliSecs()
    Local LeftClickDuration     = TimeUserLeftClicked   - cMouseEvent\LastTimeUserLeftClicked
    Local RightClickDuration    = TimeUserRightClicked  - cMouseEvent\LastTimeUserRightClicked
    Local MiddleClickDuration   = TimeUserMiddleClicked - cMouseEvent\LastTimeUserMiddleClicked
    Local LeftDoubleClick       = (LeftClickDuration   > 0) And (LeftClickDuration   < cMouseEvents_DoubleClickTimeWindowInMillisecs)
    Local RightDoubleClick      = (RightClickDuration  > 0) And (RightClickDuration  < cMouseEvents_DoubleClickTimeWindowInMillisecs)
    Local MiddleDoubleClick     = (MiddleClickDuration > 0) And (MiddleClickDuration < cMouseEvents_DoubleClickTimeWindowInMillisecs)
	
    cMouseEvent\LeftClick         = LeftClick
    cMouseEvent\RightClick        = RightClick
    cMouseEvent\MiddleClick       = MiddleClick
    cMouseEvent\LeftDown          = LeftDown
    cMouseEvent\RightDown         = RightDown
    cMouseEvent\MiddleDown        = MiddleDown
    cMouseEvent\LeftUp            = LeftUp
    cMouseEvent\RightUp           = RightUp
    cMouseEvent\MiddleUp          = MiddleUp
    cMouseEvent\LeftDoubleClick   = LeftDoubleClick
    cMouseEvent\RightDoubleClick  = RightDoubleClick
    cMouseEvent\MiddleDoubleClick = MiddleDoubleClick
	
    If LeftDoubleClick Then
        cMouseEvent\LastTimeUserLeftClicked   = 0
    ElseIf TimeUserLeftClicked > 0 Then
        cMouseEvent\LastTimeUserLeftClicked = TimeUserLeftClicked
    EndIf
	
    If RightDoubleClick Then
        cMouseEvent\LastTimeUserRightClicked   = 0
    ElseIf TimeUserRightClicked > 0 Then
        cMouseEvent\LastTimeUserRightClicked = TimeUserRightClicked
    EndIf
	
    If MiddleDoubleClick Then
        cMouseEvent\LastTimeUserMiddleClicked  = 0
    ElseIf TimeUserMiddleClicked > 0 Then
        cMouseEvent\LastTimeUserMiddleClicked = TimeUserMiddleClicked
    EndIf
	
End Function

Function cMouseEvents_ButtonClick(ButtonNumber)
	If cMouseEvent <> Null Then
        Select ButtonNumber
            Case 1 : Return cMouseEvent\LeftClick
            Case 2 : Return cMouseEvent\RightClick
            Case 3 : Return cMouseEvent\MiddleClick
            Default: Return 0
        End Select
    Else
        Return -1
    EndIf
End Function

Function cMouseEvents_ButtonDown(ButtonNumber)
	If cMouseEvent <> Null Then
        Select ButtonNumber
            Case 1 : Return cMouseEvent\LeftDown
            Case 2 : Return cMouseEvent\RightDown
            Case 3 : Return cMouseEvent\MiddleDown
            Default: Return 0
        End Select
    Else
        Return -1
    EndIf
End Function

Function cMouseEvents_ButtonUp(ButtonNumber)
    If cMouseEvent <> Null Then
        Select ButtonNumber
            Case 1 : Return cMouseEvent\LeftUp
            Case 2 : Return cMouseEvent\RightUp
            Case 3 : Return cMouseEvent\MiddleUp
            Default: Return 0
        End Select
    Else
        Return -1
    EndIf
End Function

Function cMouseEvents_ButtonDoubleClick(ButtonNumber)
    If cMouseEvent <> Null Then
        Select ButtonNumber
            Case 1 : Return cMouseEvent\LeftDoubleClick
            Case 2 : Return cMouseEvent\RightDoubleClick
            Case 3 : Return cMouseEvent\MiddleDoubleClick
            Default: Return 0
        End Select
    Else
        Return -1
    EndIf
End Function
;-------------------------------------------------------------------------------------------------

Comments

N2010
There are eight cMouseEvents functions
You forgot the ninth and most important one: Update.


Spencer2010
lol good point!


Spencer2010
Thanks Nilium, Code has been updated.


Serpent2010
Nice function set. All the variables are managed nicely inside the type.


Code Archives Forum