In MQL5, how can I detect keypresses in MT5 running on a Mac
In MQL5, how can I detect keypresses in MT5 running on a Mac
Loading saved threads...
Jeronymite · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Jeronymite
Original post: https://stackoverflow.com/questions/76874463
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
When running MT5 on a PC, I can easily detect specific keys when pressed with this MQL5:
#import "user32.dll"
int GetKeyState(int nVirtKey);
#import
// --- Windows Key Codes ---
#define VK_SHIFT 0x10 // SHIFT key
#define VK_CONTROL 0x11 // CTRL key
#define VK_ALT 0x12 // ALT key
#define VK_ESCAPE 0x1B // ESC key
bool IsShift()
{
return(IsKeyDown(VK_SHIFT));
}
bool IsCtrl()
{
return(IsKeyDown(VK_CONTROL));
}
bool IsAlt()
{
return(IsKeyDown(VK_ALT));
}
bool IsEscape()
{
return(IsKeyDown(VK_ESCAPE));
}
bool IsKeyDown(int nVirtKey)
{
return(GetKeyState(nVirtKey) > 1000);
}
// Run this crude diagnostic method every 1 second and hold down any key to see its key code
void GetKeyCodeOfKeyPressed() // Display the key code and the key state
{
for (int k = 0; k < 10000; k++)
if (IsKeyDown(k))
Print("That's it! ",k," [",GetKeyState(k),"]");
}
The code above includes methods specifically trying to detect the PC keyboard modifier keys with individual checks, and the Escape key, as well as a more broad-based test to discover the key code for any pressed key in an attempt to see if it could discover the key codes on a Mac.
Running MT5 on a PC, the code above works exactly as one would expect. Running it on a Mac, the code does not detect any keypresses when a key is pressed. None. It's as if even GetKeyState is completely irrelevant on the Mac.
In MQL5 with MT5 running on a Mac, how can one detect specific keys pressed? If it is simpler to do this for a subset of keys, it is specifically the modifiers (or Mac equivalents) and Escape that are most relevant. The modifier keys will be used with mouse clicks. Thanks.
Quote
Report
Dale Woods · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Dale Woods
Original post: https://stackoverflow.com/a/76962244
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
You don't need to use those winapi functions
Look at for example
TerminalInfoInteger(TERMINAL_KEYSTATE_CONTROL)
https://www.mql5.com/en/docs/constants/environment_state/terminalstatus
and
void OnChartEvent(const int id,const long& lparam,const double& dparam,const string& sparam)
{
if(id==CHARTEVENT_KEYDOWN)
{
short sym=TranslateKey((int)lparam);
}
}
https://www.mql5.com/en/docs/common/translatekey
Quote
Report
Post Reply
Checking account access…