With mouse clicks in PowerLanguage we can distinguish between left and right mouse clicks. But it can also be helpful to have certain code only execute when a mouse click happened (like when outputting data to a file). How to do that?

In this article:

Only processing PowerLanguage code when a mouse click happens

Handling mouse clicks first requires that PowerLanguage’s ProcessMouseEvents attribute is set to true (MultiCharts Wiki, 2012a). Any left or right mouse click on the data series’ chart area will then trigger a script calculation.

With the GetAppInfo() keyword and its aiCalcReason argument the script’s calculation reason can be retrieved (MultiCharts Wiki, 2014). When this reason is either CalcReason_MouseLClick or CalcReason_MouseRClick, we know that a left or right mouse click occurred. That info can be used in an if statement so that code is only executed when a mouse click happened.

A second approach is skipping code execution when a mouse click did not happen. This is done with the #return keyword, which works like the corresponding C++ statement (MultiCharts Wiki, 2012b). For MultiCharts, this means that #return prematurely ends the current script calculation and any statements below it aren’t executed (see Liberty & Cadenhead, 2011).

Only executing certain PowerLanguage code with a mouse click

The first approach, using an if statement to check if a mouse click happened, can be implemented as follows:

[ProcessMouseEvents = true];

if (GetAppInfo(aiCalcReason) = CalcReason_MouseLClick) then begin

    Print("Left mouse click @ ", NumToStr(MouseClickPrice, 5));


Print("MouseClickPrice = ", NumToStr(MouseClickPrice, 5));

//> Left mouse click @ 1.16928
//> MouseClickPrice = 1.16928
//> MouseClickPrice = 0.00000
//> MouseClickPrice = 0.00000
//> Left mouse click @ 1.16757
//> MouseClickPrice = 1.16757
//> MouseClickPrice = 0.00000
//> MouseClickPrice = 0.00000
//> MouseClickPrice = 0.00000
//> MouseClickPrice = 0.00000
//> Left mouse click @ 1.17014
//> MouseClickPrice = 1.17014
//> MouseClickPrice = 0.00000
//> MouseClickPrice = 0.00000

After mouse clicks are enabled with ProcessMouseEvents, an if statement evaluates whether the script’s calculation (GetAppInfo(aiCalcReason)) is triggered by a left mouse click (CalcReason_MouseLClick).

When that happens, the if statement’s code that outputs the click’s price (MouseClickPrice) is executed. This code is therefore only executed with a left mouse click. The Print() statement below the if statement, however, is executed unconditionally and so with every calculation.

Using #return to prematurely exit a calculation

The second approach is using #return. This keyword contributes to a cleaner code layout since the whole script doesn’t need to be placed in an if statement. It can be used as follows:

[ProcessMouseEvents = true];

if (GetAppInfo(aiCalcReason) <> CalcReason_MouseLClick) and 
    (GetAppInfo(aiCalcReason) <> CalcReason_MouseRClick) then

Print("Clicked on price ", NumToStr(MouseClickPrice, 5));

if (GetAppInfo(aiCalcReason) = CalcReason_MouseLClick) then 
    Print(".. with a left click")
    Print(".. with a right click");

//> Clicked on price 1.17328
//> .. with a left click
//> Clicked on price 1.18429
//> .. with a right click
//> Clicked on price 1.18343
//> .. with a right click
//> Clicked on price 1.18257
//> .. with a left click

After setting ProcessMouseEvents to true, an if statement compares the script’s calculation reason (GetAppInfo(aiCalcReason)) with a calculation caused by a left (CalcReason_MouseLClick) or right (CalcReason_MouseRClick) mouse click. By combining the and keyword with evaluating unequal to (<>), the #return keyword is executed when the calculation reason was not a left or right mouse click. Any statement that is below #return will then not be executed.

When there was a mouse click on the chart, no code is skipped because #return isn’t executed. The Print() keyword then outputs the click’s price value (MouseClickPrice) with NumToStr() to display this number with five decimals.

Next is an if-else statement that evaluates whether the calculation reason was a left click or not. When it was, this information is outputted with Print(). When it wasn’t a left click, we know it had to be a right click (since any other calculation reason would have triggered #return). And so Print() is also used to output that information.

More on left and right click examples can be found in differentiating between mouse click buttons.


Before mouse clicks can be processed, ProcessMouseEvents needs to be set to true. Then each click will trigger a script calculation of which the reason can be retrieved with GetAppInfo(aiCalcReason). With the help of #return programming code can be ‘skipped’ when there is a left (CalcReason_MouseLClick) or right (CalcReason_MouseRClick) mouse click.


Liberty, J. & Cadenhead, R. (2011). Sams Teach Yourself C++ in 24 Hours. Indianapolis, IN (USA): Sams/Pearson Education.

MultiCharts Wiki (2012a, February 24). ProcessMouseEvents. Retrieved on January 11, 2015, from http://www.multicharts.com/trading-software/index.php/ProcessMouseEvents

MultiCharts Wiki (2012b, February 24). Return. Retrieved on January 16, 2015, from http://www.multicharts.com/trading-software/index.php/Return

MultiCharts Wiki (2014, September 8). GetAppInfo. Retrieved on January 16, 2015, from http://www.multicharts.com/trading-software/index.php/GetAppInfo

Visit programming tutorials for more helpful coding articles.