Calling a script from an indicator by simulating a hot key

 

Hello,

I want my indicator to execute an order but I know that is only possible by a script or EA. I don't want to use an EA here and therefore the only possibility is to use a script and I read that the only method to execute a script from an indicator is by simulating a hotkey-keystroke which starts the script.

So i tried this one:

#import "user32.dll"
    void keybd_event(int bVk, int bScan, int dwFlags,int dwExtraInfo);
#import


keybd_event(0xA2, 0, 0, 0);      // press left CTRL
keybd_event(0x51, 0, 0, 0);      // press Q
keybd_event(0xA2, 0, 0x0002, 0); // release left CTRL
keybd_event(0x51, 0, 0x0002, 0); // release Q

 Unfortunately, it doesn't simulate a CTRL-Q. But why? I don't see an error here...

 

I just tested it with one single keystroke and that works fine. What am I doing wrong when simulating CTRL and Q together?

This works:

#import "user32.dll"
    void keybd_event(int bVk, int bScan, int dwFlags,int dwExtraInfo);
#import
#define  VK_F9 0x78              // F9
#define  KEYEVENTF_KEYUP 0x0002  // key up

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   keybd_event(VK_F9, 0, 0, 0);
   keybd_event(VK_F9, 0, KEYEVENTF_KEYUP , 0);
  }
//+------------------------------------------------------------------+
 
mar:

Unfortunately, it doesn't simulate a CTRL-Q. But why? I don't see an error here... 

The dwFlags for the first call needs to be 1 (KEYEVENTF_EXTENDEDKEY), not zero:

keybd_event(0xA2, 0, 1, 0);      // press left CTRL
keybd_event(0x51, 0, 0, 0);      // press Q
keybd_event(0xA2, 0, 0x0002, 0); // release left CTRL
keybd_event(0x51, 0, 0x0002, 0); // release Q
 
Works perfect now! Thank you very much!
 
The problem with keyboard events is that it maps a key rather than a character. So changing layout from QWERTY to AZERTY would result in a misinterpretation. If it is just for your personal use, then it is probably OK, but for distribution it might need switching to a defined layout (US Intl. layout have all).
 

Thanks for that advice but this is only for my personal use.

I just noticed that there is still a bug in it. The script is executed as it should be but obviously the CTRL key is still pressed even I release it. When I press E on my keyboard normally nothing should happen but it toggles on/off the expert function. Only when I press the RIGHT CTRL key once, everything works fine again. But why?? I only simulate a left CTRL keystroke. And if I press the left CTRL key it stays active.

So it seems to me that by pressing the left CTRL key the keyboard is in a CTRL lock and it can only be released by pressing the right CTRL key. What's wrong here? 

 
mar:

So it seems to me that by pressing the left CTRL key the keyboard is in a CTRL lock and it can only be released by pressing the right CTRL key. What's wrong here? 

Try altering dwFlags=2 in the last call to dwFlags=3. I haven't tried that, but it's probably the answer. (i.e. a combination of KEYEVENTF_KEYUP and KEYEVENTF_EXTENDEDKEY, matching the fact that the key-down is KEYEVENTF_EXTENDEDKEY)
 
Ovo:
The problem with keyboard events is that it maps a key rather than a character. So changing layout from QWERTY to AZERTY would result in a misinterpretation. If it is just for your personal use, then it is probably OK, but for distribution it might need switching to a defined layout (US Intl. layout have all).

Well, I'll take your word for it, but the virtual key codes which this function uses should be independent of the keyboard layout. The A key should result in generation of code 0x41 regardless of whether the physical layout is QWERTY or AZERTY.

https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx 

 
jjc:

Well, I'll take your word for it, but the virtual key codes which this function uses should be independent of the keyboard layout. The A key should result in generation of code 0x41 regardless of whether the physical layout is QWERTY or AZERTY.

https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx 

Yes, it is the 'A' key - the one next to the caps-lock. It is independent on what the layout is.
 

@jjc: I changed 0x0002 to 0x0003 and it works like it should. Thanks! :)

By the way... where can I learn Win-coding-stuff like this? I googled a lot but I didn't find anything useful. 

 
mar:

@jjc: I changed 0x0002 to 0x0003 and it works like it should. Thanks! :)

By the way... where can I learn Win-coding-stuff like this? I googled a lot but I didn't find anything useful. 

I usually rely on the Microsoft ISDN site. But this topic has probably best info here.

Reason: