Link to home
Start Free TrialLog in
Avatar of OB1Canobie
OB1CanobieFlag for United States of America

asked on

Application to move mouse

I have a need to write an application in vb.net to move the mouse cursor every 4 minutes. The mouse movement can be minimal, but enough to keep screen saver from coming on. Does anyone have code to do this?
Avatar of Zhaolai
Zhaolai
Flag of United States of America image

      Windows.Forms.Cursor.Position = New System.Drawing.Point(Windows.Forms.Cursor.Position.X + 1, Windows.Forms.Cursor.Position.Y + 1)


Disable the screen saver instead;
Imports Microsoft.Win32


'// This goes on form_load

'// Reads and sets the registry to disable screensaver from kicking
Dim regKey As RegistryKey
Dim regVal As String
regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
regVal = regKey.GetValue("ScreenSaveActive")
If regVal = 1 Then
regKey.SetValue("ScreenSaveActive", "0")
End If
regKey.Close()


'//This goes on app exit

'// Reads and sets the registry to re-enable screensaver
Dim regKey As RegistryKey
Dim regVal As String
regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
regVal = regKey.GetValue("ScreenSaveActive")
If regVal = 0 Then
regKey.SetValue("ScreenSaveActive", "1")
End If
regKey.Close()


OR Do like this:
Microsoft.Win32.RegistryKey rkScreenSaver = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"  Control Panel\Desktop", true );

if ( (string)rkScreenSaver.GetValue( "ScreenSaveActive" ) == "1" )
{
rkScreenSaver.SetValue( "ScreenSaveActive", "0" );
rkScreenSaver.Close( );
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

In the example above which I never mentioned you would periodically call SetThreadExecutionState() once every minute is fine. When your finished and you want it to go back to normal use SetThreadExecutionState(ES_CONTINUOUS).
Avatar of Mike Tomlinson
Thanks for posting that one egl1044.  Learned something new today!  =D
No problem Idle_Mind it's very useful for background processing.  I would like to also point out is that I have always called this routine periodically but it's appears only neccessary if you use only ES_CONTINUOUS flag.
If you use both flags ES_CONTINUOUS | ES_DISPLAY_REQUIRED it locks the Idle counter all together until you remove the ES_DISPLAY_REQUIRED bit flag. That means with the two flags you only need call it once but periodically if using only ES_CONTINUOUS.
When I started using this the documentation wasn't the same from a couple years ago but they seem to have improved it over time with much better descriptions.
Whoops the above should read
If you use SetThreadExecutionState without* ES_CONTINUOUS flag.
That means with the two flags you only need call it once but periodically if using SetThreadExecutionState without* ES_CONTINUOUS flag.
<Fills up more coffee>...