A call to MessageBox() make subsequent calls to TerminalInfoInteger(TERMINAL_KEYSTATE_*) return incorrect result?

 

It looks like a call to MessageBox() breaks something in the ability of MetaTrader to detect key presses correctly.

Consider for example this sample EA code:

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
    if (id == CHARTEVENT_KEYDOWN)
    {
        Print("Key: ", lparam);
        Print("Shift: ", TerminalInfoInteger(TERMINAL_KEYSTATE_SHIFT));
        Print("Control: ", TerminalInfoInteger(TERMINAL_KEYSTATE_CONTROL));
        if (lparam == 9) // Tab key
        {
            MessageBox("test", "caption", MB_OKCANCEL | MB_ICONWARNING);
        }
    }
}

On a key down event, it will print the key code and will also print the states of the Shift and Ctrl keys. It works correctly, printing either -127 or-128 when Shift or Ctrl are down. On a TAB key press, it will call MessageBox(). After that, no matter what's the real state of Shift and Ctrl keys, TerminalInfoInteger(TERMINAL_KEYSTATE_*) will only return either 0 or 1.

Does anyone know a solution to this?

Re-attaching (or re-compiling) the EA works, but switching timeframe/symbol doesn't.

EDIT: The problem with TerminalInfoInteger(TERMINAL_KEYSTATE_*) persists after you close the message box window. This is not a complaint about not being able to detect Shift/Ctrl presses while a modal window is active.
 
Andriy Moraru: t looks like a call to MessageBox() breaks something in the ability of MetaTrader to detect key presses correctly.

Doesn't break anything. The message box is functioning; it is looking for a click or key press. When you click and it returns, there is nothing in the keyboard queue to trigger a key Down event.

 
William Roeder #:

Doesn't break anything. The message box is functioning; it is looking for a click or key press. When you click and it returns, there is nothing in the keyboard queue to trigger a key Down event.

Sorry, I wasn't being clear. The problem is that the Shift and Control keys aren't being detected even after you close the message box.
 
Andriy Moraru #: Sorry, I wasn't being clear. The problem is that the Shift and Control keys aren't being detected even after you close the message box.

This is just a suspicion, but have you considered that the Message Box is shifted focus away from the chart window, and that subsequent key events are being directed elsewhere and not the chart?

You may need to have focus brought back to the chart window by having the user select the chart window again.

 
Fernando Carreiro #:

This is just a suspicion, but have you considered that the Message Box is shifted focus away from the chart window, and that subsequent key events are being directed elsewhere and not the chart?

You may need to have focus brought back to the chart window by having the user select the chart window again.

If it is so, then clicking on the chart doesn't "restore" this focus.

 
Isn't MessageBox by definition a Modal Dialog?

Meaning, it blocks the application that's creating it and redirecting all input to that dialog?


 
Dominik Christian Egert #:
Isn't MessageBox by definition a Modal Dialog?

Meaning, it blocks the application that's creating it and redirecting all input to that dialog?


The problem is, the Shift/Ctrl keys aren't being detected even after you close the message box window.
 
Andriy Moraru #: The problem is, the Shift/Ctrl keys aren't being detected even after you close the message box window.

They were already detected, by the message box. Those events are gone. There is nothing in the keyboard queue to trigger a key Down event.

 
William Roeder #:

They were already detected, by the message box. Those events are gone. There is nothing in the keyboard queue to trigger a key Down event.

New Shift/Ctrl presses aren't detected after the message box is closed. I am not talking about presses during the message box's life.
 

I have recorded a video demonstrating the issue at hand. It goes through the following steps:

  1. Adding the test EA to the chart.
  2. Pressing Shift, Ctrl, then both keys. Note the correct return values printed for TerminalInfoInteger().
  3. Pressing Tab to call MessageBox().
  4. Closing the message box.
  5. Pressing Shift, Ctrl again. Note the incorrect return values printed for TerminalInfoInteger().
  6. Removing the EA.
  7. Attaching the EA again.
  8. Pressing Shift and Ctrl keys again - correct values are printed.


 
Some more testing showed that the bug is present in both Build 1353 and Build 1355, while it is absent in MT5/MQL5.