Mouse clicks in PowerLanguage become especially useful when combined with the Control and Shift key. That way mouse clicks can be processed in different ways depending on the keys pressed. So, how can Shift and Control be used programmatically?

In this article:

Combining mouse clicks with keyboard keys in MultiCharts

Programmatically processing mouse clicks in PowerLanguage is possible after the ProcessMouseEvents attribute is added to the script and set to true (MultiCharts Wiki, 2012a). For example:

Example of setting the ProcessMouseEvents attribute in MultiCharts

Now with ProcessMouseEvents enabled, two keyboard keys can be used alongside mouse clicks:

  • When the Control key is pressed down during a mouse click, the MouseClickCtrlPressed keyword returns true and false otherwise (MultiCharts Wiki, 2012b).
  • And when the Shift key is held down during a mouse click, MouseClickShiftPressed returns true and false otherwise (MultiCharts Wiki, 2012c).

Both keywords recognise the left and right keyboard key, and so it doesn’t matter whether we use the left Control or right Control key and there also isn’t a difference between left and right Shift. We can, however, combine MouseClickCtrlPressed and MouseClickShiftPressed with the left and right mouse buttons.

Characterising different keyboard combinations in MultiCharts

What makes the MouseClickCtrlPressed and MouseClickShiftPressed keywords especially useful is that they can be combined to check for keyboard shortcuts. A quick example that shows the different Control and Shift combinations is the following:


[ProcessMouseEvents = true];

if (MouseClickCtrlPressed or MouseClickShiftPressed) then begin

    Print("* * * *");

    if (MouseClickCtrlPressed) then 
        Print("Control was pressed down");

    if (MouseClickShiftPressed) then
        Print("Shift was pressed down");

    if (MouseClickCtrlPressed and not MouseClickShiftPressed) then
        Print("Control was pressed down, but Shift wasn't");

    if (not MouseClickCtrlPressed and MouseClickShiftPressed) then
        Print("Control wasn't pressed down, but Shift was");

    if (MouseClickCtrlPressed and MouseClickShiftPressed) then
        Print("Both Control and Shift were pressed down");

end;

//> * * * *
//> Control was pressed down
//> Control was pressed down, but Shift wasn't
//> * * * *
//> Shift was pressed down
//> Control wasn't pressed down, but Shift was
//> * * * *
//> Control was pressed down
//> Shift was pressed down
//> Both Control and Shift were pressed down

We start by enabling mouse clicks through setting the ProcessMouseEvents attribute to true. An if statement then evaluates whether a mouse click happened with the Control (MouseClickCtrlPressed) or Shift (MouseClickShiftPressed) key held down. In that case, one of these keywords returns true and the condition of the if statement would satisfy:


if (MouseClickCtrlPressed or MouseClickShiftPressed) then begin

    Print("* * * *");

    if (MouseClickCtrlPressed) then 
        Print("Control was pressed down");

    if (MouseClickShiftPressed) then
        Print("Shift was pressed down");

    if (MouseClickCtrlPressed and not MouseClickShiftPressed) then
        Print("Control was pressed down, but Shift wasn't");

    if (not MouseClickCtrlPressed and MouseClickShiftPressed) then
        Print("Control wasn't pressed down, but Shift was");

    if (MouseClickCtrlPressed and MouseClickShiftPressed) then
        Print("Both Control and Shift were pressed down");

end;

When indeed the Control or Shift key was pressed during the click, the code inside this if statement is executed. That code begins with the Print() keyword to output data to the Output tab in the PowerLanguage Editor (MultiCharts Wiki, 2012e). The outputted text are simply four asterisks ("* * * *") and these simply help to keep the output of the different clicks separate.

The remaining code in this if statement consists out of 5 different if statements, each evaluating the MouseClickCtrlPressed and/or MouseClickShiftPressed keywords. All of these if statements output an informational message to the Output tab that states which key was pressed.

The first and second if statements simply evaluate whether the Control or Shift key were pressed and, if one or both were, output that information with Print().

The last three if statements are a bit more elaborate. The first of these evaluates whether the Control key was pressed (MouseClickCtrlPressed) but without the Shift key held down. We check for that latter by placing not before MouseClickShiftPressed. That not keyword returns the opposite of a true/false expression (MultiCharts Wiki, 2012d). This means that, whenever MouseClickShiftPressed returns true, not MouseClickShiftPressed returns false. And when the Shift key isn’t pressed, MouseClickShiftPressed returns false but not MouseClickShiftPressed will return true then.

Similarly, the next if statement places not before MouseClickCtrlPressed to see whether Control wasn’t pressed. Since that if statement’s condition also includes MouseClickShiftPressed without not, Shift (but not Control) needs to be held down to make that if statement true.

The last if statement in this code block evaluates whether Control (MouseClickCtrlPressed) and Shift (MouseClickShiftPressed) were both (and) pressed down when a mouse click happened on the chart. When that’s the case, an accompanying message is outputted with Print().

Depending on how you click on the chart when the script is added to it, the above example generates an output like:

* * * *
Control was pressed down
Control was pressed down, but Shift wasn't
* * * *
Shift was pressed down
Control wasn't pressed down, but Shift was
* * * *
Control was pressed down
Shift was pressed down
Both Control and Shift were pressed down

Differentiating between mouse clicks with keyboard keys

The benefit of using keyboard keys is that we have more control over how mouse clicks are processed programmatically, for instance when reloading a chart:


[ProcessMouseEvents = true];

if (MouseClickCtrlPressed = true and 
    MouseClickShiftPressed = false) then
        CommandLine(".rld int=1day")

else if (MouseClickCtrlPressed = false 
    and MouseClickShiftPressed = true) then
        CommandLine(".rld int=1week")

else if (MouseClickCtrlPressed = true and 
    MouseClickShiftPressed = true) then 
        CommandLine(".rld");

MouseClickCtrlPressed and MouseClickShiftPressed are used in one three ways here. When Control is held down, but Shift not (the first if statement), CommandLine() is executed with the .rld int=1day command to reload one day of price data on the chart (see MultiCharts Wiki, 2014).

When Shift was pressed while the click occurred, but Control not (the second if statement), one week worth of price data is reloaded with the .rld Command Line command (.rld int=1week). And when both the Control and Shift key were used (the last if statement), the .rld Command Line command is executed to reload the full data range of all data series on the chart (MultiCharts Wiki, 2014).

Other examples that use mouse clicks with keyboard keys include plotting arrows with mouse clicks and submitting market orders with the mouse.

Summary

Mouse clicks on the chart can be processed when the ProcessMouseEvents attribute is enabled. The MouseClickCtrlPressed keyword returns true when a mouse click happened while the Control key was held down; MouseClickShiftPressed indicates whether a click occurred with Shift.


References

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

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

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

MultiCharts Wiki (2012d, February 19). Not. Retrieved on January 3, 2015, from http://www.multicharts.com/trading-software/index.php/Not

MultiCharts Wiki (2012e, February 13). Print. Retrieved on March 1, 2016, from https://www.multicharts.com/trading-software/index.php/Print

MultiCharts Wiki (2014, August 18). MultiCharts Work Area: Understanding Command Line. Retrieved on January 4, 2015, from http://www.multicharts.com/trading-software/index.php/MultiCharts_Work_Area#Understanding_Command_Line

Visit Kodify.net for more helpful coding articles.