Put Ctrl+B in the Sell input and Hotkeys won’t take it. It answers with the fix in the same breath:
Sell: Ctrl+B is a terminal shortcut, use Ctrl+Shift+B or Shift+BPlain Ctrl shortcuts belong to the terminal, and a key bound on top of one is either dead or does two jobs at once. Hotkeys is the thing saying no here, a free include file from the MQL5 CodeBase for MetaTrader 5 and MetaTrader 4. You bind your own actions, any int you like, to shortcuts written as text, like Shift+B, straight from your inputs. OnChartEvent gets back the action that was pressed, ready for a switch. A shortcut that can’t work is refused with its reason before your program even starts.
No other includes, no separate MT4 build
Hotkeys.mqh pulls in no other file, and the MetaTrader 5 and MetaTrader 4 CodeBase entries ship the same one. Same API, same refusals, same messages on both terminals. It goes in MQL5\Include\Hotkeys\, or MQL4\Include\Hotkeys\ on MT4, and the zip already keeps it in that layout. The example expert, HotkeysExample, sits at the root of the zip and goes in your Experts directory. Its first line that matters:
#include <Hotkeys\Hotkeys.mqh>Its shortcuts are plain string inputs. A user who can’t stand Shift+H changes it in the inputs dialog, and nobody pesters you for a rebuild over one goddamn letter:
input string InpLineKey = "Shift+H"; // Draw a line at the bid
input string InpClearKey = "Shift+D"; // Delete the lines
input string InpGridKey = "Shift+G"; // Show or hide the grid
input string InpQuoteKey = "Shift+Q"; // Print bid, ask and spreadAt start it binds each input to an action from its own enum and quits with the reason if any of them is refused:
if(g_keys.Bind(EXAMPLE_ACTION_LINE, "Line", InpLineKey, error) != HOTKEYS_OK ||
g_keys.Bind(EXAMPLE_ACTION_CLEAR, "Clear", InpClearKey, error) != HOTKEYS_OK ||
g_keys.Bind(EXAMPLE_ACTION_GRID, "Grid", InpGridKey, error) != HOTKEYS_OK ||
g_keys.Bind(EXAMPLE_ACTION_QUOTE, "Quote", InpQuoteKey, error) != HOTKEYS_OK)
{
PrintFormat("not started reason=\"%s\"", error);
return false;
}After that, every chart event goes through one call:
int action = g_keys.ChartEvent(id, lparam, dparam, sparam);
if(action == HOTKEYS_NO_ACTION)
{
return;
}No Print calls
Hotkeys writes nothing to the Experts log. I rip out any library that prints into my log on its own, and I wasn’t going to ship one. A call that can fail returns HOTKEYS_OK or one of six HOTKEYS_ERROR_* codes and fills an error string. Where that string ends up is yours to pick: a print, an Alert, a line on your panel. The codes start above ERR_USER_ERROR_FIRST, in the range MetaTrader leaves for programs to define their own.
No F keys, no arrows, no Alt
A shortcut is a letter A to Z or a digit 0 to 9, on its own, with Shift, or with Ctrl+Shift. Keys the terminal handles itself come back as reserved:
"ESC", "ESCAPE", "ENTER", "RETURN", "SPACE", "TAB", "BACKSPACE", "DELETE", "DEL",
"INSERT", "INS", "HOME", "END", "PAGEUP", "PAGEDOWN", "PGUP", "PGDN", "UP",
"DOWN", "LEFT", "RIGHT", "PLUS", "MINUS", "F1", "F2", "F3", "F4",
"F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"Alt and the Windows key come back as not supported, because the terminal only reports Shift and Ctrl to a program. A library that skips this check binds Alt+B happily, and you learn the truth hammering the key on a live chart, wondering what the hell broke. Here the bind fails and says why. A single character that isn’t a letter or a digit, like ?, is refused too.
The example tries five bad shortcuts for a Sell action on a scratch map where Buy already owns Shift+B. This is what it prints at start:
bound action=Line key=Shift+H
bound action=Clear key=Shift+D
bound action=Grid key=Shift+G
bound action=Quote key=Shift+Q
refused text=Shift+B reason="Sell: Shift+B is already bound to Buy"
refused text=Ctrl+B reason="Sell: Ctrl+B is a terminal shortcut, use Ctrl+Shift+B or Shift+B"
refused text=Alt+B reason="Sell: Alt is not available, the terminal reports only Shift and Ctrl"
refused text=F5 reason="Sell: F5 is a terminal key, use A to Z or 0 to 9"
refused text=Shift+Shift+S reason="Sell: "Shift+Shift+S" names a modifier twice"
No sloppy shortcut text
The parser forgives case and spaces. ctrl + shift + 1 is the same as Ctrl+Shift+1, and Control works as well as Ctrl. That’s where the mercy runs out. An empty part, a word in the modifier slot that isn’t Shift or Ctrl, the same modifier twice, modifiers with no key after them, or a key name longer than one character all come back as syntax errors. The message quotes the user’s own spelling, and Bind puts the action’s name in front, so you can show it as it is. Your user reads what they typed wrong, in plain words, instead of a bare number they’d have to dig out of a crusty forum thread.
No key fires two actions
Buy has Shift+B, you try Shift+B on Sell, and Sell gets Shift+B is already bound to Buy. A failed bind leaves every binding exactly as it was. One fat-fingered input can’t leave the map half rebuilt and half screwed. Binding the same action again isn’t a conflict, it replaces that action’s shortcut, so a settings panel can rebind a key without unbinding first. Unbind(action) and Clear() take actions out.
Two more refusals at bind time: an empty action name gets an action needs a name, and a 65th action gets too many actions, the limit is 64. If one chart needs more than 64 shortcuts, the limit is the least of your problems.
If you already have key codes instead of text, BindKey(action, name, key, modifiers, error) takes the virtual key and the HOTKEYS_SHIFT and HOTKEYS_CTRL bits and runs the same checks.
No near misses on the modifiers
Shift+B fires on Shift+B. Not on a plain B, not on Ctrl+Shift+B. The match wants the key and the exact set of modifiers, so typing on the chart doesn’t open a trade by accident. A half-assed matcher that only looks at the letter would buy every time you typed a lowercase b.
ChartEvent reads Shift and Ctrl from the terminal at the moment of the key event, through TerminalInfoInteger(TERMINAL_KEYSTATE_SHIFT) and TERMINAL_KEYSTATE_CONTROL, and hands them to Match. Match(key, repeats, flags, modifiers) is public, which means a test can feed it a key and modifiers without a keyboard.
No machine-gun on a held key
Hold a key down and the terminal keeps sending key-down events for as long as your finger stays there. On a trade panel that turns one press held a second too long into a pile of crap buys. Match drops anything that isn’t a single fresh press:
if(!m_enabled || key <= 0 || repeats != HOTKEYS_SINGLE_PRESS)
{
return HOTKEYS_NO_ACTION;
}
if((StringToInteger(flags) & HOTKEYS_PREVIOUS_STATE_MASK) != 0)
{
return HOTKEYS_NO_ACTION;
}repeats is the event’s dparam, the repeat count. flags is its sparam, the key-down flags, where bit 30 means the key was already down. Either one says repeat, and the press is ignored. One press, one action.
A capital S typed into your program’s own text box is Shift+S as far as the terminal cares. While that box has the keyboard, SetEnabled(false) switches every shortcut off. SetEnabled(true) brings them back.
What it does let through
Nothing at all. An empty input or none is a valid shortcut. The action stays bound with no key, Count() still counts it, and Label(i) or LabelOf(action) prints none. A user who hates one of your shortcuts switches it off, and your expert still starts.
Hotkeys is a free download from the MQL5 CodeBase for MetaTrader 5 and MetaTrader 4.