Help with Chart Events

 

Hi guys, 

I need your help :). I want to combine two different chart events. For example if I press a key and the left mouse button at the same time, then another action should follow ( e.g an object is created).

Does anyone have an idea how to realize this?

Thanks for your help! 

 

I don't think that you can check for 2 chart events at the same time.

You can use CHARTEVENT_KEYDOWN to set a boolean flag 

Then CHARTEVENT_CLICK that checks for the flag and does whatever you want 

 

Hi GumRai,

thanks for your quick answer. I know that you can use these two chartevent-functions. Actually, i wanted to press a key and then a special object is drawn at the coordinates that are given by the mouse which was clicked before. So what do you think about storing the lparam and dparam values of the latest mouse click. Do you think that maybe this is possible and know how to do this? 

 
If your keys were only limited to shift and ctrl, you could consider using the CHARTEVENT_MOUSE_MOVE.
 
Ovo:
If your keys were only limited to shift and ctrl, you could consider using the CHARTEVENT_MOUSE_MOVE.

Yeah i saw something in the codebase but I didn't understand the code. Chould you please give me an example or explain it to me?
 

Ooops, that would be a guess, as I have been using custom classes in a custom framework for two years.. the base event wrappers example appended just for curiosity. Try to learn inspiration from docs example: 

https://docs.mql4.com/constants/chartconstants/enum_chartevents

 

/**
 * Chart event object with accessors.
 */
class MT4ChartEvent: public MT4BaseEvent {
public:
   MT4ChartEvent() {}
   MT4ChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam):
         MT4BaseEvent(id,lparam,dparam,sparam) {}
  
   bool isMouseMove() {
        return (getType() == CHARTEVENT_MOUSE_MOVE);
   }
   
   bool isMouseClick() {
        return (getType() == CHARTEVENT_CLICK);
   }

   bool isObjectClick() {
        return (getType() == CHARTEVENT_OBJECT_CLICK);
   }
   
   bool isKeyDown() {
        return (getType() == CHARTEVENT_KEYDOWN);
   }
   
   bool isChartChange() {
      return (getType() == CHARTEVENT_CHART_CHANGE);
   }
   
   bool isObjectChange(string drawObject="") {
      bool returnValue = (getType() == CHARTEVENT_OBJECT_CHANGE);
      if (""!=drawObject) {
         returnValue &= (getStringValue()==drawObject);
      }
      return returnValue;
   }
   
   bool isObjectDrag(string drawObject="") {
      bool returnValue = (getType() == CHARTEVENT_OBJECT_DRAG);
      if (""!=drawObject) {
         returnValue &= (getStringValue()==drawObject);
      }
      return returnValue;
   }
   
   
};

#define MOUSE_MOD_SHIFT 0x04
#define MOUSE_MOD_CTRL 0x08

class MT4ClickEvent: public MT4ChartEvent {
   uchar modifier;
public:
   MT4ClickEvent(const int id,const long &lparam,const double &dparam,const string &sparam, const uchar aModifier):MT4ChartEvent(id,lparam,dparam,sparam), modifier(aModifier) {
   }

   bool isShift() {
      return ((modifier & MOUSE_MOD_SHIFT) == MOUSE_MOD_SHIFT);
   }
   bool isCtrl() {
      return ((modifier & MOUSE_MOD_CTRL) == MOUSE_MOD_CTRL);
   }   
};
 
Ovo:

Ooops, that would be a guess, as I have been using custom classes in a custom framework for two years.. the base event wrappers example appended just for curiosity. Try to learn inspiration from docs example: 

https://docs.mql4.com/constants/chartconstants/enum_chartevents

 

 


 Yes I already read this post and I used the 2nd example script on the page. Here it is:

 

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- enable CHART_EVENT_MOUSE_MOVE messages
   ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,1);
  }
//+------------------------------------------------------------------+
//| MouseState                                                       |
//+------------------------------------------------------------------+
string MouseState(uint state)
  {
   string res;
   res+="\nML: "   +(((state& 1)== 1)?"DN":"UP");   // mouse left
   res+="\nMR: "   +(((state& 2)== 2)?"DN":"UP");   // mouse right 
   res+="\nMM: "   +(((state&16)==16)?"DN":"UP");   // mouse middle
   res+="\nMX: "   +(((state&32)==32)?"DN":"UP");   // mouse first X key
   res+="\nMY: "   +(((state&64)==64)?"DN":"UP");   // mouse second X key
   res+="\nSHIFT: "+(((state& 4)== 4)?"DN":"UP");   // shift key
   res+="\nCTRL: " +(((state& 8)== 8)?"DN":"UP");   // control key
   return(res);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(id==CHARTEVENT_MOUSE_MOVE)
      Comment("POINT: ",(int)lparam,",",(int)dparam,"\n",MouseState((uint)sparam));
  }

Every time I click the mouse for a longer time it says DN for that period. If not, it says UP. But I am a Mac User so I have other codes of the keys . So I tried to change e.g. the shift code 4 to 16, because thats the mac lparam code. But nothing happend. Do you know where my problem is?

 

Timerider:

Every time I click the mouse for a longer time it says DN for that period. If not, it says UP. But I am a Mac User so I have other codes of the keys . So I tried to change e.g. the shift code 4 to 16, because thats the mac lparam code. But nothing happend. Do you know where my problem is? 

Not really, the 08 and 04 values do work for me. I use them like in this software, and I would be curious if it fails on a Mac.

 

 
Ovo:

Not really, the 08 and 04 values do work for me. I use them like in this software, and I would be curious if it fails on a Mac.

 

But if I press the shift key, in the script I've attached, nothing happened. Maybe I misunderstood something. I thought that the number in the script is the lparam value and the lparam value of shift for Mac is 16.
 
Timerider:
But if I press the shift key, in the script I've attached, nothing happened. Maybe I misunderstood something. I thought that the number in the script is the lparam value and the lparam value of shift for Mac is 16.
Try to print out only the raw parameters rather than the message.
 
Ovo:
Try to print out only the raw parameters rather than the message.

Hope you meant this:

 

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- enable CHART_EVENT_MOUSE_MOVE messages
   ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,1);
  }
//+------------------------------------------------------------------+
//| MouseState                                                       |
//+------------------------------------------------------------------+
string MouseState(uint state)
  {
   string res;
   if((state& 1)== 1) //works
   Alert("Left mouse clicked");
   
   if((state& 2)== 2) //works
   Alert("Right mouse clicked");

   
   if((state& 16)== 16) // 16 = shift but nothing happens
   Alert("Shift clicked");
   return(res);
   
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(id==CHARTEVENT_MOUSE_MOVE)
      Comment("POINT: ",(int)lparam,",",(int)dparam,"\n",MouseState((uint)sparam));
      }

I don't know why but the shift button doesn't work. 

Reason: