trigger a key press event?

 

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?

 
Do you know that you can click and drag the date/time axis. Right to zoom in, left to zoom out.
 

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
[Deleted]  
OnChartEvent() function  with CHARTEVENT_KEYDOWN is to survey the keyboard.
 
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.

 
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...
 


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.

 
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.)
 
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;
        }

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








[Deleted]  
static datetime lastctrl=0;
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
{
   if(id==CHARTEVENT_KEYDOWN)
   {
      Print("lparam:"+lparam+" dparam:"+dparam+" sparam:"+sparam);
      if(lparam==17)
         lastctrl=TimeCurrent();
      if(TimeCurrent()-lastctrl<2)
      {
         if (lparam == 49)
            OpenBuy();
         if (lparam == 51)
            OpenSell();
         if (lparam == 48)
            CloseAll();
      }
   }
}
 

a bit late my contributon :) 

First, you don't need to trigger anything. The chart's zoom factor can be set like this: 

ChartSetInteger(0, CHART_SCALE, zoom); // on MT4 zoom is from 0 to 5

Second. the state of the Control or Shift keys can be accessed through TerminalInfoInteger()
See https://docs.mql4.com/constants/environment_state/terminalstatus

To read currently pressed keyboard keys, use something like this: 

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 
                  {
  if(id==CHARTEVENT_KEYDOWN) {
    short sym = TranslateKey((int)lparam);
    if(sym > 0) {
      string currentKey = ShortToString(sym);
      Print("currentKey:  ", currentKey);       
    }
  }
}

HTH 

TerminalInfoInteger - Checkup - MQL4 Reference
TerminalInfoInteger - Checkup - MQL4 Reference
  • docs.mql4.com
TerminalInfoInteger - Checkup - MQL4 Reference