Chart event when mouse button is pressed

 

Hi Gurus,

Can I get a chart event, when I click on mouse button (BEFORE I release the button)?

As I experienced, the event occurs when I release the button.

Thanks

 
Check the mouse move rather than the mouse click event.
 
Ovo:
Check the mouse move rather than the mouse click event.
I need all of them (as when you use the crosshair). Click down and move as well.
 

Ok, I thought you needed a clue. So, here is the doc for the mouse move, copy/pasted.

 For CHARTEVENT_MOUSE_MOVE event the sparam string parameter contains information about state of the keyboard and mouse buttons:

Bit

Description

1

State of the left mouse button

2

State of the right mouse button

3

State of the SHIFT button

4

State of the CTRL button

5

State of the middle mouse button

6

State of the first extra mouse button

7

State of the second extra mouse button

 
Ovo:

Ok, I thought you needed a clue. So, here is the doc for the mouse move, copy/pasted.

 For CHARTEVENT_MOUSE_MOVE event the sparam string parameter contains information about state of the keyboard and mouse buttons:

Bit

Description

1

State of the left mouse button

2

State of the right mouse button

3

State of the SHIFT button

4

State of the CTRL button

5

State of the middle mouse button

6

State of the first extra mouse button

7

State of the second extra mouse button

Thank you, Ovo!
 

I found this topic because I've a similar issue.

I realize that CHARTEVENT_OBJECT_CLICK occurs when left button of mouse is released and not pressed... So if I click on any point on chart and release left mouse button on my OBJECT BUTTON, event occurs (and this can create a lot of problems).


   if(id==CHARTEVENT_OBJECT_CLICK )
     {
      if(sparam=="Button_Buy")
        {
           open(OP_BUY);
           guiRefresh();
        }
     }


I've tried to add a check using CHARTEVENT_MOUSE_MOVE but I've no success.


  string MouseState(uint state)
  {
   string res;
   res+=(((state& 1)== 1)?"DN":"UP");   // mouse left
   return(res);
  }
   
   if(id==CHARTEVENT_MOUSE_MOVE && MouseState((uint)sparam)=="DN"  )   {
      leftClick=true;
   }

   if(id==CHARTEVENT_OBJECT_CLICK && leftClick )
     {
      if(sparam=="Button_Buy")
        {
           open(OP_BUY);
           guiRefresh();
        }
     }


Can someone help me in trying fixing this issue?

Thanks

 

Anyone can help? A lot of commercial indicator/EA has this issue fixed but can't find a solution on internet.


In few words, the issue is that CHARTEVENT_OBJECT_CLICK is triggered when left button of mouse been released and not when it's clicked....... It seems like a bug in the MT4 function, but I'm pretty sure that there is a workaround

 
Fabio Cavalloni:

Anyone can help? A lot of commercial indicator/EA has this issue fixed but can't find a solution on internet.


In few words, the issue is that CHARTEVENT_OBJECT_CLICK is triggered when left button of mouse been released and not when it's clicked....... It seems like a bug in the MT4 function, but I'm pretty sure that there is a workaround

There is no bug and triggering the event on release of a button is the right behaviour.

Deal with it. What is the real problem ?

 
Alain Verleyen:

There is no bug and triggering the event on release of a button is the right behaviour.

Deal with it. What is the real problem ?

If I click in any part of the graph, mantaining mouse clicked and releasing mouse on the button, the action of button is triggered.

You can think that it's a very rare case but in some cases I noticed that it occurs and can be very annoying if button triggers some orders, for example, and confirmation boxes are not used. It often occurs if I scroll chart horizontally.


I don't understand why action is linked to the button release and not to the button click.....

 
Fabio Cavalloni:

If I click in any part of the graph, mantaining mouse clicked and releasing mouse on the button, the action of button is triggered.

You are right, and it probably should not behave like this (a click event should be down/up on the same "object" only). Good luck if you want Metaquotes to recognize it as a bug.

You can think that it's a very rare case but in some cases I noticed that it occurs and can be very annoying if button triggers some orders, for example, and confirmation boxes are not used. It often occurs if I scroll chart horizontally.

So you can monitor to mouse down event, check if there is a button implied and if not, later, cancel the click.

I don't understand why action is linked to the button release and not to the button click.....

Because it's like that from the start of mouse usage, that will not change for sure.

Why is a click event triggered only after mouse click and release?
Why is a click event triggered only after mouse click and release?
  • 2013.12.09
  • Prem AnandPrem Anand 15422 silver badges66 bronze badges
  • softwareengineering.stackexchange.com
Every click event on the web works on mouse click and release. Is there a way to do a action on a click event before mouse release??
 

I found a workaround checking for mouse position on Mouse Left DOWN and Mouse Left UP and comparing them.

Hope that it can help someone else!

bool   MouseDown = false, MouseClicked = false;
string ObjectDown = "", ObjectClick = "";

int OnInit() {
   ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,1);
}

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam
                 ) {
   if(id==CHARTEVENT_MOUSE_MOVE) {
      if( MouseLeftButtonState((uint)sparam) == "DN" && !MouseDown ) {
         MouseDown = true;
         MouseClicked = false;
         ObjectDown = ButtonZone(lparam,dparam);
      }
      if( MouseLeftButtonState((uint)sparam) == "UP" && MouseDown ) {
         MouseClicked = true;
         MouseDown = false;
         ObjectClick = ButtonZone(lparam,dparam);
         //Print("Mouse click completed! Mouse DN: "+ObjectDown+" Mouse UP: "+ObjectClick);
      }
   }
   if(MouseClicked && ObjectClick==ObjectDown) {
      if(ObjectClick=="Button.Name") {
         // BUTTON ACTIONS
      }
      MouseClicked = false;
      ObjectDown   = "";
      ObjectClick  = "";
   }
}

string MouseLeftButtonState(uint state) {
   string res;
   res+=(((state& 1)== 1)?"DN":"UP");   // mouse left
   return(res);
}

string ButtonZone(long lparam, double dparam) {
   long ChartX = ChartGetInteger(0,CHART_WIDTH_IN_PIXELS);
   long ChartY = ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS);
   for( int i=ObjectsTotal(); i>=0; i-- ) {
      string name = ObjectName(i);
      long X_Start=0, X_Size=0, X_End=0, Y_Start=0, Y_Size=0, Y_End=0;
      if( ObjectType(name)!=OBJ_BUTTON ) continue;
      X_Size = (int)ObjectGetInteger(0,name,OBJPROP_XSIZE);
      Y_Size = (int)ObjectGetInteger(0,name,OBJPROP_YSIZE);
      switch((int)ObjectGetInteger(0,name,OBJPROP_CORNER)) {
         case CORNER_LEFT_UPPER : { 
            X_Start = (long)ObjectGetInteger(0,name,OBJPROP_XDISTANCE);
            Y_Start = (long)ObjectGetInteger(0,name,OBJPROP_YDISTANCE);
            break;
         }
         case CORNER_RIGHT_UPPER : { 
            X_Start = ChartX-(long)ObjectGetInteger(0,name,OBJPROP_XDISTANCE);
            Y_Start = (long)ObjectGetInteger(0,name,OBJPROP_YDISTANCE);
            break;
         }
         case CORNER_LEFT_LOWER : { 
            X_Start = (long)ObjectGetInteger(0,name,OBJPROP_XDISTANCE);
            Y_Start = ChartY-(long)ObjectGetInteger(0,name,OBJPROP_YDISTANCE);
            break;
         }
         case CORNER_RIGHT_LOWER : { 
            X_Start = ChartX-(long)ObjectGetInteger(0,name,OBJPROP_XDISTANCE);
            Y_Start = ChartY-(long)ObjectGetInteger(0,name,OBJPROP_YDISTANCE);
            break;
         }
      }
      X_End   = X_Start + X_Size;
      Y_End   = Y_Start + Y_Size;
      if( lparam >= X_Start && lparam <= X_End && dparam >= Y_Start && dparam <= Y_End ) return(name);
   }
   return("");
}