<p>Is the mouse over a component? Was a mouse button pressed? Which button was it? Did the user release a button, which one? Exactly where inside the form is the mouse? Did the user move the mouse out of the form and into another?</p><p>Delphi (as Windows) constantly monitors what the user is doing with the mouse. Naturally, Delphi can detect any mouse action. When a Delphi application detects a mouse action, it calls whatever event handler we&#39;ve defined for the corresponding event.</p><h3>Responding to the mouse</h3>There are four kinds of mouse actions you can respond to in your applications. Three of these are uniquely mouse actions: mouse-button down, mouse moved, and mouse-button up. The fourth kind of action, a click (a complete press-and-release, all in one place) is a bit different, as it can also be generated by some kinds of keystrokes (such as pressing Enter in a modal dialog box).<p>Components recognize a mouse event only when the mouse pointer is inside the component. However, if a mouse button is pressed and held while the mouse pointer is inside a component or form, that object captures the mouse. This means that no other Delphi object can react to mouse events until the user releases the mouse button, regardless of where the user moves the mouse.</p><p>There are three fundamental mouse event handlers: OnMouseDown, OnMouseMove, and OnMouseUp. The MouseUp and MouseDown event handlers take the same five parameters as follows (the MouseMove lacks a Button parameter):</p><ul><li> <b>Sender</b> - The object that detected the mouse action.</li><li> <b>Button</b> - Indicates which mouse button was involved: mbLeft, mbMiddle, or mbRight.</li><li> <b>Shift</b> - Indicates the state of the Alt, Ctrl, and Shift keys and the state of the mouse buttons when the event occurred.</li><li> <b>X, Y</b> - The pixel coordinates of the mouse pointer in the client area of the Sender.</li></ul><h3>OnMouseDown/OnMouseUp</h3>Whenever the user presses a button on the mouse, an OnMouseDown event goes to the object the pointer is over. To see the event handler at work, assign the following code to the OnMouseDown procedure: <pre><code> <b>procedure</b> TForm1.FormMouseDown(Sender: TObject;\n Button: TMouseButton; Shift: TShiftState;\n X, Y: Integer) ;\n <b>begin</b>\n Canvas.Ellipse(x-20,y-20,x&#43;20,y&#43;20) ;\n <b>end</b>;\n </code></pre> When the application runs, you can press the mouse button with the mouse cursor on the form and have the circle appear at the point clicked. This simple event uses the X and Y parameters sent to the method, and calls the Ellipse method of the canvas to display the circle.<p>Let&#39;s spice this code a bit. Suppose that we want red colored circles when the user presses the left mouse button, and blue colored rectangles when right button is pressed. The Button argument has three predefined constants to determine which button was pressed: mbLeft, mbRight, mbMiddle:</p><pre><code> <b>procedure</b> TForm1.FormMouseDown(Sender: TObject;\n Button: TMouseButton; Shift: TShiftState;\n X, Y: Integer) ;\n <b>begin</b>\n <b>case</b> Button <b>of</b>\n mbLeft:\n <b>begin</b>\n Canvas.Brush.Color :&#61; clRed;\n Canvas.Ellipse(x-20,y-20,x&#43;20,y&#43;20) ;\n <b>end</b>;\n mbRight:\n <b>begin</b>\n Canvas.Brush.Color :&#61; clBlue;\n Canvas.Rectangle(x-20,y-20,x&#43;20,y&#43;20) ;\n <b>end</b>;\n <b>end</b>;\n <b>end</b>;\n </code></pre> As for the OnMouseUp event: use the OnMouseUp event handler to implement special processing when the user releases a mouse button.<h3>OnMouseMove</h3>OnMouseMove occurs when the user moves the mouse pointer while the mouse pointer is over a control. Since the OnMouseMove event handler will be called relatively frequently, any code inside this event handler will be executed often.<p>As we know, we can combine the keyboard with the mouse. For example, we can have the Shift-Move combination draw a circle. Let&#39;s see MouseMove in action (new project/new form):</p><pre><code> <b>procedure</b> TForm1.FormMouseMove(Sender: TObject;\n Shift: TShiftState; X,Y: Integer) ;\n <b>begin</b>\n <b>if</b> ssShift <b>in</b> Shift <b>then</b>\n Canvas.Ellipse(x-20,y-20,x&#43;20,y&#43;20)\n <b>else</b> <b>if</b> ssCtrl <b>in</b> Shift <b>then</b>\n Canvas.Rectangle(x-20,y-20,x&#43;20,y&#43;20) ;\n <b>end</b>;\n </code></pre> With this code, moving the mouse over the form causes circles to follow the mouse if Shift key was pressed during move, and rectangles if Ctrl key was pressed<h3>There is more</h3>In the following <a href="https://www.thoughtco.com/advanced-mouse-processing-1058399" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="1">article</a> I&#39;ll show you how to include some special API calls to your programs, and take more advanced control over the Mouse. Third <a href="https://www.thoughtco.com/understanding-drag-and-drop-operations-1058386" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="2">article</a> will show how to include drag and drop capabilities to a Delphi application.