Download MetaTrader 5

trigger a key press event?

To add comments, please log in or register
Nikolay
175
Nikolay  

How can I program triggering a key press/event?

For the following use case: Normally one can zoom with the "+" and "-" keys, but I would like to zoom with "q" and "r" and my idea is when "q" is pressed to trigger pressing the "+" key. Or are there better ideas how to create this use case?

Keith Watford
Moderator
10569
Keith Watford  
Do you know that you can click and drag the date/time axis. Right to zoom in, left to zoom out.
Nikolay
175
Nikolay  

I found it:


#import "user32.dll"

void keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo);

keybd_event(187,0,0,0); //+ button

keybd_event(189,0,0,0); //- button

Keycodes: http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html
Tecuciztecatl
70
Tecuciztecatl  
OnChartEvent() function  with CHARTEVENT_KEYDOWN is to survey the keyboard.
traderbigbear92
11
traderbigbear92  
Nikolay:

I found it:


#import "user32.dll"

void keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo);

keybd_event(187,0,0,0); //+ button

keybd_event(189,0,0,0); //- button

Keycodes: http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html


I am trying to make a custom indicator and want some thing like.

if(CTRL + Q) {

Alert("Hello World")

}

how would you code this?

The return value of the keybd_event is void so not sure how to do.

whroeder1
15590
whroeder1  
mql4 pressing two keyboard buttons at the same time
mql4 pressing two keyboard buttons at the same time
  • www.mql5.com
Hi all I want to make a custom indicator which detects the pressing down of multiple keys...
traderbigbear92
11
traderbigbear92  


Ok so I worked out what that those lines of code do. 

keybd_event(187,0,0,0); //+ button

means press + button. But how does mql4 detect multiple key presses? CHARTEVENT_KEYDOWN only takes one sparam argument.

whroeder1
15590
whroeder1  
traderbigbear92: But how does mql4 detect multiple key presses? CHARTEVENT_KEYDOWN only takes one sparam argument.
It doesn't like every other Windows program. Key (e.g. Alt) down - remember, key down - process key modified by (e.g. Alt.)
Roberto Jacobs
48230
Roberto Jacobs  
Nikolay:

I found it:


#import "user32.dll"

void keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo);

keybd_event(187,0,0,0); //  NOT + button, but =+ button

keybd_event(189,0,0,0); //  NOT - button, but -_ button

//----

keybd_event(107,0,0,0); //  107 the + code numbers keyboard

keybd_event(109,0,0,0); //  109 the - code numbers keyboard


//-- for Q and R

keybd_event(81,0,0,0); //  81 the Q code numbers keyboard

keybd_event(82,0,0,0); //  82 the R code numbers keyboard


//---

// I use this code on my indicator like code below
// and working smoothly, no problems.

#include <WinUser32.mqh>

#define KEY_PLUS   107
#define KEY_MINUS  109

void OnChartEvent(const int id,            // Event identifier
                  const long &lparam,      // Event parameter of long type
                  const double &dparam,    // Event parameter of double type
                  const string &sparam)    // Event parameter of string type
  {
//---
   ResetLastError();
   RefreshRates();
   //--
   static bool keyPressed=false;

//--- this is an Event of a keystroke
   if(id==CHARTEVENT_KEYDOWN)
     {
      if(lparam==KEY_PLUS || lparam==KEY_MINUS)
        {
         if(!keyPressed) keyPressed=true;
         else keyPressed=false;
        }
      //--
      switch(int(lparam)) 
        { 
          case KEY_PLUS:  
                 {
                    .... etc
                 }
               break;

          case KEY_MINUS: 
                 {
                    .... etc
                 }
               break;
        }

     }
  }
//--------------------------------------------//








To add comments, please log in or register