Solved

Application to move mouse

Posted on 2010-08-24
7
639 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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