-
March 2nd, 2005, 02:21 PM
#1
MouseMove on every control
I've got the code for an idle timeout on my program working, but I need to know if there's a way to make it so when the mouse moves regardless of which control the cursor is over, an event fires calling my function.
Right now as a test I just have
Code:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
But I would like to avoid doing that event on every control (Form_MouseMove, Button_MouseMove, etc...), and try and consolidate it to one line, I'm assuming that's possible I just can't figure it out.
-
March 2nd, 2005, 03:57 PM
#2
Re: MouseMove on every control
Two ways which are common are intercepting mouse messages, or using a timer to check the position every so often. Since your are looking for an idle condition, you are likely using a timer already, set to one minute, five minutes, or whatever. That being the case, you could simply check the mouse position, and compare it to the position at the time of the last check. You can use GetCursorPos, and store the results in a Static variable. Then the next timer event will compare the last position to the current one, and if changed, we know the user is active.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
-
March 3rd, 2005, 07:38 AM
#3
Re: MouseMove on every control
Originally Posted by WizBang
Two ways which are common are intercepting mouse messages, or using a timer to check the position every so often. Since your are looking for an idle condition, you are likely using a timer already, set to one minute, five minutes, or whatever. That being the case, you could simply check the mouse position, and compare it to the position at the time of the last check. You can use GetCursorPos, and store the results in a Static variable. Then the next timer event will compare the last position to the current one, and if changed, we know the user is active.
The problem is that the timer is there to count to 30 minutes since the last time the mouse was moved. Once it fires it then dumps people back to the login. That works fine providing that the mouse is moved over the form, but if they are in a control I seem to get hosed b/c it's not detecting that move.
-
March 3rd, 2005, 09:00 AM
#4
Re: MouseMove on every control
Originally Posted by High_D
The problem is that the timer is there to count to 30 minutes since the last time the mouse was moved. Once it fires it then dumps people back to the login. That works fine providing that the mouse is moved over the form, but if they are in a control I seem to get hosed b/c it's not detecting that move.
That won't happen if you use GetCursorPos. It will return the coords of the cursor for the entire screen.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
-
March 3rd, 2005, 11:15 AM
#5
Re: MouseMove on every control
Try this code:
Code:
Private Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Const MaxIdleTime = 1800 'IdleTime before the event (in secs)
Dim IdleTime as Long, LastKnownPoint as POINTAPI
Dim FunctionRunning as Boolean
Private Sub Form_Load()
LastKnownPoint.X = 0
LastKnownPoint.Y = 0
Timer1.Interval = 1000
FunctionRunning = False
End Sub
Private Sub tmrCheckIdle_Timer()
Dim LatestPoint as POINTAPI
GetCursorPos LatestPoint
If LatestPoint = LastKnownPoint Then
If FunctionRunning = True Then Exit Sub
IdleTime = IdleTime + 1
If IdleTime >= MaxIdleTime Then
FunctionRunning = True
Call TheFunction 'Change this to the sub you want calling when you idle for 30 mins.
End If
Else
LastKnownPoint = LatestPoint
IdleTime = 0
FunctionRunning = False
Endif
End Sub
This code will reset the idle time (in seconds) to 0 every time you move the mouse.
When the idle time reaches the MaxIdleTime value, it calls 'TheFunction'. You need to change this to whatever the name of the procedure is that you want to be called.
FunctionRunning is false as default, and becomes true when the MaxIdleTime is exceeded. When the mouse is moved again, FunctionRunning becomes false again to notify your procedure to stop what it's doing. During the time that FunctionRunning is true, the IdleTime does not increase and is not reset to 0. After the mouse is moved again, the last known point is updated, the idle time reset and FunctionRunning becomes false, allowing the idle time to increase.
Hope it's useful,
Burningmace
-
March 3rd, 2005, 03:39 PM
#6
Re: MouseMove on every control
Burningmace: That's the basic idea, though it's not necessary to check every second. If it were my program, I'd have the timer interval set to 1 minute (60000). Of course, this means changing the MaxIdleTime to 30, but it looks like the rest will work just as it is.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|