-
Sep 17th, 2020, 12:12 PM
#1
Thread Starter
New Member
[RESOLVED] Mouse move API problem.
A long time ago, I created a small program to move the mouse pointer at fixed intervals of time. The goal was to move the pointer just slightly to prevent a screen saver from loading or an auto-monitor power-off. It would move the pointer from its current position to one pixel to the left, and then back again regardless of the pointers position on the screen. My problem, I lost the code when I accidentally initiated a format of an external hard drive I used for storage. Close to 600MB of anything and everything, gone. I felt really bad.
So, below is my attempt to recreate the program. The public declarations are in a module, and the rest in a form. The only purpose of a small form is to unload the program. What I have does not work. It puts the pointer in the far upper-left corner of the screen on each iteration no matter how far I move it away. The timer interval is one second for testing. Once working, I planned to set the interval much longer. If anyone could take a look and tell me what I am doing wrong, I would appreciate it.
Code:
Option Explicit
Public Const MOUSEEVENTF_MOVE = &H1
Public Declare Sub mouse_event Lib "user32.dll" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy _
As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
===============================
Option Explicit
Dim C As Integer
Dim A As Long
Dim B As Long
Sub MoveMouse(x As Single, y As Single)
Dim RetVal As Long
mouse_event MOUSEEVENTF_MOVE, 0, 0, x, y
RetVal = SetCursorPos(x, y)
End Sub
Private Sub Form_Load()
Me.Top = 100
Me.Left = 100
End Sub
Private Sub tmrMov_Timer()
C = C + 1
If C Mod 2 = 0 Then
MoveMouse 1, 0
Else
MoveMouse -1, 0
End If
If C >= 2 Then C = 0
End Sub
-
Sep 17th, 2020, 01:19 PM
#2
Re: Mouse move API problem.
parameters are in wrong order
Code:
mouse_event MOUSEEVENTF_MOVE, 0, 0, x, y
should be:
Code:
mouse_event MOUSEEVENTF_MOVE, x, y, 0, 0
look again at the parameters in the API declaration.
-
Sep 17th, 2020, 05:59 PM
#3
Thread Starter
New Member
Re: Mouse move API problem.
-
Sep 17th, 2020, 06:28 PM
#4
Re: Mouse move API problem.
Code:
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetMessageExtraInfo Lib "user32" () As Long
Private Const MOUSEEVENTF_MOVE = &H1
Private Const MOUSEEVENTF_ABSOLUTE = &H8000&
Code:
mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, X, Y, 0&, GetMessageExtraInfo()
-
Sep 17th, 2020, 06:31 PM
#5
Re: Mouse move API problem.
Originally Posted by ndwaynep
Sorry. No affect.
That's not quite descriptive.
P.S. Why are you moving the mouse then using SetCursorPos. Can't believe that didn't trigger with me earlier. Use one or the other; definitely don''t use both.
Understand what your code is doing:
mouse_event is adjusting the current mouse coords by 1 or -1, depending on your timer event
SetCursorPos is overriding that, setting the mouse coords to 1,0 or -1,0 depending on timer event; -1 may be 0 since I don't know if you can move the cursor off screen (not sure).
mouse_event has another flag: MOUSEEVENTF_ABSOLUTE else the move is relative
absolute: move to these coordinates
relative: adjust current coordinates by this much
@Eduardo. His timer event is moving the mouse relative not absolute
Last edited by LaVolpe; Sep 17th, 2020 at 08:15 PM.
-
Sep 17th, 2020, 06:33 PM
#6
Re: Mouse move API problem.
More complete:
Code:
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetMessageExtraInfo Lib "user32" () As Long
Private Const MOUSEEVENTF_MOVE = &H1
Private Const MOUSEEVENTF_ABSOLUTE = &H8000&
Code:
Public Sub MakeMouseMove()
Dim iCP As POINTAPI
Dim iPixelXInMouseCoord As Double
Dim iPixelYInMouseCoord As Double
GetCursorPos iCP
iPixelXInMouseCoord = 65535 / (Screen.Width \ Screen.TwipsPerPixelX)
iPixelYInMouseCoord = 65535 / (Screen.Height \ Screen.TwipsPerPixelY)
iCP.x = iCP.x * iPixelXInMouseCoord
iCP.y = iCP.y * iPixelYInMouseCoord
mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, iCP.x + iPixelXInMouseCoord, iCP.y + iPixelYInMouseCoord, 0&, GetMessageExtraInfo()
DoEvents
mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, iCP.x, iCP.y, 0&, GetMessageExtraInfo()
End Sub
-
Sep 17th, 2020, 10:36 PM
#7
Re: Mouse move API problem.
do u really need to move the mouse for this purpose?
it feels odd that theres nothing else that can disable the screensaver/auto poweroff.
why not use SetThreadExecutionState
https://docs.microsoft.com/en-us/win...executionstate
-
Sep 18th, 2020, 08:19 AM
#8
Thread Starter
New Member
Re: Mouse move API problem.
What is below works and is far shorter than what I had before.
Code:
Option Explicit
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_MOVE = &H1
Dim C As Integer
Private Sub Form_Load()
Me.Top = 300
Me.Left = 300
Me.tmrMov.Interval = 1000
End Sub
Private Sub tmrMov_Timer()
C = C + 1
If C = 1 Then
mouse_event MOUSEEVENTF_MOVE, 3, 0, 0, 0
End If
If C = 3 Then
mouse_event MOUSEEVENTF_MOVE, -3, 0, 0, 0
End If
If C = 4 Then C = 0
End Sub
Using SetCursorPos uses the VB runtime and did not prevent the screen saver or prevent the power off. Calling user32.dll has the proper affect. Using this gives an alternate option of not having to go back into the settings and turn everything off. At night, I can close it and the settings will resume.
-
Sep 18th, 2020, 09:34 AM
#9
Re: [RESOLVED] Mouse move API problem.
another way, is to use this API
Code:
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Long, ByVal fuWinIni As Long) As Long
Private Const SPI_SETSCREENSAVEACTIVE = 17
Public Function ToggleScreenSaverActive(Active As Boolean) As Boolean
Dim lActiveFlag As Long
Dim retvaL As Long
lActiveFlag = IIf(Active, 1, 0)
retvaL = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, lActiveFlag, 0, 0)
ToggleScreenSaverActive = retvaL > 0
End Function
-
Sep 18th, 2020, 10:45 AM
#10
Re: Mouse move API problem.
Originally Posted by ndwaynep
Using SetCursorPos uses the VB runtime and did not prevent the screen saver or prevent the power off.
FYI: SetCursorPos simply moves the mouse, it doesn't add any mouse move events to active threads. Mouse_Event however does and those events are processed by the system as applicable, i.e., keeping screensaver from activating.
-
Sep 18th, 2020, 01:51 PM
#11
Re: [RESOLVED] Mouse move API problem.
According to this (I've not verified it), yet another way of suppressing the screensaver is by handling the WM_SYSCOMMAND(SC_SCREENSAVE) message. But I agree with baka and the most popular answer in that link — the SetThreadExecutionState API is the most elegant and effective solution to this problem.
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
|