//+------------------------------------------------------------------+
//|                                                 ScrollToDate.mq4 |
//|                                         Copyright © 2011, zznbrm |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, zznbrm"
#property show_inputs

#import "user32.dll"
   void keybd_event( int bVk, int bScan, int dwFlags, int dwExtraInfo );
#import

#define VK_RETURN          0x0D
#define VK_BACK            0x08
#define VK_ESCAPE          0x1B
#define VK_CAPITAL         0x14
#define VK_COMMA           0xBC
#define VK_SHIFT           0x10
#define KEYEVENTF_KEYUP    0x0002

extern string estrDate = "2014.01.01";

//+------------------------------------------------------------------+
//| expert Start function                                            |
//+------------------------------------------------------------------+
int start()
{   
   changeChart( estrDate );       
   return( 0 );
}

//+------------------------------------------------------------------+
//| changeChart                                                      |
//+------------------------------------------------------------------+
void changeChart( string strDate )
{
   // We are not sure if the symbol entry field is currently visible.
   // So, let's send the ESC key to remove it just in case.
   sendKey( VK_ESCAPE, false );
   
   // Display the symbol entry field - use the ENTER key for this
   sendKey( VK_RETURN, false );
   
   // The symbol entry field may already have data in it.
   // Send some BACK keys (20 should be enough for most use cases)
   // to erase this data
   sendMultipleKeys( VK_BACK, 20, false );
   
   // Turn on caps lock
   //sendKey( VK_CAPITAL );
   
   // Send each character of symbol string
   sendKeysForString( strDate );
        
   // Turn off caps lock
   //sendKey( VK_CAPITAL );
   
   // Submit the data - use the ENTER key for this
   sendKey( VK_RETURN, false );
}

//+------------------------------------------------------------------+
//| sendKeysForString                                                |
//+------------------------------------------------------------------+
void sendKeysForString( string str )
{
   // Loop through all characters of the string, sending a key for each
   for ( int jnx = 0; jnx < StringLen( str ); jnx++ )
   {
      int intKey = StringGetChar( str, jnx );      
      
      if ( intKey == 46 ) 
      {
         // Special logic for period (.)
         sendKey( 0xBE, false );
      }
      else if ( intKey == 58 )
      {
         // Special logic for colon (:)
         sendKey( 0xBA, true );
      }
      else
      {      
         sendKey( intKey, false );
      }
   }
}

//+------------------------------------------------------------------+
//| sendMultipleKeys                                                 |
//+------------------------------------------------------------------+
void sendMultipleKeys( int intKey, int intCount, bool blnShift )
{
   // Send the key multiple times
   for ( int inx = 0; inx < intCount; inx++ )
   {
      sendKey( intKey, blnShift );
   }
}

//+------------------------------------------------------------------+
//| sendKey                                                          |
//+------------------------------------------------------------------+
void sendKey( int intKey, bool blnShift )
{
   if ( blnShift )
   {
      keybd_event( VK_SHIFT, 0, 0, 0 );
   }
   
   keybd_event( intKey, 0, 0, 0 );
   keybd_event( intKey, 0, KEYEVENTF_KEYUP, 0 );
   
   if ( blnShift )
   {
      keybd_event( VK_SHIFT, 0, KEYEVENTF_KEYUP, 0 );
   }
}

