Results 1 to 11 of 11

Thread: [RESOLVED] Mouse move API problem.

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Location
    Madison, IN
    Posts
    15

    Resolved [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

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Location
    Madison, IN
    Posts
    15

    Re: Mouse move API problem.

    Sorry. No affect.

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    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()

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Mouse move API problem.

    Quote Originally Posted by ndwaynep View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    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

  7. #7
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Location
    Madison, IN
    Posts
    15

    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.

  9. #9
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    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

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Mouse move API problem.

    Quote Originally Posted by ndwaynep View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    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
  •  



Click Here to Expand Forum to Full Width