GetCursorMouseButtons
- get current state of mouse buttons

Indicators
(AFL 2.80)


SYNTAX GetCursorMouseButtons()
RETURNS NUMBER
FUNCTION This function returns mouse button state at the time when chart formula is executed.
The state is COMBINATION (bitwise OR) of the following flags:
  • 0 - if no mouse button is down
  • 1 - if left mouse button is down
  • 2 - if right mouse button is down
  • 4 - if middle mouse button is down
  • 8 - if window just received mouse click (version 5.06 and higher only)

So for example the following combinations are possible:

  • 3 - left + right down
  • 3 - left + right down
  • 5 - left + middle down
  • 6 - right + middle down
  • 7 - left + right + middle down
  • 9 - left mouse button just clicked (v.5.06)
  • 10 - right mouse button just clicked (v.5.06)
  • 11 - left + right mouse button just clicked (v.5.06)
For example if you click the left mouse button and hold it down, the underlying chart will be refreshed and GetCursorMouseButtons will return 9 (8+1) during very first execution since click and 1 in all subsequent executions as long as left mouse button is kept pressed down. You need to use bitwise AND (&) to extract these flags as shown in the example.
EXAMPLE if( GetCursorMouseButtons() & 1 )
{
printf("left mouse button is pressed down" );
}

Example 2. Low-level graphic + Interactive GUI control sample

/////////////////////////////////////////////////
// Low-level graphic + Interactive GUI control sample
// This example shows:
//  1. how to draw "buttons"
//  2. how to handle mouse clicks
//  3. how to implement event call-backs
///////////////////////////////////////////////////

Version( 5.04 ); // requires 5.04 or higher

////////////////////////////////////////////////////
// Part 1: DRAWING TABLE OF BUTTONS
//////////////////////////////////////////////////
GfxSetOverlayMode( 2 );
// formatted text output sample via low-level gfx functions

CellHeight =
20;
CellWidth =
100;
GfxSelectFont( "Tahoma", CellHeight/2 );

GfxSetBkMode( 1 );

function PrintInCell( string, row, Col )
{
GfxDrawText( string, Col * CellWidth, row * CellHeight, (Col + 1 ) * CellWidth, (row + 1 ) * CellHeight, 0 );
}

GfxSelectPen( colorBlue );
for( i = 0; i < 10 && i < BarCount; i++ )
{
for( k = 0; k < 5; k++ )
{
   PrintInCell(
"Button " + i + "," + k, i, k );
}
GfxMoveTo( 0, i * CellHeight );
GfxLineTo( 5 * CellWidth, i * CellHeight );
}
GfxMoveTo( 0, i * CellHeight );
GfxLineTo( 5 * CellWidth, i * CellHeight );

for( Col = 1; Col < 6; Col++ )
{
GfxMoveTo( Col * CellWidth, 0);
GfxLineTo( Col * CellWidth, 10 * CellHeight );
}


/////////////////////////////////////////////////////////
// Part 2: MOUSE BUTTON CALL BACKS
//////////////////////////////////////////////////////////
Title="";

function DrawButton( px, py, Clr1, Clr2, text )
{
     Col =
floor( px / CellWidth );
     Row =
floor( py / CellHeight );

   
   
GfxGradientRect( Col * CellWidth, row * CellHeight, (Col + 1 ) * CellWidth, (row + 1 ) * CellHeight,
                   Clr1, Clr2 );

     PrintInCell( text +
" " + row + "," + Col, row, Col );

}

function OnLMouseButton(x, y, px, py)
{
   
_TRACE("LButton x = " + DateTimeToStr( x ) + " y = " + y );

   DrawButton( px, py,
ColorHSB( 50, 255, 255 ), ColorHSB( 90, 255, 255 ), "just clicked" );
}

function OnRMouseButton(x, y, px, py)
{
   
_TRACE("RButton x = " + DateTimeToStr( x ) + " y = " + y );
}

function OnMMouseButton(x, y, px, py)
{
   
_TRACE("MButton x = " + DateTimeToStr( x ) + " y = " + y );
}

function OnHoverMouse(x, y, px, py)
{
   
_TRACE("LButton x = " + DateTimeToStr( x ) + " y = " + y );

     DrawButton( px, py,
ColorRGB( 230, 230, 230 ), ColorRGB( 255, 255, 255 ), "mouse over" );
}

function OnLButtonIsDown(x, y, px, py)
{
   
_TRACE("LButton x = " + DateTimeToStr( x ) + " y = " + y );

     DrawButton( px, py,
ColorHSB( 190, 255, 255 ), ColorHSB( 210, 255, 255 ), "down" );
}

/////////////////////////////////////////////////////////
// Part 3: GENERAL PURPOSE EVENT HANDLER (reusable! - may be put into "include" file)
////////////////////////////////////////////////////////

function EventHandler()
{
local b, x, y, px, py;
b =
GetCursorMouseButtons();

// retrieve co-ordinates in date/value units
x =
GetCursorXPosition(0);
y =
GetCursorYPosition(0);

// retrieve co-ordinates in pixel units
px =
GetCursorXPosition(1);
py =
GetCursorYPosition(1);

if( b & 8 ) // flag = 8 is set when window just received mouse click
{
  
// not-null means clicked in THIS (current) window
  
if( b & 1 ) OnLMouseButton( x, y, px, py );
  
if( b & 2 ) OnRMouseButton( x, y, px, py );
  
if( b & 4 ) OnMMouseButton( x, y, px, py );
}
else
{
  
if( b == 0 ) OnHoverMouse( x, y, px, py ); // no button pressed
  
if( b == 1 ) OnLButtonIsDown( x, y, px, py ); // button pressed
}
}

EventHandler();
RequestTimedRefresh( 1 );

SEE ALSO GetCursorXPosition() function , GetCursorYPosition() function

References:

The GetCursorMouseButtons function is used in the following formulas in AFL on-line library:

More information:

Updated on-line reference