Friday, May 24, 2024

Excel Follow Cursor

There are a few ways to track the position of the mouse cursor in Excel. However, if we want to react to mouse movement, we need to set a routine that monitors the position all the time and that’s very CPU-intensive. Another possibility is using one of Excel’s objects that allow mouse events. That’s the case of charts, for example, as well as ActiveX controls, among others. In this post we see how to use an ActiveX image in the background of the worksheet to capture mouse movement and follow the cursor position.

Mouse Position

One of the most common ways to track the position of the mouse cursor in Excel is using GetCursorPos and SetCursorPos functions of the windows library. Here’s how we declare those functions in Excel VBA.

 Private Declare PtrSafe Function GetCursorPos Lib "user32" (Point As POINTAPI) As Long
 Private Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Long


 Private Type POINTAPI
     x As Long
     y As Long
 End Type

 

We need a custom variable (POINTAPI) to assign the x and y coordinates when getting the cursor position. Note that x and y refer to the position in the entire screen in pixels. Then you can get the position of the mouse cursor as follows.

 Sub GetCurrentPosition()
     Dim Hold As POINTAPI
     GetCursorPos Hold
     MsgBox Hold.x & ", " & Hold.y
 End Sub

 

However, if we want to react to the movement of the mouse cursor when changing position, we should be running such routine all the time and that’s resource intensive and not efficient in Excel. Unfortunately, Excel does not really have a function that captures the event of moving the cursor over the worksheet, ribbon, or other components, but it does for some specific objects such as charts or ActiveX controls (see later).

 

With the approach explained above, we can also get the range/cell the mouse cursor is pointing at, if it is within the boundaries of the worksheet. We can put that into a range object variable as follows.

 Set GetRange = ActiveWindow.RangeFromPoint(Hold.x, Hold.y)

 

And we can also set or change the position of the cursor using the SetCursorPos function along the horizontal and vertical coordinates as indicated below.

 SetCursorPos pixelsX, pixelsY

 

Mouse Movement

As explained above, Excel cannot capture the event of moving the cursor over the worksheet or other places in the workbook such as the ribbon, unless setting up a specific routine that tracks the movement all the time. But doing so is not efficient and can slow down Excel significantly. On the other hand, and as mentioned earlier, some objects in Excel have mouse-related events. That’s the case for example of the Chart object and some ActiveX controls, which have three mouse events: MouseDown, MouseMove, and MouseUp. Thus, we could use an ActiveX control as background to capture mouse movement. As an example, we have added an ActiveX image associated with a MouseMove event procedure that triggers whenever the cursor moves over the image.


Any code inside the event procedure will run whenever the mouse cursor moves while inside the ActiveX image area. The parameters X and Y correspond to the coordinates of the mouse position every time it moves. Note that the coordinates here are in points relative to the position of the image. This means that the top left corner of the image corresponds to X=0 and Y=0.

 Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
     'code to run when the mouse moves within the image area
 End Sub

 

Other mouse-related events work in a similar way. MoseDown and MouseUp are triggered when pressing the mouse button down or up respectively.

 Private Sub Image1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
     'code to run when pressing down the mouse button within the image area
 End Sub

 

Ghost Eyes Movement

The ghost is a freeform shape with oval shapes that resemble the eyes. The eye pupils are black smaller oval shapes that move inside the main eye oval to keep a position relative to the position of the mouse cursor within the ActiveX image. That could be more accurately calculated using the function of the circumference of the sphere. But in this case, it is simply calculated as the quotient of the distance between the mouse cursor and the ghost and the diameter of the eye oval shape in both horizontal and vertical dimensions. The following picture shows the variables used to calculate the distance of the eye pupil relative to the center of the eye in order to reach a reasonable position of an eye staring at a point in the distance.


As explained earlier, the position of the mouse cursor is determined by the X and Y coordinates from the MouseMove event procedure. Note that we need to exit “design mode” after adding the ActiveX image to let Excel capture the event and trigger the macro. That’s done by deselecting “design mode” under Developer-Controls in the Excel ribbon.

 

Microsoft Excel cannot capture mouse movement while hovering over the ghost though. As an additional funny feature in this example, another macro makes the ghost turn blue by changing the color of the shape when clicking the ghost. That tries to resemble what happens in the game when PacMan eats the power-pellets that give PacMan the advantage to chase and eat the ghosts in order to earn points and send them back to the starting chamber.


Download Excel Follow Cursor


No comments:

Post a Comment

Popular Posts