MQL5 TUTORIAL BASICS – 38 HOW TO CATCH A SIMPLE KEY EVENT

video

In this video we are going to create a little expert advisor that is able to recognize keystrokes, in this case, we have a simple chart here, this is the expert advisor that we have coded, it is called simple key event and when I click into the chart and press any button I will get the notice that a key was pressed.
I have pressed the key a, now I have pressed the key b and this time the key was c, so let’s find out how to code that in mql5.
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 file, expert advisor from template, continue, I will call this file simple key event, click on continue, continue and finish.
Now you can delete everything above the on tick function and let’s also remove the two comment lines here.
So usually we are using the on tick event, this is the event that is triggered whenever the price changes on the chart but in this case, we need another kind of event because we want to find out if a key was pressed and to do that we use the on chart event.
It takes a few parameters, we have already done that for the mouse click but let’s repeat the parameters.
The first one is an integer value, it’s called event id and it will return the id of the event, the second one is a long parameter, this symbol here is not a mistake it’s how we can pass a value by reference in mql5, it’s a long value so we call it lparam, the next one is a double parameter, also passed as a reference so it’s called dparam, and the last parameter is a string parameter, this is also a reference and this one is called sparam.
Depending on what you do you need to find out what each of those parameters contains, in our case it’s easy because we only need the event id to identify if we have a keystroke and that is the case when the event id equals, it’s called chart event underscore key down – all in capital letters – now we want to translate the key code – that’s a number into a letter – that is done by using translate key for the value that’s inside of the lparam, it is an int value, that’s a little bit strange because lparam is usually a long parameter but actually key codes only have integer values and now we want to create an output for the key that was pressed, we use a message box, it will output the text: the key was, followed by the converted value for the key that was pressed, this is the heading and our message box will have an ok button just for confirmation, you could also use other buttons like yes or no and cancel, in our case the ok button is enough.
Finally, we have to add the two closing brackets here and that’s about it.
If this was too fast for you or if you don’t understand what all the code here does maybe you want to watch the other videos in this video series or maybe even the premium course is interesting for you but for now please click on the compile button…
I have messed up something here…
Actually, I missed the curly bracket here, so let’s re-compile the code and this time it works, so let’s find out if it does what we want.
Please click on the little button here or press F4 to go back to Metatrader, let’s pick an empty chart, drag the newly created expert advisor onto that chart and press a key and now here is our message box, the key that was pressed is k, let’s press the b key and as you see it works as designed, while it will not be able to recognize every possible combination, for example, if I press the shift key I will get an empty return code, it’s also possible to do that but in this little video you have learned how to recognize a pressed key on your chart and you have coded it yourself with a few lines of mql5 code.