Keystrokes within an EA

 

Hi everyone,

I am trying to get my EA to send the key command "ALT-R" (Tile chart windows) periodically. I'm using the following code.

#include <WinUser32.mqh>
#import "User32.dll"

void Tile_Windows() 
   {
   keybd_event(12, 0, 0, 0); // ALT down
   keybd_event(52, 0, 0, 0); // R down
   keybd_event(52, 0, 2, 0); // R up
   keybd_event(12, 0, 2, 0); // ALT up
   }

It's not working as it should. I'm fairly sure that the HEX codes for the keys are correct, but for some reason it generates a "4" instead when the cursor is somewhere that allows text input. Any ideas?

 
DrBeardface: I'm fairly sure that the HEX codes for the keys are correct,
   keybd_event(12, 0, 0, 0); // ALT down
   keybd_event(52, 0, 0, 0); // R down
   keybd_event(52, 0, 2, 0); // R up
   keybd_event(12, 0, 2, 0); // ALT up
Except you are not sending hex codes but integers.
#define VK_MENU                          0x12    // ALT key 
#define VK_R                            0x52    // R key 
          See WinUser32.mqh at Free download of the 'Programmatic modification of Tester "From:" and "To:" fields with user32.dll' script by 'mfurlend' for MetaTrader 4 in the MQL5 Code Base
 
Perfect as always. Thanks!!
 
Is there a way to check that the MT4 terminal window has focus before executing the key press? To avoid pressing ALT-R in some other random program.