The MultiCharts mouse click keywords, like those for a click’s price or a click’s date and time, return accurate values only after a left or right click on the chart. But how can we figure out which mouse button was used?

In this article:

Mouse clicks and script calculations in MultiCharts

Mouse clicks can be programmatically processed when the ProcessMouseEvents attribute is enabled in the indicator or trading strategy (MultiCharts Wiki, 2012).

Then each click on the chart’s data series triggers a script calculation for the current bar. This makes sense because keywords like MouseClickPrice or MouseClickDateTime need to be accessible in the context of the current click — if the script would wait for the next price update before processing a click, another click might have already happened.

However, there is no mouse click keyword for determining whether a left or right mouse click happened. Luckily, we can use a script’s calculation reason for that.

Retrieving a script’s calculation reason in PowerLanguage

Why a script is calculated can be retrieved with the GetAppInfo() keyword together with the aiCalcReason argument (MultiCharts Wiki, 2014). Of the different values returned by GetAppInfo(aiCalcReason), two are related to mouse clicks: CalcReason_MouseLClick (when the calculation happens due to a left mouse click) and CalcReason_MouseRClick, which signals that a right mouse click initiated the calculation.

Tip: By using GetAppInfo(aiCalcReason) to see if a mouse click occurred, we can prevent accessing mouse click keywords when a click didn’t happen. For example, MouseClickPrice returns 0 without a mouse click – so we don’t want to use that keyword in calculations when no click happened.

By the way, even though right mouse clicks can be processed in PowerLanguage, they still trigger the pop-up window:

Mouse clicks; example of the right button in MultiCharts

This behaviour cannot be changed (Henry MultiCharts, 2012), making right mouse clicks less useful.

Differentiating between a left and right mouse click in MultiCharts

One way to use a script’s calculation reason is with a switch statement:


[ProcessMouseEvents = true];

switch (GetAppInfo(aiCalcReason)) begin

    case CalcReason_MouseLClick:
        Print("Left click @ ", NumToStr(MouseClickPrice, 5));

    case CalcReason_MouseRClick:
        Print("Right click @ ", NumToStr(MouseClickPrice, 5));

    default:
        Print("Received no click. MouseClickPrice = ",
            NumToStr(MouseClickPrice, 5));

end;

//> Received no click. MouseClickPrice = 0.00000
//> Left click @ 1.20103
//> Left click @ 1.20784
//> Right click @ 1.27275
//> Received no click. MouseClickPrice = 0.00000
//> Right click @ 1.26915
//> Received no click. MouseClickPrice = 0.00000
//> Received no click. MouseClickPrice = 0.00000

After enabling mouse click processing, the switch statement evaluates GetAppInfo(aiCalcReason) and then directs code execution to one of three code blocks depending on the value of this keyword.

The first switch statement case is for the left mouse click (CalcReason_MouseLClick). Its code contains a Print() statement that outputs MouseClickPrice, converted to a five decimal string with NumToStr(). The second case processes right mouse clicks (CalcReason_MouseRClick) and also outputs the click’s price level.

The last case is the default case, which is triggered for any other calculation reason than a left or right mouse click (like a new price update). Its Print() statement still outputs MouseClickPrice, but now this keyword returns zero since a click didn’t happen.

Combining mouse buttons with keyboard keys in PowerLanguage

For more control over mouse clicks, the mouse button can be combined with keyboard keys. For example:


[ProcessMouseEvents = true];

Variables:
    leftClick(False),
    rightClick(False);

leftClick  = (GetAppInfo(aiCalcReason) = CalcReason_MouseLClick);
rightClick = (GetAppInfo(aiCalcReason) = CalcReason_MouseRClick);

if (leftClick) then begin

    if (MouseClickCtrlPressed and MouseClickShiftPressed) then
        Print("Left click with Ctrl + Shift")
    else if (MouseClickCtrlPressed) then 
        Print("Left click with Ctrl")
    else if (MouseClickShiftPressed) then
        Print("Left click with Shift");

end

else if (rightClick) then 
    Print("Right click");

//> Left click with Ctrl
//> Left click with Shift
//> Left click with Ctrl + Shift
//> Right click
//> Right click
//> Left click with Ctrl

Two true/false variables are created here (leftClick and rightClick). Each is subsequently given a value based on the mouse button pressed.

Since a left click happened when GetAppInfo(aiCalcReason) equals CalcReason_MouseLClick, this expression is assigned to the leftClick variable. That way, leftClick returns true for a calculation triggered by a left mouse click and false for all other calculation reasons. The value of the rightClick variable is determined similarly, although here the calculation reason needs to equal CalcReason_MouseRClick.

Then an if-else statement begins with evaluating the leftClick variable. When this variable returns true, the if statement’s code block is executed. That code contains a cascaded if statement that evaluates three keyboard key options: Control and Shift pressed together, only Control, and only Shift. Each of these outputs a message with Print().

When leftClick equals false, the else if statement is executed that checks whether rightClick is true. When it is, a short Print() statement is executed. See mouse clicks and keyboard keys to learn more about working with Control and Shift.

Being able to determine whether a mouse click happened is helpful with all mouse click keywords, such as those for the data series number and time and date of a click, since these keywords only return a valid value after a mouse click.

Summary

When the ProcessMouseEvents attribute is set to true, a mouse click on the chart triggers a script calculation for the current bar. With GetAppInfo(aiCalcReason) the calculation reason can be retrieved to see whether a mouse click occurred.


References

Henry MultiCharts (2012, June 22). Mouse Events / clicks. Retrieved on January 6, 2015, from http://www.multicharts.com/discussion/viewtopic.php?f=1&t=10527#p51529

MultiCharts Wiki (2012, February 24). ProcessMouseEvents. Retrieved on December 27, 2014, from http://www.multicharts.com/trading-software/index.php/ProcessMouseEvents

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