Originally published in FoxTALK February 1998

As the Mouse Moves

Jim Booth

If your application needs to keep track of mouse actions you can either call an exterminator or use the events that Visual FoxPro gives us to track the mouse and its buttons.  Since my users get upset when that guy with the metal suit starts smashing their mice, I prefer to use the events in Visual FoxPro.

There are a number of events that can tell us about mouse activities.  MouseMove, MouseDown, MouseUp, Click, RightClick, DoubleClick, MiddleClick, and the MouseWheel events are the ones we are interested in.

Letís look at the MouseMove first.  The help file says that this event fires whenever the mouse is moved over a control.  This is correct and because it fires continuously while the mouse is moving over the control it can be a problem to performance if it is not used properly.  Remember that when you are writing code in the MouseMove that the code will run frequently and therefore it should be short and to the point.

The MouseMove event receives four (4) parameters.  These are nButton, nShift, nXCoord, and nYCoord respectively.  These parameters tell us about the position of the mouse pointer, the status of the mouse buttons, and the status of the shift, ctrl and alt keys.  Their values are as follows;

The nButton parameter has a value between 0 and 7 and it tells us the status of the mouse buttons.  The value 0 means no buttons are down while values of 1, 2, and 4 each mean that the left, right, and middle buttons are down respectively.  The other values (3,5,6,7) tell us about combinations of the buttons being down.  The nButton parameter has a value that is the sum of the values for the buttons that are down, so if the left and right buttons are down then nButton would equal 3, or the sum of 1 (left button) and 2 (right button).

The nShift parameter tells us similar information about the Shift, CTRL, and Alt keys.  The values for this parameter are handled similarly to the nButton parameter in that Shift is 1, CTRL is 2, and Alt is 4.  No key is 0 and if more than one of these keys is down then nShift is the sum of those key values.

The nXCoord and nYCoord parameters tell us the horizontal and vertical position of the mouse respectively.  The values of these parameters are in respect to the form and not the control, so when the mouse enters at the top of the control the nYCoord is NOT 0 or 1 but is the pixel offset from the top of the form itself.

The sample form Mice.scx shows an example of using the MouseMove event of a command button to update some other controls.  The other controls are showing us the state of things as the mouse moves over the command button.  An excerpt from the command buttonís MouseMove code is listed below, this excerpt is the code that determines if the mouse pointer is just entering, just leaving, or is moving over the command button.  It does this by checking the nXCoord of the mouse pointer to see if it falls within a 3-pixel border at the top, bottom, left, or right edge of the button.  If the pointer is in this perimeter area of the button then it checks to see if the mouse was previously over the button or not (in the sample we are using the current value of the option button group, but in real code this could be handled with a property of the button itself) and then set the entering or exiting value of the OptionButton group in the form to reflect this.  There is also code in the formís MouseMove that sets the option groupís value to 0 indicating that the mouse is not over the command button at all.

LPARAMETERS nButton, nShift, nXCoord, nYCoord


       IF ((nXCoord >= THIS.Left AND nXCoord <= THIS.Left+3) OR ;

          (nXCoord <= THIS.Left + THIS.Width AND ;

           nXCoord >= THIS.Left + THIS.Width-3)) OR ;

          ((nYCoord >= THIS.Top AND nYCoord <= THIS.Top+3) OR ;

          (nYCoord <= THIS.Top + THIS.Height AND ;

           nYCoord >= THIS.Top + THIS.Height-3))

                 DO CASE

                     CASE .optEntering.Value = 0

                            * We are entering

                            .optEntering.Value = 1

                     CASE .optEntering.Value = 2

                            * We are leaving

                            .optEntering.Value = 3



* We are over the control

              .optEntering.Value = 2


* Other code is here




The example form is available to FoxTALK subscribers from the subscriber download area at  The sample form shows reading the status of the mouse buttons, the shift/ctrl/alt keys, and the x and y coordinates of the mouse as well as the above handling of entering and leaving the control.  If you are not a subscriber to FoxTALK you can order a subscription at the same site.