Download MetaTrader 5

Can I simulate a real mouseclick on a button with a script?

To add comments, please log in or register
Marcus Riemenschneider
198
Marcus Riemenschneider  

Hey guys,

I use a button in an indicator and I wonder if it is possible to simulate a mouseclick on a button object? I only know that the visual attributes of a button can be changed with a script but not its function. Do I miss something or is it simply not possible?

nicholishen
1065
nicholishen  
Marcus Riemenschneider:

Hey guys,

I use a button in an indicator and I wonder if it is possible to simulate a mouseclick on a button object? I only know that the visual attributes of a button can be changed with a script but not its function. Do I miss something or is it simply not possible?


It's not really clear what you mean by simulating a mouse click but here's an example of how to change the state of a button with a keystroke instead of a mouse click. 

//+------------------------------------------------------------------+
//|                                           ButtonClickKeyDown.mq4 |
//|                                      Copyright 2017, nicholishen |
//|                         https://www.forexfactory.com/nicholishen |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, nicholishen"
#property link      "https://www.forexfactory.com/nicholishen"
#property version   "1.00"
#property strict
#property indicator_chart_window
#include <ChartObjects\ChartObjectsTxtControls.mqh>
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
input string key_press = "x";
uchar key_char;
CChartObjectButton button;

int OnInit()
  {
//--- indicator buffers mapping
   button.Create(0,"button",0,0,0,303,30);
   button.Description("TEST");
   char arr[];
   StringToCharArray(key_press,arr,0,1);
   if(ArraySize(arr)>=1)
      key_char = arr[0];
   else 
      return INIT_FAILED;
  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(id == CHARTEVENT_KEYDOWN)
   {
      if(uchar(TranslateKey((int)lparam)) == key_char)
         button.State(!button.State());
   }
  }
//+------------------------------------------------------------------+
Marcus Riemenschneider
198
Marcus Riemenschneider  
nicholishen:

It's not really clear what you mean by simulating a mouse click but here's an example of how to change the state of a button with a keystroke instead of a mouse click. 

Unfortunately this doesn’t solve my problem. 

Imagine the following: I use an indicator and when you press a button object, the alert window pops up.

If I change the button’s state with a script the alert window doesn’t pop up. The button’s underlying function only works if the user does a real mouse click on the button. And this was my question. Is it possible to simulate a real mouse click on a button. Only changing its state with a script doesn’t work.

nicholishen
1065
nicholishen  
Marcus Riemenschneider:

Unfortunately this doesn’t solve my problem. 

Imagine the following: I use an indicator and when you press a button object, the alert window pops up.

If I change the button’s state with a script the alert window doesn’t pop up. The button’s underlying function only works if the user does a real mouse click on the button. And this was my question. Is it possible to simulate a real mouse click on a button. Only changing its state with a script doesn’t work.


That's 100% dependent on the code you wrote. This example works as you would expect.

//+------------------------------------------------------------------+
//|                                           ButtonClickKeyDown.mq4 |
//|                                      Copyright 2017, nicholishen |
//|                         https://www.forexfactory.com/nicholishen |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, nicholishen"
#property link      "https://www.forexfactory.com/nicholishen"
#property version   "1.00"
#property strict
#property indicator_chart_window
#include <ChartObjects\ChartObjectsTxtControls.mqh>
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
input string key_press = "x";
uchar key_char;
CChartObjectButton button;

int OnInit()
  {
//--- indicator buffers mapping
   button.Create(0,"button",0,0,0,303,30);
   button.Description("TEST");
   char arr[];
   StringToCharArray(key_press,arr,0,1);
   if(ArraySize(arr)>=1)
      key_char = arr[0];
   else 
      return INIT_FAILED;
  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   if(id == CHARTEVENT_KEYDOWN || id==CHARTEVENT_OBJECT_CLICK)
   {
      if(uchar(TranslateKey((int)lparam)) == key_char)
         button.State(!button.State());
      ButtonFunc();
   }
}
//+------------------------------------------------------------------+
void ButtonFunc()
{
   static bool last_state = false;
   if(button.State() != last_state)
   {
      PlaySound("expert.wav");
      last_state = button.State();
   }
}
To add comments, please log in or register