Keyboard Shortcut Router
A panel or EA that wants keyboard shortcuts usually ends up with one long if/else ladder inside OnChartEvent, and every new mode (an edit box focused, a confirmation dialog open) means bolting another condition onto the front of every existing check to make sure the shortcut still means the right thing. This library separates "what key was pressed" from "what should it do right now" by scoping bindings to named contexts: the same key can be bound to a different action in "default" and in "editing", the router tracks which context is active via a small push/pop stack, and Route() looks up the active context first, falling back to "default" only when the active context has no binding of its own for that key. MQL4 has no function pointers, so Route() cannot call a handler directly - it returns the bound action as a plain string, and the host program switches on that name. This is the same callback-by-string idiom already used across this catalogue wherever a widget needs to hand an event back to its owner without a real callback mechanism to lean on. Modifier state comes from CHARTEVENT_KEYDOWN's own sparam, which MQL4 reports as a small bit flag string (bit 0 = SHIFT, bit 1 = CTRL) - ALT is not reliably delivered this way by the platform and is not modelled here. Notes and limitations - ALT is not modelled - MQL4's CHARTEVENT_KEYDOWN modifier flags only reliably cover SHIFT and CTRL. - Lookup is a linear scan over registered bindings. Fine for the dozens of shortcuts a real panel realistically needs; not built for hundreds. - Fallback only goes one level deep (active context -> "default"). There is no arbitrary context-inheritance chain. - No money is at risk. This library only interprets keyboard events; it never sends an order itself.
Script · MT4 · v1.0 · Free
About Keyboard Shortcut Router
Keyboard Shortcut Router is a script for MetaTrader 4. MQL source code included.
Files in the download: KeyboardShortcutRouter.mqh, KeyboardShortcutRouter.txt, KeyboardShortcutRouterDemo.mq4.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code