Download MetaTrader 5
To add comments, please log in or register
Have you made a screenshot in MetaTrader? Share it with friends!
Black_Jack822
57
Black_Jack822 2015.11.21 18:51 
Hi all. I'm trying to make simple indi which will speed up placing sup/res lines on the chart.
It will place Hline on mouse click only while holding (ctrl for Blue or shift for Red line)  but I don't know how to combine CHARTEVENT_KEYDOWN & CHARTEVENT_CLICK or I'm doing it the wrong way?

Can someone help me with this please.


#property script_show_inputs
#property indicator_chart_window
//---------------------------------------------------------------
#import "user32.dll"
void keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo);

#define  VK_CONTROL        0x11          
#define  VK_SHIFT          0x10
//+------------------------------------------------------------------+
int init()
 {
   return(0);
 }
 //+------------------------------------------------------------------+
int deinit()
  {
  return(0);
}
//---------------------------------------------------------------
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
      return(0);
  }
//+------------------------------------------------------------------+ 
void OnChartEvent(const int id,         // Event identifier  
                  const long& lparam,   // Event parameter of long type
                  const double& dparam, // Event parameter of double type
                  const string& sparam) // Event parameter of string type
{
      datetime dt    =0;
      double   price =0;
      int      window=0;

      int TimeNow = TimeCurrent();
      ChartXYToTimePrice(0,lparam,dparam,window,dt,price);
      
   if (id==CHARTEVENT_KEYDOWN && id==CHARTEVENT_CLICK) 
      if (lparam == (VK_SHIFT))
   //-----------
   //???????????
   //-----------
      {
      ObjectCreate("Support"+ TimeNow,OBJ_HLINE,0,0,price);
      ObjectSet("Support" + TimeNow,OBJPROP_WIDTH,2);  
      ObjectSet("Support" + TimeNow,OBJPROP_COLOR,Red);
      } 

   if (id==CHARTEVENT_CLICK)   //VK_CONTROL
      {
      ObjectCreate("Resistance"+ TimeNow,OBJ_HLINE,0,0,price);
      ObjectSet("Resistance" + TimeNow,OBJPROP_WIDTH,2);  
      ObjectSet("Resistance" + TimeNow,OBJPROP_COLOR,Blue);
      }      
}
//+------------------------------------------------------------------+
int del_obj()  {
//+------------------------------------------------------------------+
  return(0);
}  
Stuart Browne
5609
Stuart Browne 2015.11.22 04:29  
Black_Jack822:
Hi all. I'm trying to make simple indi which will speed up placing sup/res lines on the chart.
It will place Hline on mouse click only while holding (ctrl for Blue or shift for Red line)  but I don't know how to combine CHARTEVENT_KEYDOWN & CHARTEVENT_CLICK or I'm doing it the wrong way?

Can someone help me with this please.


Hi Jack

It's a bit of a pain in native MQL as there is no KEYUP event. Also, if the user is say holding down CTRL, then they hold down and release SHIFT, you'll get no more KEY events. And thirdly, whilst the dparam is supposed to hold the repeat count of a key being pressed, it doesn't seem to work (in MQL4 at least, not sure about MQL5). So all this makes it very difficult to track keydown & up events.

You will also have an issue with your statement 

if (id==CHARTEVENT_KEYDOWN && id==CHARTEVENT_CLICK)

I don't believe you will ever get this statement to be true. The chart event will be called once for the keydown and once for the click, each with a different lparam and more importantly, different "EVENT" type  

One work around would be to set a global bool like CTRL_DOWN & SHIFT_DOWN & set it to true when you capture a key event. Then when the chart event captures a click, check the state of those vars. Of course, you could run into the problem where say the CTRL key was pressed, then released but the bool would not reset. So you would also have to come up with a solution for reseting those events.

The final option would be to use windows DLLs which is fine if the program is only going to be yours, but can't be used if you want to sell it on the market in MQL5.com

Hope that points you in the right direction mate.

 

Black_Jack822
57
Black_Jack822 2015.11.22 11:43  
Stuart Browne:

Hi Jack

It's a bit of a pain in native MQL as there is no KEYUP event. Also, if the user is say holding down CTRL, then they hold down and release SHIFT, you'll get no more KEY events. And thirdly, whilst the dparam is supposed to hold the repeat count of a key being pressed, it doesn't seem to work (in MQL4 at least, not sure about MQL5). So all this makes it very difficult to track keydown & up events.

You will also have an issue with your statement 

I don't believe you will ever get this statement to be true. The chart event will be called once for the keydown and once for the click, each with a different lparam and more importantly, different "EVENT" type  

One work around would be to set a global bool like CTRL_DOWN & SHIFT_DOWN & set it to true when you capture a key event. Then when the chart event captures a click, check the state of those vars. Of course, you could run into the problem where say the CTRL key was pressed, then released but the bool would not reset. So you would also have to come up with a solution for reseting those events.

The final option would be to use windows DLLs which is fine if the program is only going to be yours, but can't be used if you want to sell it on the market in MQL5.com

Hope that points you in the right direction mate.

 

Hi Stuart, thank you for that explanation. And like you've said it is a bit of a pain :/ I thought it's gonna be easier.
Thanks anyway.
/
To add comments, please log in or register