HOME - - - - - Delphi Tutorials TOC - - - - - - Other material for programmers

Watching the mouse with Delphi

This has good information, and a search button at the bottom of the page.

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

Click here if you want to know more about the source and format of these pages
The comments in this were tested using Delphi version 2.

This "How To..." relates to something you probably won't "need" in a hurry... but if you want what it covers, there's no reason not to have it. Delphi and Windows have made getting it easy.

With the techniques explained here, you can know the position of the mouse pointer on the screen whenever you need to know. You can also see whether the shift, ctrl or alt buttons are down, and check the states of the mouse buttons. Note that if you want your application to respond to the pressing of a mouse button, you are better off using the OnClick event handler. The button and key sensing provisions mentioned here are primarily to simplify making your application respond differently, depending on the state of the buttons and keys when the mouse pointer moves. For example, the mouse pointer might be used to read temperatures from a graph on the screen. If no button or key were pressed, the information could be in degrees Celsius. If the mouse's left button were held down, the information could be displayed in degrees Fahrenheit. Anyway... done to "How To Watch the Mouse"...

Start a new application. Put a panel on the main form. Put two labels on the form, not on the panel; call them laMseX and laMseY.

Just to get things started, make the panel's OnMouseMove event handler...
laMseX.caption:='left/ right of mouse pointer';
laMseY.caption:='up/ down of mouse pointer';
When you run the application, as soon as the mouse pointer moves over any part of the panel, the captions on the labels should change. By the way: Constantly watching the mouse pointer's position doesn't seem to impose much strain on the system. Some sort of optimization has been built in, either by Microsoft of Delphi. The bad news is that if you move the mouse rapidly out of the panel, the last noticed position of the pointer may not be the column or row of pixels at the extreme edge of the panel. Not a problem, in practice, for many uses of the routines, but something you need to know.

It isn't just panels which have an OnMouseMove event; many components have one.

Take a close look at the line that Delphi generated when you asked for an OnMouseMove event handler shell, the line starting "procedure TForm1.Panel1MouseMove(Sender..". For many such lines, the parameters are simply (Sender:TObject) and that's it. Not so with OnMouseMove.

When the event occurs, it passes several things to the handler, including, hurrah, the mouse's position. Change the code you already have, to make it...
... and run it and marvel!

The variables X and Y will have been filled by the system. As you move your mouse pointer (within the area of the panel), you should see the coordinates being displayed.

That's all there is to the basics of what you're most likely to want. Is Delphi nice to work with?

The other bits...

Y oh y? (Why oh why?)

You may have noticed that the Y value is relative to the top of the panel, contrary to the x/ y systems we are used to in every other context.

Change the laMseY.caption line to...
... to "flip" things around. You can dispense with the minus 1 unless you really, really need the minimum value to be zero.

Shift, alt, ctrl, left click, right click, ...

Add another label to the form, laShiftState

Add the following to your OnMouseMove handler
if (ssShift in Shift) then laShiftState.caption:='Shift is down';
... and now if the mouse pointer is moved, over the panel, while the keyboard's shift key is down, laShiftState should say so. Not that the program won't "notice" the shift key at the moment it is depressed... or that it has been released, for that matter. There are other ways to look for the actual moment of pressing / releasing. However, within the OnMouseMove event handler, there is a way to check the state of the shift (and alt, and ctrl) keys, so that if you want the application to behave differently when you move the mouse, it is easy to build that into your code. As a simple example...
if (ssShift in Shift) then begin
     laShiftState.caption:='Shift is down';
     end// no ; here
    else begin
     laShiftState.caption:='Shift is up';
This little bit of code is just to give you the idea. It doesn't "work" very well for use in a "real" application because if you have the shift key down when you move the mouse pointer out of the panel, and subsequently release the shift key, the on-screen message doesn't change to reflect the new reality. But that doesn't mean that there are no good uses for the "Shift" information! Just be sure to use it intelligently.

To see almost the same thing happening, but this time when the mouse's left button is held down, add a label called laLeftState to your form, and the following code just after the code presented above.
if (ssLeft in Shift) then begin
     laLeftState.caption:='Left is down';
     end// no ; here
    else begin
     laLeftState.caption:='Left is up';
You may have noticed the slightly unusual condition tests:
if (ssShift in Shift) then....
if (ssLeft in Shift) then...
ssShift and ssLeft are pre-defined Delphi words, rather like clRed is a pre-defined way of referring to the numerical code which will color things red.

The important thing to notice is the word "in", and to understand "Shift". The latter is just a variable name, like so many others, but it happens of be a variable of the type "set".

I've not seen much use made of sets... call mine a sheltered life? They can be extremely useful, and Delphi provides you with some useful tools.

In this instance, the OnMouseMove event handler is passed not only the location of the mouse pointer, on the screen (X and Y), but it is also supplied with a set of values, in the variable "Shift". Sometimes Shift will be empty, hold no values. Alternatively, it may hold a bunch of things, e.g. ssShift and ssCtrl and ssLeft , which would be the case when the Shift and Ctrl keys and the mouse's left button were all held down as the mouse was moved.

When we say "ssShift in Shift" we are saying, "Is the ssShift value in the set of values referred to by the name "Shift"?"

See the "OnKeyDown" event handler helpfile entry for a complete list of the things which can be present in "Shift" at the time of a call to the OnMouseMove event handler. They are mostly what you would expect.

I'm not providing the source-code for this as it is such a small scrap of code.

   Search this site or the web        powered by FreeFind
  Site search Web search
Site Map    What's New    Search

Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's freeware, shareware page.

Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.

Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org

If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....