Solved

Application to move mouse

Posted on 2010-08-24
7
635 Views
Last Modified: 2012-06-22
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?
0
Comment
Question by:OB1Canobie
7 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33512191
      Windows.Forms.Cursor.Position = New System.Drawing.Point(Windows.Forms.Cursor.Position.X + 1, Windows.Forms.Cursor.Position.Y + 1)


0
 
LVL 14

Expert Comment

by:systan
ID: 33512239
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

0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 33512495
You don't need to move the mouse just inform the operating system your application is doing work with the display and it will reset the idle timer. You can use SetThreadExecutionState() with ES_CONTINUOUS | ES_DISPLAY_REQUIRED flags.
For example if your watching a DVD on your computer the application would call the above method, if they didn't the screen saver would interrupt the movie.
 

Imports System.Runtime.InteropServices



Public Class Form1

    Private Const ES_CONTINUOUS As Integer = &H80000000

    Private Const ES_DISPLAY_REQUIRED As Integer = &H2

    <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _

    Private Shared Function SetThreadExecutionState(ByVal esFlags As Integer) As Integer

    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SetThreadExecutionState(ES_CONTINUOUS Or ES_DISPLAY_REQUIRED)

    End Sub

End Class

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33512608
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).
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33512638
Thanks for posting that one egl1044.  Learned something new today!  =D
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33521796
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.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33521944
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>...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
Red error squiggly on vb.net 7 27
update textboxes with Streamreader 21 42
I need help converting a bitmap to an image in VB.Net 1 14
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now