Help with OnChartEvent(...) (id == CHARTEVENT_KEYDOWN)

 

I'm not getting the correct characters to display for the corresponding keystroke events. For example when I press the "," key I get "1/4". Am I doing something wrong?


Here is my sample code:

#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| 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_KEYDOWN)
      Print("CharToString("+CharToString((uchar)lparam)+")");   

}
//+------------------------------------------------------------------+
 

I was able to answer my own question, but leaving the answer here in case anyone else runs into the same issue. The keystroke needed to be translated using the TranslateKey() function

https://docs.mql4.com/common/translatekey

#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| 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_KEYDOWN)
      Print("ShortToString("+ShortToString(TranslateKey((int)lparam))+")");   

}
//+------------------------------------------------------------------+
TranslateKey - Common Functions - MQL4 Reference
TranslateKey - Common Functions - MQL4 Reference
  • docs.mql4.com
TranslateKey - Common Functions - MQL4 Reference
 
Emma Schwatson:

I was able to answer my own question, but leaving the answer here in case anyone else runs into the same issue. The keystroke needed to be translated using the TranslateKey() function

https://docs.mql4.com/common/translatekey

Thanks for posting your fix . this helped me 
Reason: