MQL4 TUTORIAL BASICS – 38 HOW TO CATCH A KEY EVENT

video

In this video, we want to find out how to catch a key event with mql4.
A key event is whenever you press a key on your keyboard that is a so-called chart event, so this is the chart, you don’t see very much right now but when I press the key a I will get a message box that says a key was pressed and the key was a. So let’s confirm that with okay.
Now I will press the b key and here is the next message box, the key that was pressed is key b and now we want to find out how to code that in mql4.
To do that please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor and here you want to click on file, new, expert advisor from template, continue, I will call this file simple key event, click on continue, continue and finish, now we can delete everything that is above the on tick function and let’s also remove the two comment lines here.
So most of the time we would use the on tick function because this is the function that is automatically called whenever the price changes on the chart but in our case, we are looking for a key that is pressed, so we need another event. This one is called on chart event and we need to pass a few parameters, we already have done that before for the mouse click event but let’s repeat the parameters.
The first one is an integer value, it’s called event id and it will give us the event that happened, in our case we are looking for a key that was pressed. An event could be something like an object that was created or a mouse-click.
The second parameter is a long value and this is no mistake, it’s the way to pass a value as a reference and because it’s a long value its called lparam, actually we will see that what we need is not a long value but it is passed in the lparam.
The next parameter is a double value so it’s called dparam and the last one also passed as a reference is called sparam because it’s a string.
That’s it for the parameters, so let’s find out what we can do.
We want to check if the event id equals chart event underscore key down – all in capital letters – and if that is the case we use the function translate key to get the value of the lparam, we convert it to an integer value here and we also want to create a message box for the output, that is done with the message box function that comes with mql4.
We want to output the text: key was, followed by the converted key code, this is the heading and here we use an ok button to confirm the message box, it is actually able to handle a lot of flags, you could use something like a yes/no and a cancel button, these are all the return codes, so you can actually check which of the contained buttons was pressed, we don’t need that for this simple example, please don’t forget the closing brackets here and now you can click on the compile button, that should work without any errors but if you don’t understand what all the code here does maybe you want to watch the other videos in this basic video series or maybe even the premium course could be interesting for you.
In my case the compilation process was successful, so now I can click on a little button here or press F4 to go back to Metatrader.
This is an empty chart window and now I simply drag the expert advisor from the navigator panel onto the chart, click on ok, now you should see that the simple key event is enabled here, so let’s press the m key and now the message box comes up and says that a key was pressed, the key was m and in this little video you have learned how to recognize key codes with mql4 and you have coded it yourself with a few lines of mql4 code.