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

video
play-sharp-fill

 

  1. Introduction to Recognizing Keystrokes in MQL5 (00:00 – 00:16)
    • Introduction to creating an expert advisor that recognizes keystrokes on a chart in MQL5, with a demonstration of the ‘Simple Key Event’ expert advisor.
  2. Demonstration of Keystroke Recognition (00:16 – 00:29)
    • Demonstrating how pressing keys ‘a’, ‘b’, and ‘c’ on the chart triggers a notification that a key was pressed.
  3. Opening MetaEditor and Creating a New Expert Advisor File (00:29 – 01:05)
    • Instructions on opening MetaEditor and creating a new expert advisor file named “Simple Key Event”.
  4. Setting Up the Code Structure for Keystroke Events (01:05 – 01:24)
    • Deleting unnecessary code and focusing on the ‘OnChartEvent’ function for handling keystroke events.
  5. Explaining the Parameters of the OnChartEvent Function (01:24 – 02:19)
    • Detailed explanation of the parameters of the ‘OnChartEvent’ function, including event ID, long parameter, double parameter, and string parameter.
  6. Implementing Keystroke Event Handling (02:19 – 03:29)
    • Coding to check if the event ID equals ‘CHART_EVENT_KEY_DOWN’ and using ‘TranslateKey’ to convert the key code into a letter.
  7. Creating a MessageBox Output for the Pressed Key (03:29 – 04:04)
    • Using a message box to display the text “The key was” followed by the converted value of the key that was pressed.
  8. Compiling the Code and Testing in MetaTrader (04:04 – 04:53)
    • Compiling the code in MetaEditor and instructions on testing the expert advisor in MetaTrader by dragging it onto a live chart.
  9. Demonstration of the Keystroke Recognition Expert Advisor (04:53 – 05:29)
    • Demonstrating the expert advisor in action on a MetaTrader chart, showing how it recognizes and displays pressed keys.
  10. Conclusion and Further Learning Opportunities (05:29 – 05:47)
  • Concluding remarks on the tutorial and suggestions for further learning through other videos or a premium course.

 

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.