 Hello and welcome to another lecture in the course on computer programming. I am Dr. Aviram Ranade and I am the guest lecturer today. I will be talking about advanced graphics events. Let me quickly do a recap. In the last session, we saw the get click function for waiting for clicks. Today, we are going to see how to wait for other kinds of events. The reference for this lecture is Chapter 20 of an Introduction to Programming through C++ written by me and published by McGraw Hill education earlier this year. The term event in simple CPP is used to denote a number of different things. For example, pressing of a button of the mouse is an event. Releasing of a button of the mouse is also an event. So when you press, it becomes one event and then when you release, it becomes another event. So we think of clicks as an event. So far, we have been thinking of clicks as an event. But now we can actually separate things into pressing a button and releasing a button. After pressing a button, you can move the mouse as you are familiar with, I am sure. And this operation of moving the mouse is called dragging. So dragging a mouse is also an event. If you are dragging for a long time, then it will generate several events. Essentially, a dragging event is generated every 30 milliseconds or something like that, some short amount of time. So when you drag, you will generate several drag events. Pressing a key on the keyboard is also an event. A C++ program can choose to wait for all of these events. Once such event happens, then the program resumes and then the program can find out what event has happened and accordingly take action. Instead of waiting for an event, a C++ program can also merely query has any event happened. If an event has indeed happened, then the program can again find out what event happened and accordingly perform operations. Notice that checking does not involve any waiting whatsoever. It is really asking at the point at which I am checking has some event already happened. So the event must have really happened before I do the checking. Now, if you have several windows on the screen, then you may be interacting with different windows. So if you press a mouse button, then that event may go to one of those windows. You do not know which one. If you click on a certain window, then that window is set to get the focus and subsequent mouse events or subsequent keyboard events as you know will be sent to the program running in that window. So when we talk about events like the ones listed here, we are assuming that your canvas has the focus. In simple CPP, events are represented by objects. So we have objects of class X event which is built in and they are used for holding information about events. You do not really need to know all the details of the class X event. You can look at some of its data members which we will see shortly and you have functions available by which you can extract information out of an X event object. So let us now look at how do we wait for an event. For this simple CPP provides you the function next event. Next event takes as argument a reference to an X event object. Here is a typical use. We have created an instance of an X event object called event and then we pass that event to our function next event. This call causes the program to wait for an event to happen. Indeed calling next event is like waiting for the user to type in data. So if you execute C in greater than greater than something, then just as your program waits, when you execute next event also your program waits until any of our specified events happens. When an event finally happens, the information about that event is put by simple CPP into the event object passed to next event. In this case, that would be the event object E1. After this the execution continues. So when the call next event even finishes, even will contain information about what event has happened. Now you can get the information about what event has happened by executing functions on that event object. For example, if you call mouse button press event on our event E1 and by the way the event even is to be passed by reference. So you do not have to do anything, but this is just to let you know that we do not really ever copy these event objects, but we just pass them by reference. So that event object when operated upon the mouse button press event function call will reveal whether that event is in fact a mouse button press. If it is indeed a mouse button press then this function will return true. So this function returns true if even involves some mouse button being pressed. Likewise you have a function mouse button release event. It also takes as argument an event and this will return true if even involves some mouse button being released. You also have a mouse drag event function. Again it takes as an argument an event object and this returns true if even involves the mouse being dragged. You also have a function key press event which also takes as argument an event object and it also returns true if and only if this event E1 involves some key being pressed. Of course if you know that a certain event involves a key being pressed you would like to get more information. Similarly if you know that a mouse is being dragged you might want to get more information say where is the mouse when it is being dragged. So how to do all this we will describe next. So to get more information about an event E1 you are allowed to access the data members of event E1 some of the data members. For example you can write even.xbutton.button. So this is a data member and this equals one or two or three depending upon which button was pressed or released. So if the left button was pressed or released then even.xbutton.button would equal one. If the middle button is pressed and then released then that would or released then it would equal two and three for the right button. You can also get the coordinate where the pressing or releasing happen. For this you just look at the member even.xbutton.x. So this member tells you the coordinate. Even.xbutton.y likewise tells you the y coordinate of where even happened. If you are dragging you need to use x motion.x. Actually you can also use xbutton.y but I guess the more official thing is x motion.x. This will equal the x coordinate of where the dragging happened in case even was a mouse track event. Similarly even.xmotion.y equals the y coordinate of where the dragging happened in case even was a mouse track event. So I have written in this example even.xbutton.button or even.xmotion.x. Obviously if you are dealing with an event E2 or some event of some other name then you would have to write name.xbutton.button and so on. You are also provided the function care from event which takes as argument and event E1 and returns a character. The character returns the character return is the as key value of the key pressed. So if you press the key A then this would return the as key value of that key. Of course for this even had better be a key pressed event and you should check whether it was a key pressed event by calling the functions listed in the previous slide. The following data members can also be used even.xkey.x equals the x coordinate of the cursor position when even happened. Even.xkey.y equals the y coordinate of the cursor position when even happened. And again here it is assumed that even is of type key pressed event. Here is an important note key pressed events may not be detected properly by your program if the caps lock key or the pneum lock mode rather is on. So if you have pressed the caps lock key then the caps lock mode becomes on or if you have pressed the pneum lock key if you have it on your keyboard then that mode becomes on. So you need to press these keys again so that these modes are not active. So you have to remove these modes you cannot have the caps lock mode on or the pneum lock mode on if you want to run programs which use the care from event function. We have said that you can wait for events but you can also check for events without waiting. The function check event allows you to do this. It takes as argument like next event it takes an argument as an argument. This function takes as argument reference to an x event object. The function returns true if an event has happened after the last call to next event but an event which has not yet been reported in any check event call. So this will return true only if an event has happened after the last call to x event but if an event has happened and it was already reported in check event then this will return false. So the same event will not be reported again and again if you execute consecutive check events, check event calls. Here is a typical use. You declare an event x event object E1 then you call check event with that object as an argument. The function returns true if an event happened and in that case the code named A gets executed otherwise the code named B gets executed. Notice that in this call there is no waiting. Immediately the function returns and either the code named A or the code named B gets executed. What event has happened? That information can be obtained as before by looking at the data members of the event or executing appropriate calls on that event. So now I am going to show you an example program. This is a very simple program but I will remark later how you can build upon it. So we are going to write a program that allows you to draw on the canvas in a very simple manner. So you take your cursor to the position where you want to start drawing a line and you press a mouse button. Then you drag the mouse in the path along which you want the line to be drawn. When you want to stop the line drawing you release the mouse button. That is it. After that you can again move your mouse to another position and draw another line and so on. In other words if you merely move the mouse without pressing any button no line is drawn. To stop the program you just have to press the escape key. So now I will show you the demo. So here is the program. First we are going to create the canvas and here we are using the three argument version. So in this we are supplying a title for the canvas. The title says drawing using the mouse. We are getting a canvas of width 800 and height 500. Now to denote the escape key I have created a constant character which contains the as key value of the escape key. Then we need an event object. So in this case I have given it the name event and this statement will create it. Last x and last y are going to be the coordinates of the last point from which you need a line to be drawn. So we will see how they get used. Now there is this infinite loop and in this loop we merely wait for events to happen. So this is the call which waits for an event to happen. Next we check has a mouse button been pressed. If a mouse button has been pressed that means the user will start a line drawing. Now our variable last x is set to event.xbutton.x and last y is set to event.xbutton.y because once the mouse is dragged we want a line to be drawn from this position last x last y from the current mouse position. If on the other hand a mouse drag event happened then that means earlier a mouse pressed event must have happened. In which case we want a line drawn from the previous mouse position to the current mouse position. The previous mouse position is given by last x last y that is what we saved over here and then the current mouse position is given by event.xbutton.x and event.xbutton.y and then we again save the mouse position, the current mouse position to draw the next line in our variables last x and last y. Finally we check whether a key press event has happened. So this is done by the call key press event of event and if this has happened then we want to know whether that key happens to be the escape key. If it is then the user is signaling us that the program should stop and so we break and after breaking there is nothing else so the program will terminate. So let us run this program. So this is our canvas and we can start drawing. For example I am going to draw a hill maybe another hill maybe I will draw the sun inside this maybe I will put some rays pardon me for my sloppy drawing but that is okay. I can maybe even write a title here maybe I can write a title here say sun okay and then I am done I am tired of drawing so I am going to hit escape okay so that is it. So that is the end of the demo and let us get back to the slides okay. So basically what did we do in this lecture? So we discussed how to handle mouse and keyboard events just to remind you next event enables waiting for mouse and keyboard events check event enables whether a mouse or keyboard event has already happened. Information about events can be obtained by calling functions on the event object or examining its members by that I mean examining its data members. Chapter 20 of the book gives an example of a snake game that can be developed. You should look at it and you will see that you should be able to draw you should be able to develop many fun games. Here is another suggestion take the drawing program that we developed and extended to recognize what is drawn. For example is the user writing something we wrote the name SUN your program should be able to look at the coordinates of the lines drawn so for this you should take the coordinates and store them in a vector and then examine them and see whether they look like the letter s the letter u and things like that. So this will allow you to do some ambitious I mean you will have to think about this a little and maybe you should be able to write a program at least a preliminary program which will recognize whether what you are writing is a letter or a number or things like that or you may want to recognize things like well I drew the sun so maybe if I had drawn a full circle maybe you should print a nice circle there rather than the jagged circle that I had. In any case I hope you see that there are many many possibilities for doing interesting projects. Thank you.