Hi all,

I have got the problems to sense the mouse's movement in VB 6.0. How should I solve it? At first, I use Form_MouseMove function, but the movement will not be detected while the mouse pointer gets out of form. If so, I can't use it.
I know the mouse can detect left or right and up or down direction. How should I put the code in my program.

Please give me some suggestions as soon as possible
:( :confused:

Recommended Answers

All 13 Replies

You need to capture the X and Y position of the mouse.

Once the pointer gets out of the form you can't trace it.

The mouse's positions is determined by the handle of the window that is active. Once you go outside of a vb form, then you need to use other means to trace the mouse. This calls for a couple of Windows APIs. I'll look into it.

Hank

Use the GetCursorPos API

Option Explicit
Private type POINTAPI
    X as Long
    Y as Long
End Type

Private declare function GetCursorPos lib "User32" (lpPoint as POINTAPI) as long

You can use the function inside of a Timer Control. You need to make sure the timer control is enabled and set the interval at which you want the timer control event to fire. The interval property is measured in milliseconds; so, 1000 for the interval property would equal 1 second.

Private Sub Timer1_Timer()
dim Point as POINTAPI
dim lngReturn as long
dim lngScreenX as Long
dim lngScreenY as Long
lngReturn = GetCursorPos(Point)
lngScreenX = Point.X
lngScreenY = Point.Y
' Enter Code to whatever you want with the results
'  
debug.print "X-pos: " & lngScreenX & " , Y-pos: " & lngScreenY
End sub

Hi all,

Happy to get some suggestions. Now, I can detect the mouse movement by using the following sample program. But, I still can't detect it when the mouse pointer get the boundaries of the Screen although the mouse is moving.
Is there any ideal to solve it? I would like to detect the mouse movement at any situation.

But, I still can't detect it when the mouse pointer get the boundaries of the Screen although the mouse is moving.

If no handle is specified the GetCursorPos API only responds within the reference of the screen coordinates of your monitor:0,0 is your upper left hand corner of your monitor screen.'

You can change the area which the GetCursorPos API responds to by using the GetWindowRect API. Call that API and then call the GetCursorPos API.

If you want to show areas outside of the window whose handle the GetCursorPos API is using, you can declare static variables in the MouseMove Event to give values outside of the window. You'll have to determine the size of your screen first, however.

If your screen is at a resolution of 1024 x 768, then you can use those values as a reference in the MouseMove, MouseUp, and MouseDown events to programatically give you the value you desire.

Though why in the world you would want to know where your mouse isn't is beyond me.

Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Static StaticLongPos as long
'
If X = 0 then 
  StaticLongPos = StaticLongPos - 1
elseif X > 0 then
  StaticLongPos = 0
EndIf

This only deals with a mouse movement to the left: when the X value is 0 the mouse is moving left; so, you subtract a value.

You should use the same logic to deal with the top, bottom, and right hand sides.
And you'll have to deal with the values of the top of the Rectangle referenced by GetWindowRect, the rightmost side, and the bottommost side.

Hi,

GetCursorPos API only know the cursor position, but it dont' know whether the mouse is still moving or not. PC's resolution is correct.
Form_MouseMove also couldn't be used because it doesn't know or sense the mouse is moving while the cursor arrives outside of the Form.
I really would like to know how to sense the mouse is moving. Is it related with GetWindowRect API? If so, please show me how to combine and use them together.

it dont' know whether the mouse is still moving

I would say you're right. The GetCursorPos just pulls the current position in relation to the screen.

I really would like to know how to sense the mouse is moving.

I would say you're also right about not using MouseMove for sensing mouse movement outside of the form.

So, I would recommend using static variables in the timer function. I would declare regular (e.g., Dim lngScreenX as long) variables to assign the values obtained from the GetCursorPos API. And then I would also declare a couple of Static variables inside the Timer function to use to compare with the values of Point.X and Point.Y. I would assign the static variables value at the end of the timer function code after assigning the variables declared with a Dim statement.

If either of the Static variable's value differs from the Dim Declared variables inside the timer function has changed, then the mouse has moved.

Is it related with GetWindowRect API?

The GetWindowRect API just pulls the top, left, bottom, and right coordinates of the form with the handle referenced in the API. These coordinates will be in relation to the computer monitor's screen pixels: 0,0 being the top left and the bottom right being 1023, 767 (if monitor resolution is set as 1024 x 768).

Hi,

I would like to ask one more question. Is there any other API to detect mouse movement without using GetCursorPos API? Just for sensing the movement.
I have already run the program as you told me. But, X and Y positions are not change when the cursor arrive the boundary because X and Y are the location of screen. May be the code that is written by me is wrong.
Actually, I want to show the output when the mouse is moving where the cursor is. If mouse moving stops, the output will stop.
Could you please show me the sample code how to write down because I am not the professional?

Thanks.
zawpai

But, X and Y positions are not change when the cursor arrive the boundary because X and Y are the location of screen. May be the code that is written by me is wrong.

That's because the GetCursorPos retrieves the position of the cursor on the screen. Again, The GetCursorPos API retrieves the X Y position of the mouse cursor relative to the screen coordinates. I believe all mouse positions of this API are relative to the screen. When the mouse hits the boundaries it ceases to move. And hence the cursor is not moving.

Is there any other API to detect mouse movement without using GetCursorPos API?

GetMouseMovePointsEx API Function: retrieves up to 64 previous positions based on screen coordinates.

Actually, I want to show the output when the mouse is moving where the cursor is. If mouse moving stops, the output will stop.

Apparently, nobody from among the Windows designers sensed a need to track a mouse where it can't be seen. And your statement is not fully correct: The mouse on the screen only moves when the cursor moves. The mouse on the screen will not move if its cursor position stays the same.

Your physical mouse may move, but that does not necessarily mean that the mouse on the screen will move. You would have to declare a RECT structure that is greater in size than the screen coordinates. I don't know if it's possible to create a RECT structure as you desire. To do so you would have to have either negative values for the top left coordinates or values that are greater for the bottom right coordinates of the RECT structure. But trying to create a RECT structure of such dimensions that exceed the screen coordinates may throw an error in the function. I don't know. I haven't tried.

But in essence Windows is a visually interactive program. If you can't see it, why track it?

Hello,

I understand all what you mean. I am a development engineer. I saw some of the machine that is a simple robot can jog when mouse is tracking. I don't know which software they use. I really don't know whether it can be done it VB 6.0. I think their software can sense the actual mouse track.
If so, I am trying to get the answer. If you get the new information about it, please give me some suggestions. I will try to do this.

bye,
zawpai

Hi,

I really want to detect whether the physical mouse is tracking or not? Is there any ideals?

zawpai

I really want to detect whether the physical mouse is tracking or not? Is there any ideals?

If that's all you want to do, then just put the cursor back inside a window where it can be tracked

Use the SetCursorPos API.

If your only objective is to get mouse tracking and you want to use the existing APIs, then using these 2 together should do the trick. Your only other choice is to design your own dll which fits within your goals.

Perhaps you've seen some of those programs that make the mouse move to the right of the screen when it reaches the far left side. They are using the GetCursorPos and the SetCursorPos together.

So, you're not a professional. You're just a development engineer?

Hi,

Thank you very much. I have already tried the program as you told me. I think that will be ok. I use GetCursorPos and SetCursorPos together. When the cursor get any boundaries, I have set the cursor's position at the opposite side while the physical mouse is tracking. I will put it into my software.

Best Regards,
zawpai

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.