Simulating a hot key with keybd_event() causes problems

 

Hello coders,

when I press the button named "test", the key-combo ALT+Q should be simulated and a small script which has the MT4 hot key ALT-Q should be executed. 

Pretty easy but it doesn't work. If I use in-built-hot keys like ALT-R for the window-arrangement or ALT-M for the chat, it works perfect. But it never works when the simulated key-combo should run a script which has the corresponding hot key. When I press ALT-Q manually the script is executed without any problems. But why doesn't it work with my code?

#property strict
#property indicator_chart_window

#import "user32.dll"
    void keybd_event(int bVk, int bScan, int dwFlags,int dwExtraInfo);
#import
#define  REL   0x0002
#define  ALT   0x12  
#define  VK_Q  0x51

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   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_OBJECT_CLICK) {
      if (sparam=="test") {
         keybd_event(ALT, 0, 0, 0);
         keybd_event(VK_Q, 0, 0, 0);
         keybd_event(ALT, 0, REL, 0);
         keybd_event(VK_Q, 0, REL, 0);
      }
   }
}
Reason: